[Python] Discord API Endpoints not functioning with requests properly, despite in-browser...

Discussão em 'Python' iniciado por Stack, Março 3, 2025.

  1. Stack

    Stack Membro Participativo

    I've been trying to add a user with Python's requests library, however it doesn't seem to work and gives me this error response: {"message": "401: Unauthorized", "code": 0} Here is my code:

    import requests

    headers = { 'accept': '*/*',
    'accept-language': 'en-US,en;q=0.9',
    'authorization': 'TOKEN',
    'content-type': 'application/json',
    'content-length': 2,
    'sec-ch-ua': '\'Not_A Brand\';v=\'99\', \'Brave\';v=\'109\', \'Chromium\';v=\'109\'',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '\'Windows\'',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'sec-gpc': '1',
    'x-context-properties': 'eyJsb2NhdGlvbiI6IlVzZXIgUHJvZmlsZSJ9',
    'x-debug-options': 'bugReporterEnabled',
    'x-discord-locale': 'en-US',
    'x-super-properties': 'eyJvcyI6IldpbmRvd3MiLCJicm93c2VyIjoiQ2hyb21lIiwiZGV2aWNlIjoiIiwic3lzdGVtX2xvY2FsZSI6ImVuLVVTIiwiYnJvd3Nlcl91c2VyX2FnZW50IjoiTW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzEwOS4wLjAuMCBTYWZhcmkvNTM3LjM2IiwiYnJvd3Nlcl92ZXJzaW9uIjoiMTA5LjAuMC4wIiwib3NfdmVyc2lvbiI6IjEwIiwicmVmZXJyZXIiOiIiLCJyZWZlcnJpbmdfZG9tYWluIjoiIiwicmVmZXJyZXJfY3VycmVudCI6IiIsInJlZmVycmluZ19kb21haW5fY3VycmVudCI6IiIsInJlbGVhc2VfY2hhbm5lbCI6InN0YWJsZSIsImNsaWVudF9idWlsZF9udW1iZXIiOjE3MTYyMSwiY2xpZW50X2V2ZW50X3NvdXJjZSI6bnVsbH0=',
    'referrer-Policy': 'strict-origin-when-cross-origin'}

    r = requests.put('https://discord.com/api/v9/users/@me/relationships/USER_ID', json='{}')

    match r.status_code:
    case 204:
    print('Added a user')

    case _:
    print(f'Unexpected error: {r.status_code} | {r.text}')


    I would've thought this would work the same as this: Replay XHR

    I've tried debugging my code, switching things around, adding every header I see in the XHR data, I just can't get it to work. Help would be appreciated.

    Continue reading...

Compartilhe esta Página