2

So what I'm trying to do is I'm creating some kind of dashboard for my bot and I want to get the current guilds roles. When I try to do that, I get {'message': '401: Unauthorized', 'code': 0} as response. I tried everything and looked at multiple questions as well as issues on github

json.loads(discord_oauth.discord.get(f"/api/guilds/{server}/roles", headers={"Authorization": "client-secret-here"}).content)

If you ask why I didnt put Bearer in front of my client secret thats because the library I'm using already puts that in front. So the header will return like this: {"Authorization": "Bearer client-secret-here" (I tried putting that too but it didn't work either)

I tried doing it with requests.get() too but it didnt work either. I always got {'message': '401: Unauthorized', 'code': 0} as response. How can I fix this?

(This code doesn't work only when I'm trying to get guild channels and guild roles. It works with guild and user info)

My scopes are: ["guilds", "identify"]

Emir Sürmen
  • 884
  • 3
  • 14
  • 33

1 Answers1

4

HTTP 401 responses are always authorization errors, telling you that you're either not providing the correct credentials, or your credentials are expired.

Did you get a client token from Discord? If so, are you using the syntax provided by the Discord REST API documentation for bots? Based on what I'm seeing, your line should look something like:

token = 'MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWs'
headers = {"Authorization": f"Bot {token}"}
result = discord_oauth.discord.get(f"/api/guilds/{server}/roles", headers=headers).content
data = json.loads(result)

If you've followed these directions and you're still getting 401 errors, I would bet that your credentials are invalid, implying the token has been revoked. That would mean you should probably get a new token.

ZackWolf
  • 104
  • 1
  • 4
  • Where can i get a client token? I think thats my problem because i cant seem to find it @ZackWolf – Emir Sürmen Feb 25 '21 at 19:51
  • @Mr.Beanos.- You need to register an application with Discord. The instructions for doing so are in the documentation I linked to, but here's a link to the Apps page: https://discord.com/developers/applications – ZackWolf Feb 25 '21 at 22:04
  • 4
    I am not using a bot so I used "Bearer Token" in this one, https://discord.com/developers/docs/reference#authentication, it still doesnt work @ZachWolf – Emir Sürmen Feb 26 '21 at 07:04
  • 1
    Facing the same issue. I even got 401 for my own guild/server by the access token with scopes of guilds and guilds.member.read. – Alexander Mung-Zit Chiu Feb 17 '23 at 02:44