【问题标题】:Revoking Access Google API PHP撤销访问 Google API PHP
【发布时间】:2015-02-17 02:18:32
【问题描述】:

我正在尝试撤消网络应用程序的访问权限。这是我的代码:

当用户登录时:

$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;
}

这是我想撤销应用程序时的代码:

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

结果是一个 NOT FOUND 页面,上面写着 The requested URL /v2/{ "error" : "invalid_token"} was not found on this server.

我不确定这是否是撤销访问权限的正确方法。 谢谢。

【问题讨论】:

    标签: php oauth-2.0 google-api


    【解决方案1】:

    我认为这应该可行..

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

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

    【讨论】:

    • 谢谢,但答案不是删除我的 Google 帐户中的权限。我需要知道如何从 Google 的应用程序中删除权限,就像 Facebook 中的 $ret = $facebook->api('/me/permissions', 'DELETE');
    • @Crisiiii 你找到问题的答案了吗?
    【解决方案2】:

    我尝试了您的代码并遇到了同样的错误。 看看你是如何连接字符串的:

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

    PHP 很容易允许在连接的字符串上提交语法错误。对我有用的修复是:

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

    如果你需要的话,我的完整代码是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 2017-06-20
      • 2020-09-29
      相关资源
      最近更新 更多