Reputation: 87
I want to get an access token. I used the following code which is the same as in the documentation of the latest version of FreshBooks but it gives me an error.
if(!empty($_GET['code']) && isset($_GET['code']))
{
$code=$_GET['code'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.freshbooks.com/auth/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array(
"grant_type" => "authorization_code",
"client_secret" => "xxxxxxxx",
"code" => $_GET['code'],
"client_id" => " xxxxxxx",
"redirect_uri" => "https://localhost/test2.php"
),
CURLOPT_HTTPHEADER => array(
"api-version: alpha",
"cache-control: no-cache",
"content-type: application/json",
"postman-token: 471a0741-8466-2e3f-0006-8b9c3794ef9d"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
It gives me the following response:
400 error invalid request param.
Any solutions?
Upvotes: 0
Views: 564
Reputation: 1
I had the same problem and I have resolved it after escaping the JSON string.
Try This
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.freshbooks.com/auth/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n \"grant_type\": \"authorization_code\",\n \"client_secret\": \"Your client secret\",\n \"code\": \"your code\",\n \"client_id\": \"your client id\",\n \"redirect_uri\": \"https://www.yoururl.com/redirect\"\n}",
CURLOPT_HTTPHEADER => array(
"api-version: alpha",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Upvotes: 0
Reputation: 7
I think you have generated this php snippet using postman ,you can refer my code
public function fobOath($code)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.freshbooks.com/auth/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=authorization_code&client_secret=<your_client_secret>&code=<your_code>&client_id=<your_client_id>&redirect_uri=<your_callback_address>",
CURLOPT_HTTPHEADER => array(
"api-version: alpha",
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
Upvotes: 0