Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Revoke Google Access Token in PHP

As the title reads, I want to revoke a granted access token programatically (in PHP that is). I found this on their website, but can't seem to find a function in the api client library. Is there a clean library function?

EDIT: As pointed out by DaimTo there is a function called revokeToken(). So this code works in PHP (with composer):

require_once "vendor/autoload.php";
$client = new Google_Client();
$client->setApplicationName(GOOGLE_APP_NAME);
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->revokeToken($access_token);
like image 499
Jan Avatar asked Jul 20 '15 11:07

Jan


People also ask

How do I revoke a Google token?

Revocation methods Users log in to their Google Account, find your app in the Third-party apps with account access settings and select Remove Access. Your platform calls google.accounts. id. revoke .

Can we revoke access token?

Once issued, access tokens and ID tokens cannot be revoked in the same way as cookies with session IDs for server-side sessions. As a result, tokens should be issued for relatively short periods, and then refreshed periodically if the user remains active.

How do I revoke OAuth access token?

To revoke a refresh token, send a POST request to https://YOUR_DOMAIN/oauth/revoke . The /oauth/revoke endpoint revokes the entire grant, not just a specific token. Use the /api/v2/device-credentials endpoint to revoke refresh tokens.


2 Answers

try

$client->revokeToken(); 

or

$client->revokeToken($accesstoken);

Information found by digging around the Google-api-php-Client

like image 138
DaImTo Avatar answered Oct 20 '22 17:10

DaImTo


<a href="logout.php">Logout</a>
/** logout file **/
<?php    
require_once __DIR__ . '/vendor/autoload.php';    
session_start();    
$accesstoken=$_SESSION['access_token'];

//Unset token and user data from session    
unset($_SESSION['access_token']);    
unset($_SESSION['userData']);    

//Reset OAuth access token    
$client = new Google_Client();

//$client->revokeToken();    
$client->revokeToken($accesstoken);

//Destroy entire session    
session_destroy();    

//Redirect to homepage    
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/googlelogin/index.php';    
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));    
?>
like image 39
Simple Test Avatar answered Oct 20 '22 17:10

Simple Test