0

我正在尝试使用 PHP 5 中的 cURL 连接到 OAuth2 提供程序。我建立了初始授权,并且工作正常。

但是,我无法让它向我发送令牌。我一直遇到“unsupported_grant_type”错误。到目前为止,这是我的代码:

    $token_url = 'https://discordapp.com/api/oauth2/token';

    if (!$code)
    {
        echo '<a href="'. $auth_url . '">Login with Discord</a>';
    } else {
        // cURL Request Goes Here.
        $chB = curl_init();
        $post_opts = array(
            'client_id'     =>  '[id_goes_here]',
            'client_secret' =>  '[secret_goes_here',
            'code'          =>  $code,
            'redirect_uri'  =>  '[valid_redirect_uri_goes_here]',
            'grant_type'    =>  'authorization_code',
        );

        $c_opts = array(
            CURLOPT_URL     =>  $token_url,
            CURLOPT_POST    =>  true,
            CURLOPT_RETURNTRANSFER  =>  true,
            CURLOPT_HTTPHEADER  =>  array(
                'Content-type: application/x-www-form-urlencoded',
            ),
            CURLOPT_POSTFIELDS  =>  $post_opts,
        );

        curl_setopt_array($chB, $c_opts);

        $run_now = curl_exec($chB);
        $code = curl_getinfo($chB, CURLINFO_HTTP_CODE);
        curl_close($chB);

        print($code);
        var_dump($run_now);
    }

如您所见,我将数据发布发送到 authorization_code 授权类型。$code 是从授权端点返回的代码。我的表单数据集也设置为 x-www-form-urlencoded。

有人可以指出我在这里做错的方向吗?

我现在已经在 Google、SO 等网站上搜索了几个小时……除了我已经在的地方之外,我没有看到任何新的建议让我在其他任何地方都能找到。

4

0 回答 0