使用Restcord和Wohali OAuth2 Discord客户端获取用户的行会角色

时间:2018-08-26 16:34:12

标签: php oauth-2.0 discord

需要帮助,

使用Wohali OAuth2客户端,然后使用Restcord从登录的用户那里获取实际内容。

但是由于某些原因,我无法列出已登录用户的角色。

说“消息:执行listGuildMembers命令时出错:客户端错误:GET https://discordapp.com/api/v6/guilds/ {guild_id} / members?limit = 1导致401未经授权的响应:”

文档有点缺乏,我花了几个小时试图整理文档。

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);

我删除了clientId,secret,行会ID等,但是它们是有效的,因为Wohali上auth上的示例可以正常工作,并且在登录时显示了我的信息。

在此先感谢您提供任何提示/帮助/想法。

1 个答案:

答案 0 :(得分:0)

您可以在官方不一致的文档上查看响应代码:

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

401表示您的令牌不正确/丢失