dongshubang7816 2018-08-26 16:34
浏览 129
已采纳

使用Restcord和Wohali OAuth2 Discord客户端为用户获取公会角色

Need some help,

Using Wohali OAuth2 Client then Restcord to get actual content from the user logged.

But for some reason, i can't get it to list the roles for the user logged in.

Says "Message: There was an error executing the listGuildMembers command: Client error: GET https://discordapp.com/api/v6/guilds/{guild_id}/members?limit=1 resulted in a 401 UNAUTHORIZED response:"

The documentation is a bit lacking and i have spent a number of hours trying to get it sorted out.

use RestCord\DiscordClient;

$provider = new \Wohali\OAuth2\Client\Provider\Discord([
  'clientId' => '{client_id}',
  'clientSecret' => '{client_secret}',
  'redirectUri' => '{redirect_uri}'
]);

$options = ['state' => 'CUSTOM_STATE', 'scope' => ['identify', 'email', 'guilds']];

if (!isset($_GET['code'])) {

  // Step 1. Get authorization code
  $authUrl = $provider->getAuthorizationUrl($options);
  $_SESSION['oauth2state'] = $provider->getState();
  header('Location: ' . $authUrl);

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

  unset($_SESSION['oauth2state']);
  exit('Invalid state');

} else {

  // Step 2. Get an access token using the provided authorization code
  $token = $provider->getAccessToken('authorization_code', [
      'code' => $_GET['code']
  ]);
}

$user = $provider->getResourceOwner($token);

$client = new DiscordClient(['token' => $token->getToken(), 'tokenType' => 'OAuth']);

$roles = $client->guild->listGuildRoles(['guild.id' => {guild_id}]);

var_dump($roles);

I have removed the clientId, secret, guild id and such but they are valid, since the example on auth from Wohali works and shows my info when logged in.

Thanks in advance for any tips / help / ideas.

  • 写回答

1条回答 默认 最新

  • douwo5710 2018-09-26 21:50
    关注

    You can view the response codes on the official discord docs:

    https://discordapp.com/developers/docs/topics/opcodes-and-status-codes#http-http-response-codes

    401 means your token is bad/missing

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?