# Cant login as bot in python

**URL:** <https://staging-discuss.dev.twitch.com/t/cant-login-as-bot-in-python/23704>\
**Category:** API\
**Created:** [January 9, 2020, 1:27pm UTC](https://staging-discuss.dev.twitch.com/t/cant-login-as-bot-in-python/23704 "2020-01-09T13:27:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![llownall](https://avatars.discourse-cdn.com/v4/letter/l/6de8d8/32.png) [@llownall](https://staging-discuss.dev.twitch.com/u/llownall)\
**Post date:** [January 9, 2020, 1:27pm UTC](https://staging-discuss.dev.twitch.com/t/cant-login-as-bot-in-python/23704/1 "2020-01-09T13:27:48Z")

</div>

Trying to receive messages from chat continuously, but noticed that twitch disconnects me after approximately 10 min, so I created app here [https://dev.twitch.tv/console/apps](https://dev.twitch.tv/console/apps) and got access\_token by sending client\_id and client\_secret to [id.twitch.tv/oauth2/token](http://id.twitch.tv/oauth2/token). After that I try to send pass and nick using socket but I receive :tmi.twitch.tv NOTICE \* :Login unsuccessful

```
import socket
import requests

r = requests.post(f'https://id.twitch.tv/oauth2/token'
                  f'?client_id={CLIENT_ID}'
                  f'&client_secret={CLIENT_SECRET}'
                  f'&grant_type=client_credentials'
                  f'&scope=chat:read chat:edit channel:moderate')

resp = r.json()

PASS = resp['access_token']

s = socket.socket()
s.connect((Host, Port))
s.send(f"PASS oauth:{PASS}\r\n".encode("utf-8"))
s.send(f"NICK {Nickname.lower()}\r\n".encode("utf-8"))
s.send(f"JOIN {Channel}\r\n".encode("utf-8"))

while True:
    rec = s.recv(1024).decode("utf-8")
    print(rec)
```

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [January 9, 2020, 1:29pm UTC](https://staging-discuss.dev.twitch.com/t/cant-login-as-bot-in-python/23704/2 "2020-01-09T13:29:14Z")

</div>

You cannot use a token of type “client\_credentials” to login to Twitch chat.

It doesn’t represent a user

You need a user Access Token:

> **[Getting OAuth Access Tokens](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/)**
>
> Getting OAuth Access Tokens

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [January 9, 2020, 2:09pm UTC](https://staging-discuss.dev.twitch.com/t/cant-login-as-bot-in-python/23704/5 "2020-01-09T14:09:06Z")

</div>

A token of type `client_credentials` won’t let you login to chat.

It doesn’t have scopes on it (even though you requested)

To login to chat you need a user token (`response_type` `code`) that has the relevant chat scopes on it, and the `access_code` you exchange for needs to be for the user of “nickname” in your code.

The oAuth loop is done separately to your main bot code flow, since it needs a web server (even a localhost one) and user interaction to validate/access the permission(s), and you just throw a `refresh token` loop in instead.

---

<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:** [February 8, 2020, 2:09pm UTC](https://staging-discuss.dev.twitch.com/t/cant-login-as-bot-in-python/23704/6 "2020-02-08T14:09:09Z")

</div>

Closed on February 8, 2020, 2:09pm UTC

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