# (Python) Code worked once and now it's 401:Unauthorized

**URL:** <https://staging-discuss.dev.twitch.com/t/python-code-worked-once-and-now-its-401-unauthorized/1741>\
**Category:** API\
**Created:** [March 1, 2015, 5:46pm UTC](https://staging-discuss.dev.twitch.com/t/python-code-worked-once-and-now-its-401-unauthorized/1741 "2015-03-01T17:46:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![switchpinf](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@switchpinf](https://staging-discuss.dev.twitch.com/u/switchpinf)\
**Post date:** [March 1, 2015, 5:46pm UTC](https://staging-discuss.dev.twitch.com/t/python-code-worked-once-and-now-its-401-unauthorized/1741/1 "2015-03-01T17:46:53Z")

</div>

So I made a code to change the title of stream, using “requests” lib of python. It worked 1 time and then it stopped. I even tried signing up for new client-ID and got new token and it still doesn’t work.  
It returns either “HTTPError: 401 Client Error: Unauthorized” or “HTTPError: 401 Client Error: Invalid token or missing local scope”  
Here’s the code:

```
import requests
token = "my_token";
headers = {'Authorization':'OAuth ' + token,'Accept':'application/vnd.twitchtv.v3+json', 'Client-ID':'my_client_id'}

channel = "my_channel"
lmao = {'channel[status]' : 'lmao','channel[game]' : 'Diablo','channel[delay]' : '0'}
    
r = requests.put('https://api.twitch.tv/kraken/channels/my_channel', headers = headers, params = lmao).raise_for_status()
print r.content
```

---

<div class="post-metadata">

**Author:** ![switchpinf](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@switchpinf](https://staging-discuss.dev.twitch.com/u/switchpinf)\
**Post date:** [March 1, 2015, 6:14pm UTC](https://staging-discuss.dev.twitch.com/t/python-code-worked-once-and-now-its-401-unauthorized/1741/2 "2015-03-01T18:14:45Z")

</div>

Ok, so the problem is not in the code. When I try to check any of my tokens with GET on `https://api.twitch.tv/kraken/?oauth_token=`, it says `"token":{"valid":false,`  
So how come they are invalid from the start? I just made new application, got new token and 1 second later, when I check, it’s invalid

---

<div class="post-metadata">

**Author:** ![Fugiman](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/fugiman/32/108_2.png) [@Fugiman](https://staging-discuss.dev.twitch.com/u/Fugiman)\
**Post date:** [March 2, 2015, 7:52pm UTC](https://staging-discuss.dev.twitch.com/t/python-code-worked-once-and-now-its-401-unauthorized/1741/3 "2015-03-02T19:52:33Z")

</div>

You’re likely getting an authorization code instead of an oauth token. I recommend reading [https://github.com/justintv/Twitch-API/blob/master/authentication.md](https://github.com/justintv/Twitch-API/blob/master/authentication.md) to better understand the oauth flow.

---

<div class="post-metadata">

**Author:** ![system](https://d2spem0quai2ol.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://staging-discuss.dev.twitch.com/u/system)\
**Post date:** [April 1, 2015, 8:06pm UTC](https://staging-discuss.dev.twitch.com/t/python-code-worked-once-and-now-its-401-unauthorized/1741/4 "2015-04-01T20:06:14Z")

</div>

Closed on April 1, 2015, 8:06pm UTC

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