How to connect to a api that use two ways of authentication?

I have my cake php aplication and I need connect to a api of my bank to access
data and to send data.

this is the first layer of authentication which uses x-www-form-urlencoded with
grant_type -> client_credentials
client_id, client_secret

after that with access token I will can use to access the data from api.

But the problem is: every 4 hours the token expiry. How I can make a connection with form-urlencoded passing the info above in a way that persist and re-activate every 4 hours generating a token that can be accessed everywhere in my application.

Basically, how I can implement it? I searched through some docs but I only found only with JWT. and not with two types of authentication.

Do the form authentication any time you don’t have a token, and cache the resulting token for 3 hours.

1 Like

Thanks for the answer. how you would do that? By php itself, or there’s any specific function in cake that I can use to connect to api in cake.

Oh, I thought you were asking how to handle the two different pieces. There’s nothing for “connecting to an API”, but there is the HTTP library, or you can use cURL, either of which let you create requests to send to other servers.

Thanks again.
Can you tell me about this http library?(any doc or example of the connection through xx-form-urlencoded I have done api connection with node js, but I never did with cake php,
In curl I can do the same thing right?
–>Curl -> formurlencoded -> Save token to session for 3 hours if session token does not exist. then another curl to connect to the server with the access token

The documentation for it is right where one might expect it to be, in the manual.

https://book.cakephp.org/3/en/core-libraries/httpclient.html

And yes, cURL can do the same.

Nice, with the http library it seems easier.

I didnt saw explicity on documentation but if the server return access token
I should get the response header ‘access token’ by doing this exactly ?
$response->getHeader(‘access_token’);

From your screen shot, it seems that the token is returned as part of the JSON data in the response body.

im passing the body as this
‘body’ => [‘grant_type’ => ‘client_credentials’, ‘client_id’ => ‘fasfafs’, ‘client_secret’ => ‘affsaatest’],
but i’m getting error, this is the correct way to pass multiple values into the body on http client post ?