1
votes

Discord recently announced that they move their API from discordapp.com to discord.com.

I'm in the midst of rewriting some code of mine (Google Apps Script project) to account for that, but I'm running into the problem of not being able to access the discord.com-endpoints, e.g.:

function example(){
  var headers =
  {
    "Authorization": "Bot AbCdEfG123",
    "Accept": "*/*"
  };
  var options =
  {
    "method": "GET",
    "headers": headers,
  };
  var response = UrlFetchApp.fetch("https://discord.com/api/guilds/"+GUILD_ID+"/members/"+MEMBER_ID, options);
}

even though using the old discordapp.com works perfectly fine. Using discord.com results in a 403 error, with error code 1020.

Further investigation:

  • Performing the same requests (against discord.com) via Postman or curl is successful, so it looks like the problem lies with UrlFetchApp
  • By adding "Accept": "*/*" to the headers, I found out that the error comes from Cloudflare, along with the message Please enable cookies.
  • Adding a User-Agent to the headers, e.g. "User-Agent": "MyBot/1.0" doesn't change anything

I have no idea how to deal with this and I'm wondering if anyone else has encountered this issue. I'm grateful for any help.

edit:

Here's the requested documentation:

The curl code that works is

curl -H "Authorization: Bot [...]" "https://discord.com/api/guilds/[...]/members/[...]"

with an ellipsis [...] representing the token or IDs

edit 2:

Google Apps Script uses the following unmodifiable user-agent:

Mozilla/5.0 (compatible; Google-Apps-Script; beanserver; +https://script.google.com; id: [...])

Even if I take only the Mozilla part into my curl command, i.e.

curl -H "Authorization: Bot [...]" -H "user-agent: Mozilla" "https://discord.com/api/guilds/[...]/members/[...]"

I get the same 1020 error code.

1
Edit to Provide 1.Documentation link and 2. Curl code - TheMaster
Did you miss the Bot and the space before the token in authorization header? - TheMaster
Thanks for the idea. The Bot is there, right where it belongs. - PiFlavour
Apps script doesn't support User-Agent header. But I'm not sure whether that is a problem. 403 could mean you missed the authorization header or missed Bot . Do edit your question code to make it clear that you use Bot in the correct position. Also add Accept: headers in the code. Please enable cookies. would mean you're redirected from a error page to a html page. - TheMaster
Check whether your error code is from any of this: discord.com/developers/docs/topics/opcodes-and-status-codes - TheMaster

1 Answers

0
votes

You need to set the user agent per this format https://discord.com/developers/docs/reference#user-agent

User-Agent: DiscordBot ($url, $versionNumber)