Discord API。尝试获取公会频道时出现错误401

时间:2019-12-05 03:17:25

标签: node.js discord

因此,我已经弄清楚了Oauth流程,并且能够获得用户所在的行会数组。但是现在,我从其中一个行会中获取行会ID,并尝试获取其渠道。

我的代码部分如下:

 var accessToken;
console.log("about to make axios call");
axios.post(Oauth.discord_token_url, payload, config)
.then(response => {
  console.log(response.data);
  accessToken = response.data.access_token;
  const getUserUrl = (Oauth.discord_api_url + "/users/@me/guilds");
  let config1 = {
    headers: {
        "Authorization" : ("Bearer " + accessToken)
    }
  };
  axios.get(getUserUrl, config1)
  .then(response => {
    console.log("GOT USER");
    console.log("USER " + JSON.stringify(response.data))
    const guildId = response.data[0].id;
    //make some print statements to find out what we aren't getting
    console.log("GUILD_ID " + guildId);
    const getChannelUrl = (Oauth.discord_api_url + "/guilds/" +guildId + "/channels");
    console.log("CHANNELS URL: " + getChannelUrl);
    let config2 = {
      headers: {
          "Authorization" : ("Bearer " + accessToken)
      }
    };
    console.log("ACCESS_TOKEN: " + accessToken );
    axios.get(getChannelUrl, config2)
    .then(response => {
      console.log ("GOT THE CHANNELS");
      console.log("CHANNEL INFO: " + JSON.stringify(response.data));
    })

  })

我得到用户行会的第一个GET请求起作用。但是,我尝试获取频道的第二个GET请求不起作用。我收到错误401:无效的授权。我做的和第一个一样,根据不和谐的文档,URL似乎是正确的。知道我在这里缺少什么吗?

我的输出如下:

 GUILD_ID 91294111071469568
CHANNEL URL: https://discordapp.com/api/guilds/91294111071469568/channels
ACCESS_TOKEN: l33RHTHzisyM6YIieiMXW3oCGCQnxP
(node:27118) UnhandledPromiseRejectionWarning: Error: Request failed with status code 401
    at createError (/home/robbie/r6Finder/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/home/robbie/r6Finder/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/home/robbie/r6Finder/node_modules/axios/lib/adapters/http.js:237:11)
    at IncomingMessage.emit (events.js:194:15)
    at endReadableNT (_stream_readable.js:1103:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:27118) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:27118) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

0 个答案:

没有答案