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.
LVL 1
BRDigital MarketingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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.
BRDigital MarketingAuthor Commented:
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

Julian HansenCommented:
That should return the token - to use the token will require adding the Authorization Header as described above.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

BRDigital MarketingAuthor Commented:
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
Julian HansenCommented:
I am assuming you are changing the URL in your code to match the actual service?

You should be able to paste the URL directly into the browser - if that does not work then the problem is with the URL.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BRDigital MarketingAuthor Commented:
Thank you Julian
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.