2

It just says {"error":"Unauthorized","status":401,"message":"error getting authorization token"}, don't know why as I can write on chat with my token. Here's the code:

url = "https://api.twitch.tv/kraken/channel"
channel_id = urllib.request.Request(url)
channel_id.add_header("Client-ID", CLIENT_ID)

#MY_OAUTH defined as MY_OAUTH = "oauth:123blablabla"
channel_id.add_header("Authorization: OAuth", MY_OAUTH")

response = urllib.request.urlopen(channel_id)
tmpJSON = json.loads(response.read())

EDIT: Here's the Pastebin of get_channel_id function: https://pastebin.com/Jm0EuWk9

1 Answers1

0

It seems that your Authorization header is ill-formed. Supposing that MY_OAUTH as your access token, I believe you meant to write :

channel_id.add_header("Authorization", "OAuth " + MY_OAUTH)

Indeed, the Twitch-API authentication documentation recommends performing the following request when passing the access token in the HTTP header:

curl -H "Authorization: OAuth [access token]" https://api.twitch.tv/kraken/

As you can see, the header needs to be set as Authorization: OAuth [access token], instead of Authorization: OAuth: [access token].

Pierre Thalamy
  • 311
  • 1
  • 3
  • 14
  • Oh, my bad. Anyway, I tried fixing my error but I obtain _urllib.error.HTTPError: HTTP Error 401: Unauthorized_ , that's weird becaure I am using the same OAuth when connecting to chat. EDIT: even with a fresh new token, Maybe I should truncate token and set is without "oauth:" at the beginning? – Pietro Chico Apr 13 '17 at 17:28
  • Indeed, your token string should not include any additional character than the token itself, and must be of the form `pk2bh6y1vi8mrn7l67bp9i6dpg2wnk`. Have you tried removing the facing "oauth:" then ? And are you sure that it is the access token you are passing ? – Pierre Thalamy Apr 13 '17 at 18:29
  • Yes I fixed that yesterday. For some reason I am not able to get channel id: JSON returned from server is way different from what it should be. Acording o API's, should have "channel_id" field. With same parameters, using curl, JSON string is correct. – Pietro Chico Apr 14 '17 at 06:40
  • I am not sure I understand, are you still getting a `401 Unauthorized` error, or does the server return data now ? If so, can you provide more information on the difference between what you are getting and [what it should be](https://dev.twitch.tv/docs/v5/reference/channels/#get-channel) ? – Pierre Thalamy Apr 14 '17 at 09:19
  • Now I'm only getting 401 Unauthorized error at _response = urllib.request.urlopen(channel_id)_ – Pietro Chico Apr 14 '17 at 10:05