我目前正在使用 PHP 中的 Oauth2 API 通过 Discord 开发连接系统。目前我可以获得获取访问令牌的代码。我获得了访问令牌,但无法获得有关连接用户的信息,因为我收到此错误消息:
Client error: `GET https://discord.com/api/oauth2/@me` resulted in a `401 Unauthorized` response:
{ "message": "401: Unauthorized", "code": 0}.
使用访问令牌测试 imsomnia 时,我收到相同的错误消息。我使用身份和电子邮件范围。 这是我的代码,也许可以帮到你。
我想说明已经查看了 Google 的第一页但没有任何成功...
try{
$tokenEndpoint='https://discord.com/api/oauth2/token';
$userinfoEndpoint='https://discord.com/api/oauth2/@me';
$response=$client->request('POST',$tokenEndpoint,[
'form_params' => [
'client_id' => DISCORD_ID,
'client_secret'=> DISCORD_SECRET,
'grant_type' => 'authorization_code',
'code' => $_GET['code'],
'redirect_uri'=> URL1
]
]);
$accessToken=json_decode($response->getBody())->access_token;
$response=$client->request('GET',$userinfoEndpoint,[
'headers'=>[
'Authorization' =>'Bearer' . $accessToken
]
]);
$response=json_decode($response->getBody());
if($response->verified===true)
{
//Verifier si util avec openid existe
//si il n'existe pas insert
//lancer le reste
session_start();
$_SESSION['email']=$response->email;
header('Location: index.php');
exit();
}
提前感谢您将来的回答。
答案 0 :(得分:1)
问题已解决
$response=$client->request('GET',$userinfoEndpoint,[
'headers'=>[
'Authorization' =>'Bearer '.(string)$accessToken
]
]);