OAuth Error response: grant_type "missing or unknown grant type"

contacts

#1

Hi, I am using a developer account, Test App and Test Portal to go through the API tutorial. I have not been able to get past the Oauth authentication error:
{“status”:“BAD_GRANT_TYPE”,“message”:“missing or unknown grant type”,“correlationId”:“YYYYY”,“requestId”:“XXXX”}"

Here is the url:

https://app.hubspot.com/oauth/authorize?client_id=GGGG&scope=contacts&redirect_uri=https://myliveurl.com/hubspot-auth.php

I am using PHP and cURL

$appclient_token = $_GET[‘code’];

	$service_url = 'https://api.hubapi.com/oauth/v1/token';
	$curl = curl_init($service_url);
	$curl_post_data = array(
	                'grant_type' => 'authorization_code', 
			'client_id' => 'GGGG',
			'client_secret' => 'IIII',
			'redirect_uri' => 'https://myliveurl.com/hubspot-auth.php/&code=' . $appclient_token
	);
	
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_POST, true);
	curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded;charset=utf-8'));
	curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
	$curl_response = curl_exec($curl);
             curl_close($curl);
            var_dump($curl_response):

the app has no scope (i’ve tried with and without contacts scope) and the test portal is also default.

any help appreciated. thanks!


#2

Hi @mc2prod

This is likely an issue with how curl is formatting the data in the actual POST. The fields you have look ok, but the grant_type is the first thing we check for, so if there’s a general problem with the data you’d get that error by default.

You may need to convert the array into a URL encoded string before passing it directly to curl:


#3

Thank you for your response. I did modify it to encode.

  curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($curl_post_data));

However now I am getting this response back BAD_AUTH_CODE.

Is there not a list of error messages and how to resolve them? Googling them does not pull up anything.

BTW, I am following along with the Hubspot process to a tee using exact url’s and only substituting what is required to use my developer account and trial account info.


#4

That error is for the code= field. You’ll want that to be a separate item in your $curl_post_data array before it gets encoded, at the moment you have it as part of the redirect_uri field.


#5

I thought I understood what you meant but what I thought I understood is not working.

Are you recommending that I take this:

https://myliveurl.com/hubs-refresh.php/&code=’ . $appclient_token;

and turn it into this:

https://myliveurl.com/hubs-refresh.php/&code=BBFSDFUIUIUWTEPMCDVN

before the array?

what if I just put it into a variable and put that variable into the array? I am somewhat confused.

Thank you in advance for helping me understand.


#6

@mc2prod sorry, that’s actually what I meant. code should be it’s own variable in the array, should have been more clear there:

$curl_post_data = array(
    'grant_type' => 'authorization_code', 
    'client_id' => 'GGGG',
    'client_secret' => 'IIII',
    'redirect_uri' => 'https://myliveurl.com/hubspot-auth.php/
    'code' => $appclient_token
);

#7

Ok, hurdle succeded: Now here is the another error message MISMATCH_REDIRECT_URI_AUTH_CODE, redirect URI does not match initial auth code URI

Here is the original auth code take from the documentation and modified only where dev inputs required

https://app.hubspot.com/oauth/authorize?client_id=GGGG&scope=contacts%20automation&redirect_uri=https://myliveurl.com/hubs-refresh.php


#8

The redirect_uri in the request to get the access token needs to be an exact match to the URI in the original authorization URL, so if you’re using

&redirect_uri=https://myliveurl.com/hubs-refresh.php

in the authorize URL you’d need

‘redirect_uri’ => ‘https://myliveurl.com/hubs-refresh.php

in $curl_post_data


#9

The only difference in my url’s is a forward slash and the end in ‘redirect_uri’ => 'https://myliveurl.com/hubspot-auth.php/

So I removed the forward slash and now it works. For future reference I am going to post this here, hopefully to help someone in the future. Thank you for your help. I am sure I’ll be back.

Auth Request
https://app.hubspot.com/oauth/authorize?client_id=AAAA&scope=contacts%20automation&redirect_uri=https://myliveurl.com/hubs-refresh.php

Token request

$appcode = $_GET['code'];

	//next example will insert new conversation
	$service_url = 'https://api.hubapi.com/oauth/v1/token';
	$curl = curl_init($service_url);
	$curl_post_data = array(
	        'grant_type' => 'authorization_code', 
			'client_id' => 'AAAA',
			'client_secret' => 'BBBB',
			'redirect_uri' => 'https://myliveurl.com/hubs-refresh.php',
			'code' => $appcode
	);
	
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_POST, true);
	curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded;charset=utf-8'));
	curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($curl_post_data));
	$curl_response = curl_exec($curl);

#10

Can either of you help me understand where to get the $appcode? Is this the API or the App ID?


#11

$appcode is just the variable holding the data that is returned from the Auth Request.