Using PHP cURL to POST data on /oauth2/access_token and GET data as jSON
Solution 1
To perform a POST request in PHP with cURL, you can do something like:
$handle = curl_init('https://partner.path.com/oauth2/access_token');
$data = array('grant_type' => 'authorization_code', 'client_id' => 'CLIENT', 'client_secret' => 'SECRET', 'code' => 'CODE');
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$resp = curl_exec($handle);
You can then use json_decode($json_encoded)
to get an associative array from the server response.
Solution 2
addition to Fox Wilson answer:
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
Solution 3
Not sure if you have figured this out yet or not since i see it was from awhile ago, but I just had this problem and this is how I figured it out.
$code = $_GET['code'];
$url = 'https://YourPath/token?response_type=token&client_id='.$client_id.'&client_secret='.$client_secret.'&grant_type=authorization_code&code='.$code.'&redirect_uri='.$redirect_uri;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST,true);
$exec = curl_exec($ch);
$info = curl_getinfo($ch);
print_r($info);
curl_close($ch);
$json = json_decode($exec);
if (isset($json->refresh_token)){
global $refreshToken;
$refreshToken = $json->refresh_token;
}
$accessToken = $json->access_token;
$token_type = $json->token_type;
print_r($json->access_token);
print_r($json->refresh_token);
print_r($json->token_type);
Hope that helps
Jhonny Jr.
Updated on June 04, 2022Comments
-
Jhonny Jr. almost 2 years
I has follow step on Path API about how to Authentication User. In the tutorial auth process, user is begin to redirect to the following URL and prompt to grant access:
https://partner.path.com/oauth2/authenticate?response_type=code&client_id=THE_CLIENT_ID
And after that, server will give response as authorization code via URL Address (i have complete this step and got the code).
As from docs explain, Code is should be exchanged for an access token using /oauth2/access_token as long with Client ID and Client Secret (get access_token)
But i don't have any clue how to POST data via cURL to the server, i has try so many curl_setopt() option and combination, but it still give me a nothing.
From the Docs, Request is look like this:
POST /oauth2/access_token HTTP/1.1 Host: partner.path.com Content-Type: application/x-www-form-urlencoded Content-Length: <LENGTH> grant_type=authorization_code&client_id=CLIENT&client_secret=SECRET&code=CODE
And cURL format like this:
curl -X POST \ -F 'grant_type=authorization_code' \ -F 'client_id=CLIENT_ID' \ -F 'client_secret=CLIENT_SECRET' \ -F 'code=CODE' \ https://partner.path.com/oauth2/access_token
And server will give response like this:
HTTP/1.1 201 CREATED Content-Type: application/json Content-Length: <LENGTH> { "code": 201, "type": "CREATED" "reason": "Created", "access_token": <ACCESS_TOKEN>, "user_id": <USER_ID>, }
-
Jhonny Jr. almost 9 yearsI'm already using that way and reTest again, but it still not work for me :( still give me a blank pages although i'm already using echo/print_r/var_dump from $resp variable
-
tew almost 9 yearsIs it just not working, or do you get any error messages? Do you get any response from the server at all?
-
Jhonny Jr. almost 9 yearsNo, i'm not get any response from server, it just not working :( but when i test using Postman it work fine
-
Hans Z. almost 9 yearsyou have to use
http_build_query($data)
otherwise thecontent-type
won't be set tox-www-form-urlencoded
-
Otieno Rowland over 6 yearsYou should add this as a comment to the alleged answer by @Fox Wilson
-
AlexanderPop over 6 yearsI was going to, but got "You must have 50 reputation to comment"