【问题标题】:Error 401: Unauthorized with Oauth2 for Discord错误 401:未经 Oauth2 授权用于 Discord
【发布时间】:2021-10-01 11:44:49
【问题描述】:

我目前正在使用 PHP 中的 Oauth2 API 通过 Discord 开发连接系统。目前我可以获得获取访问令牌的代码。我获得了访问令牌,但我无法获得有关连接用户的信息,因为我收到以下错误消息:

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

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

我想指定 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();
        }

提前感谢您未来的回答。

【问题讨论】:

    标签: php authentication oauth-2.0 discord guzzle


    【解决方案1】:

    问题解决了

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

    【讨论】:

      猜你喜欢
      • 2019-02-01
      • 2019-08-09
      • 2013-09-13
      • 2017-09-27
      • 2017-06-04
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多