Using Laravel PHP - Getting unsupported_grant_type error

Hello,

I've been browsing the forum for an answer but unfortunately didn't find a fix to my issue yet.

I'm using PHP Laravel framework, trying to get my access token, this is what I keep getting:

+"error": "unsupported_grant_type"
+"error_description": "The value specified for the grant_type parameter '' was not valid."

After trying to use my usual way (Guzzle) with no success,

I copied the code from the tutorial, Here's the code I'm using right now:

$postRequest = array(
'code: '.$code,
'grant_type: authorization_code',
'redirect_uri: https://nmf.nmf/blackbaud'
);

$cURLConnection = curl_init('https://oauth2.sky.blackbaud.com/token');
curl_setopt($cURLConnection, CURLOPT_POST, true);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, array(
"Content-type: application/x-www-form-urlencoded",
"Authorization: Basic " . base64_encode($this->client_id . ":" . $this->client_secret)
));
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);

$apiResponse = curl_exec($cURLConnection);

curl_close($cURLConnection);

Please help! I really have no idea what is going on.

Thank you.

Comments

  • Daniel Leonard
    Daniel Leonard Blackbaud Employee
    Eighth Anniversary Kudos 2 Name Dropper Participant

    @Ahmed Latif The body of the token request needs to be encoded using application/x-www-form-urlencoded. You could consider using http_build_query function to accomplish that.

    curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, http_build_query($postRequest));

  • @Ahmed Latif

    I've used Laravel to integrate with the Sky API since late 2017. Here is the production code I am using to exchange the authorization code for an access token.

    public static function exchangeCodeForAccessToken($code = 0)
    {

    $body = array(
    'code' => $code,
    'grant_type' => 'authorization_code',
    'redirect_uri' => config('auth_redirect_uri'),
    );
    return self::fetchToken($body);
    }

    private static function fetchToken($body = array())
    {

    $headers = array(
    'Content-type: application/x-www-form-urlencoded',
    'Authorization: Basic ' . base64_encode(config('auth_client_id') . ':' . config('auth_client_secret'))
    );

    $url = config('auth_base_uri') . 'token';

    $response = Http::post($url, $body, $headers);

    $token = json_decode($response, true);

    return $token;
    }


  • @Tim Owensby
    Thanks for your reply, unfortunately, the same result.
    I believe your code works 100% because I wrote almost the same exact one and I was positive it will work before giving up and copying the one from the documentation.

  • @Daniel Leonard
    Thanks, Daniel, still the same issue.
    I also do think it's something with the way the request is sent, maybe the headers or/and the body are not sent as they should be. this authorization process is the most complicated.

  • Daniel Leonard
    Daniel Leonard Blackbaud Employee
    Eighth Anniversary Kudos 2 Name Dropper Participant

    @Ahmed Latif It's possible that the way you are creating the array for $postRequest is not correct

  • @Ahmed Latif

    Change this

    $postRequest = array(
    'code: '.$code,
    'grant_type: authorization_code',
    'redirect_uri: https://nmf.nmf/blackbaud'
    );

    To this

    $postRequest = array(
    'code' => $code,
    ‘grant_type’ => 'authorization_code',
    ‘redirect_uri’ => 'https://nmf.nmf/blackbaud'
    );

    The way you are creating the postRequest array you end up with an array of three strings, not three key => value pairs.

  • @Ahmed Latif

    Here is the solution that actually worked for me:

    $cURLConnection = curl_init();
    curl_setopt_array($cURLConnection, array(
    CURLOPT_URL => $this->token_url,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => "grant_type=authorization_code&redirect_uri=https://domain.com&code=".$code,
    CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded",
    "Authorization: Basic " . base64_encode($this->client_id . ":" . $this->client_secret)
    ),
    ));
    $apiResponse = curl_exec($cURLConnection);
    curl_close($cURLConnection);
    $jsonArrayResponse = json_decode($apiResponse);

    Just needed to focus on how you send the request., this format:

    "grant_type=authorization_code&redirect_uri=https://domain.com&code=".$code,

    Worked with no issues.

Categories