PHP-OAuth2 icon indicating copy to clipboard operation
PHP-OAuth2 copied to clipboard

How to use the refresh token

Open msyadav88 opened this issue 9 years ago • 4 comments

I have made the setup for php-oauth2 with wordpress. But it get expires after some days. I want to stay it permanent or use the refresh token to renew the token automatically using php code. Please can you help me.

msyadav88 avatar Oct 08 '15 18:10 msyadav88

Please can anyone help me.

msyadav88 avatar Oct 10 '15 17:10 msyadav88

I know this is SUPER late, but just in case you still need it, or someone else comes along:

'refresh_toke' grant type is done via the same getAccessToken function, you just pass 'refresh_token' and the access token URL to it.

Code Sample:

require("Client.php");
require("GrantType/IGrantType.php");
require("GrantType/AuthorizationCode.php");
require("GrantType/RefreshToken.php");

$authorizeUrl = '';
$accessTokenUrl = '';
$clientId = '';
$clientSecret = '';

if (isset($_SESSION['access_token']))
{
$LoggedIn = TRUE; 
$params = array("refresh_token" => $_SESSION['refresh_token']);

$client = new OAuth2\Client($clientId, $clientSecret, OAuth2\Client::AUTH_TYPE_AUTHORIZATION_BASIC);
$client->setCurlOption(CURLOPT_USERAGENT,$userAgent);
$response = $client->getAccessToken($accessTokenUrl, 'refresh_token', $params);

print_r($response);

$client->setAccessToken($response["result"]["access_token"]);
$_SESSION['access_token'] = $response["result"]["access_token"];
$client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_BEARER);
}

D0cR3d avatar Jun 06 '16 14:06 D0cR3d

What code would the RefreshToken.php contain in this example?

produktive avatar Feb 21 '17 02:02 produktive

I know this is SUPER late, but just in case you still need it, or someone else comes along:

'refresh_toke' grant type is done via the same getAccessToken function, you just pass 'refresh_token' and the access token URL to it.

More than 6 years after this post, I would like to say a big thank you for this. I think I would not have made my authentification script working without this help. Hope the original poster will see this message :)

edel79 avatar Oct 03 '22 17:10 edel79