dongyi8383 2017-01-03 00:13
浏览 81

使用cURLY坚持使用PHP OAuth 2.0,grant_type不受支持

I'm trying to connect to an OAuth2 Provider using cURL in PHP 5. I established the initial authorization, and got that working fine.

However, I'm getting stuck with getting it to send me a token. I keep hitting the "unsupported_grant_type" error. This is my code so far:

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

As you can see, I'm sending the data post to the authorization_code grant type. $code is the returned code form the authorize endpoint. I also have my form data set as x-www-form-urlencoded.

Can someone please point me into the direction of what I'm doing wrong here?

I've searched for a few hours now on Google, SO, etc... I'm not seeing any fresh advice that's getting me anywhere else other than where I already am.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥35 平滑拟合曲线该如何生成
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 自己瞎改改,结果现在又运行不了了
    • ¥15 链式存储应该如何解决
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站