APIs & Integrations

Not applicable

Missing or unknown auth code with PHP curl

I am trying to get a refreshtoken from Hubspot but I keep getting "BAD_AUTH_CODE","message":"missing or unknown auth code"
I am using following code:

$this->nonjsondata = array(
'grant_type' => 'authorization_code',
'client_id' => the_id,
'client_secret' => the_secret,
'redirect_uri' => the_url,
'code' => the_code
);
//where the_id, the_secret and the_url are replaced with the correct values and the_code is read from $_GET['code']

$service_url = 'https://api.hubapi.com/oauth/v1/token';
$ch = curl_init($service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded;charset=utf-8'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( $this->nonjsondata ) );
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);

0 Upvotes
3 Replies 3
cbarley
HubSpot Alumni
HubSpot Alumni

Missing or unknown auth code with PHP curl

Hi @Serge, How are you formatting the call? Can I see an example of your call in postman?

Make sure that your redirect_uri matches exactly the way it looked when you initiated the OAuth connection (even a trailing / will make the entire request invalid).

Not applicable

Missing or unknown auth code with PHP curl

Hi @Connor_Barley,
I don't know what changed overnight but this morning the code works fine. Could it be that it takes some time for a testaccount to be authorized? I just created it yesterday.

0 Upvotes
cbarley
HubSpot Alumni
HubSpot Alumni

Missing or unknown auth code with PHP curl

@Serge, interesting. No, I've never seen a delay with a test account being authorized to create apps. Glad you're up and running now though!

0 Upvotes