GAE - 刷新OAuth2令牌时出错

时间:2014-01-27 12:36:00

标签: google-app-engine google-cloud-datastore google-api-php-client

我在GAE上运行了一个PHP应用程序。它使用Google PHP库(v0.6.7)连接到Cloud Datastore。此连接在大多数情况下都能正常工作,但偶尔会出现以下错误,表示尝试刷新OAuth2令牌时发生错误。

Error refreshing the OAuth2 token, message: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Google Accounts</title><meta http-equiv="content-type" content="text/html; charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=0" />

该消息最后还包括以下内容:

You are not authorized to perform this request 

我在此连接的代码下面附上了。如您所见,我正在使用P12文件以自动连接到数据存储区服务。

 $account = 'xxxxxxx@developer.gserviceaccount.com';

 $key = file_get_contents(dirname(__FILE__).'/../google-api-php-client/privatekey.p12');
    $client = new Google_Client();
    $client->setApplicationName($this->appId);

    $client->setAssertionCredentials(
            new Google_AssertionCredentials(
                    $account,
                    array('https://www.googleapis.com/auth/userinfo.email',
                            'https://www.googleapis.com/auth/datastore'),
                    $key)
    );


    $datastore = new Google_DatastoreService($client);

问题是我不确定它是否是GAE问题,或者我是否可以在我的代码中执行某些操作来解决此问题,或者至少尝试减少返回此错误的请求数。

我看到有一个新的库版本(1.0.1-beta),我们正计划迁移到这个版本。但是,在这样做之前,了解可能导致此问题的原因将非常有用。

1 个答案:

答案 0 :(得分:0)

我回答自己,以防万一有人发现它有用。

执行一些测试后,我意识到连接后返回的令牌没有正确存储。结果,系统试图在每个请求中刷新令牌。显然,在给定数量的请求之后,Google只会拒绝为该用户刷新令牌的任何新尝试,从而导致上面显示的错误。

相关问题