Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

how to get the token from an API?

Dear Experts,
I use PHP, I need to get a token from a link using below information.
How can I do that? Thank you

Method : GET
Authorization  Type : oauth2
Grant Type : password
Content-Type : application/x-www-form-urlencoded
RequestUrl :xxxxxxxxxxx


Service Input Parameters:

username as string
password as string
client_Id as string

Service Return Parameters:

access_token as string
token_type as string
expires_in as string

thank you in advance.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

What this is saying is you need to make a GET request to the Request URL
The request must include in the header

Content-Type : application/x-www-form-urlencoded

Open in new window


The URL must also include URL encoded parameters for username / password and client_id

In other words (assume username = MickeyMouse, password = letmein and ClientID = 00001)
https://www.api-url-here.com/auth-function-here?grant_type=password&username=MickeyMouse&password=letmein&client_id=00001

Open in new window


This will return some text. You have not given us enough information to say what the return format is - I am assuming JSON (could be XML) - but you will need to parse the results.

For instance - if the return is JSON then you would do this

$data = json_decode($return);

Open in new window


You should end up with something like this
array(
  "access_token" => "abc123",
  "token_type" => "bearer",
  "expires_in" => "3600"
);

Open in new window


For all subsequent calls you would need to include the access_token (in this case abc123) in all future requests to the API. This is done using the Authorization header.

In your request add the following header
(Assuming that abc123 is the token)
Authorization: Bearer abc123

Open in new window


You would then pass any service specific data either as URL encoded parameters on the request URI or in the body depending on the service type.
Avatar of BR

ASKER

Dear Julian Hansen,
my return format is as you said is Json.

thank you so much for the info.

would it be correct to write it like this? thank you

//Initialize cURL.
$ch = curl_init();
 
//Set the URL that you want to GET by using the CURLOPT_URL option.
curl_setopt($ch, CURLOPT_URL, 'https://www.api-url-here.com/auth-function-here?grant_type=password&username=MickeyMouse&password=letmein&client_id=00001');

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
 
//Set CURLOPT_RETURNTRANSFER so that the content is returned as a variable.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
//Set CURLOPT_FOLLOWLOCATION to true to follow redirects.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 
//Execute the request.
$return = curl_exec($ch);
 
//Close the cURL handle.
curl_close($ch);
 
//Print the data out onto the page.
$data = json_decode($return);

var_dump ($data);

Open in new window

That should return the token - to use the token will require adding the Authorization Header as described above.
Avatar of BR

ASKER

Dear Julian,
thank you very much.

it gives Request Timeout error.
is something wrong with my code?

as soon as I get the Authorization code, I will add it to the header.

thank you
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

Thank you Julian
You are welcome.