简体   繁体   English

撤消访问Google API PHP

[英]Revoking Access Google API PHP

I'm trying to revoke the access from a web app. 我正在尝试撤消来自网络应用的访问权限。 This is my code: 这是我的代码:

When the user do login: 当用户登录时:

$scriptUri = "http:...";

$client = new Google_Client();

$client->setAccessType('online');
$client->setApplicationName('xxx');
$client->setClientId('xxx');
$client->setClientSecret('xxx');
$client->setRedirectUri($scriptUri);
$client->setDeveloperKey('xxx'); // API key
$client->setScopes(array('https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));

$oauth2 = new Google_Service_Oauth2($client);

if (isset($_GET['code']) && isset($_GET["google"])){
    $client->authenticate($_GET['code']);
    $token = $client->getAccessToken();
    $client->setAccessToken($token);
    $_SESSION['google_token'] = $token;
}

And here is the code when I want to revoke the app: 以下是我想撤销应用时的代码:

$ch = curl_init("https://accounts.google.com/o/oauth2/revoke?token=".$_SESSION['google_token'].";");
curl_exec($ch);
curl_close($ch)

The result is a NOT FOUND page saying The requested URL /v2/{ "error" : "invalid_token"} was not found on this server. 结果是一个NOT FOUND页面说明The requested URL /v2/{ "error" : "invalid_token"} was not found on this server.

I'm not sure if this is the correct way to revoke the access. 我不确定这是否是撤销访问权限的正确方法。 Thanks. 谢谢。

I tried your code and had the same error. 我尝试了你的代码并遇到了同样的错误。 Take a look at how you have concatenated the strings at: 看看你如何连接字符串:

$ch = curl_init("https://accounts.google.com/o/oauth2/revoke?token=".$_SESSION['google_token'].";");

PHP easily lets committing syntax errors over concatenated strings. PHP轻松地允许在连接字符串上提交语法错误。 The fixed that worked for me was: 对我有用的固定是:

$RevokeTokenURL="https://accounts.google.com/o/oauth2/revoke?token=".$_SESSION['google_token'];
$ch = curl_init($RevokeTokenURL);

And in case you need it, my complete code is: 如果您需要它,我的完整代码是:

  if(isset($_GET['action']) && $_GET['action'] == 'logout') {
      session_destroy();
      header('Location:'.$RedirectURL);
      $RevokeTokenURL="https://accounts.google.com/o/oauth2/revoke?token=".$_SESSION['google_token'];
      $ch = curl_init($RevokeTokenURL);
      curl_exec($ch);
      curl_close($ch); 
    }

I think this should work.. 我认为这应该有效..

$revokeURL = "https://accounts.google.com/o/oauth2/revoke?token=".$access_token;

$ch = curl_init();
$options = array(
CURLOPT_URL => $revokeURL,
CURLOPT_HEADER  =>  true, 
CURLOPT_RETURNTRANSFER  =>  true,
CURLOPT_SSL_VERIFYPEER => true, //verify HTTPS
CURLOPT_SSL_CIPHER_LIST => 'TLSv1'); //remove this line if curl SSL error  

curl_setopt_array($ch, $options); //setup

$response = curl_exec($ch); //run
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get HTTP code

if ($httpCode == 200)
{
echo "Success"; // .$response;
} 
else 
{
echo "Error : ".$httpCode."__".curl_error($ch);    
}
curl_close($ch);``` 

Based on https://developers.google.com/accounts/docs/OAuth2WebServer#tokenrevoke 基于https://developers.google.com/accounts/docs/OAuth2WebServer#tokenrevoke

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM