Google Drive API oauth2在实时服务器上挂起,并在使用auth_code

时间:2016-12-16 17:46:57

标签: google-drive-api google-oauth2 google-php-sdk

我有一个很大的问题,绝对没有从谷歌或PHP返回错误。

我已经编写了一个google客户端oauth2连接模型,当我在Mac上的开发环境中运行它时,它非常有效(我没有使用简单的本地主机,它是&#39 ; s实际上是我用来尽可能多地镜像生产服务器的流浪汉)但是当我把它推向生产时它并不起作用。

它只是没有返回任何错误。代码到达我拥有来自Google的身份验证代码的部分,它挂在我尝试将其替换为令牌的部分($ client-> authenticate($ auth_code))。

我真的不知道自己做错了什么,因为一切都运行良好,没有错误的开发网站(顺便说一下,它有一个公共链接,我尝试从不同的公共IP地址访问它)。

当我将代码推送到实时服务器时,它就会停止身份验证。我为每个生产服务器设置了正确的oauth凭据,重定向uri是正确的。我根本不知道自己做错了什么......

这是代码的一部分(我再说一遍,它完全适用于具有公共可访问URL的开发服务器):

$this->client = new Google_Client();
$this->credentials_path . '/client_secret.json'
$this->client->setAuthConfigFile($this->credentials_path . '/client_secret.json');
$this->client->setAccessType('offline');
$this->client->setRedirectUri($this->redirect_uri);
$this->client->addScope(Google_Service_Drive::DRIVE_READONLY);
$this->client->authenticate($auth_code); //this is the part where the code hangs on a live server, but works perfectly on dev
$this->token = json_encode($this->client->getAccessToken());
$this->client->setAccessToken($this->token);
$this->writeTokenToFile($this->token);

if ($this->client->isAccessTokenExpired()) {
    $this->client->refreshToken($this->token);
}

$google_drive_service = new Google_Service_Drive($this->client);

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

AFAIK,您需要在请求另一个令牌之前撤消现有令牌。

Offline access中所述,当您将API客户端的访问类型设置为offline时,客户端对象将根据需要刷新访问令牌。

有了这个,你需要撤销给应用程序的访问权限。如上所述,

  

部分删除过程可以包含API请求,以确保删除授予应用程序的权限。

您可以通过致电revokeToken()来尝试revoking a token

$client->revokeToken();

您可能还希望在以下SO帖子中查看给定的解决方案:

希望有所帮助!