2

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>,
}

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I'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
Is it just not working, or do you get any error messages? Do you get any response from the server at all?
No, i'm not get any response from server, it just not working :( but when i test using Postman it work fine
you have to use http_build_query($data) otherwise the content-type won't be set to x-www-form-urlencoded
1

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

Comments

1

addition to Fox Wilson answer:

curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);

2 Comments

You should add this as a comment to the alleged answer by @Fox Wilson
I was going to, but got "You must have 50 reputation to comment"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.