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
-
@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));
0 -
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;
}0 -
@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.0 -
@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.0 -
@Ahmed Latif It's possible that the way you are creating the array for $postRequest is not correct
0 -
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.
0 -
@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.
1
Categories
- All Categories
- 6 Blackbaud Community Help
- 213 bbcon®
- 1.4K Blackbaud Altru®
- 403 Blackbaud Award Management™ and Blackbaud Stewardship Management™
- 1.1K Blackbaud CRM™ and Blackbaud Internet Solutions™
- 15 donorCentrics®
- 360 Blackbaud eTapestry®
- 2.6K Blackbaud Financial Edge NXT®
- 656 Blackbaud Grantmaking™
- 577 Blackbaud Education Management Solutions for Higher Education
- 3.2K Blackbaud Education Management Solutions for K-12 Schools
- 940 Blackbaud Luminate Online® and Blackbaud TeamRaiser®
- 84 JustGiving® from Blackbaud®
- 6.7K Blackbaud Raiser's Edge NXT®
- 3.7K SKY Developer
- 249 ResearchPoint™
- 119 Blackbaud Tuition Management™
- 165 Organizational Best Practices
- 241 Member Lounge (Just for Fun)
- 34 Blackbaud Community Challenges
- 37 PowerUp Challenges
- 3 (Open) PowerUp Challenge: Grid View Batch
- 3 (Closed) PowerUp Challenge: Chat for Blackbaud AI
- 3 (Closed) PowerUp Challenge: Data Health
- 3 (Closed) Raiser's Edge NXT PowerUp Challenge: Product Update Briefing
- 3 (Closed) Raiser's Edge NXT PowerUp Challenge: Standard Reports+
- 3 (Closed) Raiser's Edge NXT PowerUp Challenge: Email Marketing
- 3 (Closed) Raiser's Edge NXT PowerUp Challenge: Gift Management
- 4 (Closed) Raiser's Edge NXT PowerUp Challenge: Event Management
- 3 (Closed) Raiser's Edge NXT PowerUp Challenge: Home Page
- 4 (Closed) Raiser's Edge NXT PowerUp Challenge: Standard Reports
- 4 (Closed) Raiser's Edge NXT PowerUp Challenge: Query
- 796 Community News
- 3K Jobs Board
- 54 Blackbaud SKY® Reporting Announcements
- 47 Blackbaud CRM Higher Ed Product Advisory Group (HE PAG)
- 19 Blackbaud CRM Product Advisory Group (BBCRM PAG)

