Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord API 401 Unauthorized with OAuth

Quick question: I'm trying to use the Discord API to make a backup of all the messages on a server (or a guild, if you use the official term).

So I implemented OAuth without any problems, I have my access token and I can query some endpoints (I tried /users/@me, /users/@me/guilds). Though, most of them don't work. For example, if I query /users/@me/channels (which is supposed to be the DMs) I get a 401 Unauthorized response from the API. It's the same if I gather a guild id from /users/@me/guilds and then try to list the channels in it with /guilds/guild.id/channels.

The really weird thing is that I do have all the scopes required (I think so, I didn't take the RPC ones since I don't think it's required for what I want to do) and I can't figure it out myself... What is also weird is that on the OAuth authorization screen, I have those two things:

Read all messages

This app cannot read messages

It kind of counterdicts itself... :(

Do you have any ideas you'd like to share ?

Thanks!

Note: I'm using Python but I don't think it's related here, since some endpoints do work with the headers and tokens I have...

Here is my "authentication code":

baseUrl = "https://discordapp.com/api"

def authorize():
    scopes = [
        "guilds",
        "email",
        "identify",
        "messages.read",
        "guilds.join",
        "gdm.join",
        "connections"
    ]
    urlAuthorize = "{}/oauth2/authorize?client_id={}&scope={}&response_type=code".format(baseUrl, clientid, ('+'.join(scopes)))
    pyperclip.copy(urlAuthorize)

    code = input("Code: ")
    return code

def getAccessToken(code):
    url = "{}/oauth2/token".format(baseUrl)

    params = {
        "client_id" : clientid,
        "client_secret" : clientsecret,
        "redirect_uri" : "http://localhost",
        "grant_type":"authorization_code",
        "code" : code,
    }

    req = requests.post(url, params = params)
    return json.loads(req.text)

And the code related to an API request:

def getHeaders():
    return {
        "Authorization" : "{} {}".format("Bearer", config["accessToken"]),
        # "user-agent" : "DiscordBackup/0.0.1"
    } 

def getRequest(endpoint, asJson = True, additional = None):
    url = "{}/{}".format(baseUrl, endpoint)
    req = requests.get(url, headers = getHeaders())
    print()
    print(getHeaders())
    print(url)
    print(req.text)
    if asJson:
        return json.loads(req.text)
    else:
        return req.text

def getMe(): # this works
    endpoint = "users/@me"
    return getRequest(endpoint)

def getMyDMs(): # this gives me a code 401 Unauthorized
    endpoint = "/users/@me/channels"
    return getRequest(endpoint)
like image 596
Thomas Kowalski Avatar asked Oct 18 '22 13:10

Thomas Kowalski


1 Answers

I came across this post when encountering this issue, and to put it bluntly, there's no way to resolve it.

The messages.read permission is for a local RPC server; https://discordapp.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes

However, local RPC servers are in private beta and you must sign up/get accepted to use this.

I wanted to create a DM exporter, but that doesn't look likely now.

like image 111
Majored Avatar answered Oct 30 '22 19:10

Majored