BR
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-url encoded
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.
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-url
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.
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
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);
That should return the token - to use the token will require adding the Authorization Header as described above.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Julian
You are welcome.
The request must include in the header
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)
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
Open in new window
You should end up with something like this
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)
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.