用PHP撤销Google Access令牌

时间:2015-07-20 11:23:23

标签: php oauth google-api google-oauth google-api-php-client

正如标题所示,我想以编程方式撤销授予的访问令牌(在PHP中)。我在their website上找到了这个,但似乎无法在api client library中找到一个函数。有一个干净的库函数吗?

修改 正如DaimTo所指出的,有一个名为revokeToken()的函数。所以这段代码适用于PHP(带作曲家):

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);

2 个答案:

答案 0 :(得分:5)

尝试

$client->revokeToken(); 

$client->revokeToken($accesstoken);

通过挖掘Google-api-php-Client

找到的信息

答案 1 :(得分:1)

<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));    
?>