Stuck on PHP OAuth2.0 with cURL, grant_type_unsupported

678 views Asked by At

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

There are 0 answers