Error 401: Unauthorized with Oauth2 for Discord



我目前正在通过Discord开发一个连接系统,使用PHP中的Oauth2 API。目前我能拿到密码来获取访问令牌。我得到了我的访问令牌但我不能得到连接用户的信息因为我得到了这个错误消息:

Client error: `GET https://discord.com/api/oauth2/@me` resulted in a `401 Unauthorized` response:
{ "message": "401: Unauthorized", "code": 0}.

在使用访问令牌测试失眠时,我得到相同的错误消息。我使用识别和电子邮件范围。这是我的代码,也许它能帮助你。

我想说明的是,谷歌的第一页已经看了,没有任何成功…

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

谢谢你以后的回答。

问题解决

$response=$client->request('GET',$userinfoEndpoint,[
'headers'=>[
'Authorization' =>'Bearer '.(string)$accessToken
]
]);

最新更新