【问题标题】:Getting the Guild Roles for a User using Restcord and Wohali OAuth2 Discord Client使用 Restcord 和 Wohali OAuth2 Discord 客户端为用户获取公会角色
【发布时间】:2019-02-01 08:06:06
【问题描述】:

需要帮助,

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

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

说“消息:执行 listGuildMembers 命令时出错:客户端错误:GET https://discordapp.com/api/v6/guilds/{guild_id}/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 示例可以正常工作并在登录时显示我的信息。

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

【问题讨论】:

    标签: php oauth-2.0 discord


    【解决方案1】:

    您可以在官方 discord 文档中查看响应代码:

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

    401 表示您的令牌错误/丢失

    【讨论】:

      猜你喜欢
      • 2021-11-29
      • 2020-09-19
      • 2020-08-25
      • 2019-06-16
      • 2021-06-23
      • 2018-08-20
      • 2021-08-09
      • 2021-01-06
      • 1970-01-01
      相关资源
      最近更新 更多