简体   繁体   中英

Discord API tells me: "401: Unauthorized" when i make a GET with Google-Scripts: "UrlFetchApp.fetch()"

I'm setting up a api connection. I want to get informations from discord api for my app.

So I implemented OAuth2 without any problems, I have my access token. Then I tried query some endpoints (/users/@me, /users/@me/guilds,...) but every time I get the same error.

I'm sending my authorization token in headers but it's still returning error 401.

I call the script with the generated url: https://discordapp.com/api/oauth2/authorize?client_id=XYZ&redirect_uri=https%3A%2F%2Fscript.google.com%2Fmacros%2Fs%2FAKfycbyyt9-FiVv0zXOr8p8pMfojwEs2AXvBftVN1xdWeU3UQ1xgURD%2Fexec&response_type=code&scope=identify

Here is my "authentication code":

function doGet(e){
  if(typeof e.parameter.code !== 'undefined') {
    var code = e.parameter.code;
    getAccessToken(code);
  }
  return ContentService.createTextOutput('someThink..');
}

function getAccessToken(code){
  var API_TOKEN_URL = 'https://discordapp.com/api/oauth2/token'
  var CLIENT_ID = 'XYZ'
  var CLIENT_SECRET = 'XYZ'
  var REDIRECT_URI = 'https://script.google.com/macros/s/AKfycbyyt9-FiVv0zXOr8p8pMfojwEs2AXvBftVN1xdWeU3UQ1xgURD/exec'
  data = {
    'client_id': CLIENT_ID,
    'client_secret': CLIENT_SECRET,
    'grant_type': 'authorization_code',
    'code': code,
    'redirect_uri': REDIRECT_URI,
    'scope': 'identify'
  }
  header = {
    'method' : 'post',
    'Content-Type': 'application/x-www-form-urlencoded',
    'payload' : data
  }
  var result = UrlFetchApp.fetch(API_TOKEN_URL, header);
  if (result.getResponseCode() == 200) {
    var params = JSON.parse(result.getContentText());
    Logger.log(params.access_token); // all is fine
    getUser(params.access_token, params.token_type)
  }  
}

And this code is my API request:

function getUser(accessToken, token_type){
  var API_USERS_URL = 'https://discordapp.com/api/users/@me'; 
  header2 = {
    'method' : 'GET',
    'Authorization': token_type + ' ' + accessToken,
    // I tested all of them
    // 'followRedirects' : true,
    // 'muteHttpExceptions': true,
    // 'Content-Type': 'application/json',
    // 'Content-Type': 'application/x-www-form-urlencoded',
  }
  var resultUsers = UrlFetchApp.fetch(API_USERS_URL, header2); // ERROR HERE !
  if (resultUsers.getResponseCode() == 200) {
    var paramsUser = JSON.parse(result.getContentText());
    Logger.log(paramsUser);
  }  
}

I accept the connection with discord with the same SCOPES: identify.

I tried so hard but I don't succeed. Every time the same error:

{"code": 0, "message": "401: Unauthorized"}

You're sending the header as the params / options parameter to UrlFetchApp.fetch() . Send the header as the header parameter in options :

var resultUsers = UrlFetchApp.fetch(API_USERS_URL,{headers: header2});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM