401 Unauthorized (RestClient::Unauthorized)

I’m building an app in Ruby that checks if a specific streamer is currently broadcasting. However, when I attempt to make an API request, I receive a 401 error.

require "launchy"
require "colorize"
require 'rest-client'
require "json"
require_relative "lib/config/config.rb"

puts "What's your streamer name: ".green
streamer = gets.chomp

url = "https://api.twitch.tv/helix/streams?user_login=#{streamer}"

response = RestClient.get(url, headers: {
    "Authorization": "Bearer " + Config.token,
    "Client-Id": Config.client_id
})

config file:

require "dotenv/load"

module Config
    def self.client_id
        client_ID = ENV["CLIENT_ID"]
    end

    def self.token
        token = ENV["TOKEN"]
    end

end

When I tried to make a request with Postman, I received the following JSON response:

{
	"error": "Unauthorized",
	"status": 401,
	"message": "OAuth token is missing"
}

I don’t know if the issue has to do with OAuth when I registered my application with redirect urls as http://localhost because I didn’t think I would need a local server to make a simple request.

Your post suggests you tried to use your secret as a token.

A secret is not a token

This seems to be server to server so you would want to generate a client credentials token for use Getting OAuth Access Tokens | Twitch Developers

I named my secret as token. But I still don’t understand very much the issue.I need to make a request with my secret to https://id.twitch.tv/oauth2/token?

Yes you need make a call to https://id.twitch.tv/oauth2/token to generate a client_credentials oAuth token to use with the Twitch API (for public resources)

1 Like

Thank you it solved my problem

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.