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

需要一些帮助,

使用Wohali OAuth2 Client,然后使用Restcord从用户获取实际内容 .

但由于某种原因,我无法让它列出登录用户的角色 .

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

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

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,guild id等,但它们是有效的,因为来自Wohali的auth上的示例工作并在登录时显示我的信息 .

提前感谢任何提示/帮助/想法 .

回答(1)

2 years ago

您可以在官方不和谐文档上查看回复代码:

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

401表示您的令牌不好/丢失