Google AppEngine + PHP + Google Cloud Datastore:接收未经授权的数据


Google AppEngine + PHP + Google Cloud Datastore: receiving Unauthorized

我在Google App Engine上运行了一个PHP应用程序,我想在其中使用App Engine的数据存储。

我正在使用google-api-php-client,在Google Cloud Console上,我启用了Google Cloud Datastore API,并注册了一个新的应用程序,下载了私有证书。

对于身份验证,我使用以下代码(xxxx具有实际值):

const SERVICE_ACCOUNT_NAME = 'xxxx@developer.gserviceaccount.com';
const KEY_FILE = 'secure/privatekey.p12';
$client = new Google_Client();
$key = file_get_contents ( KEY_FILE );
$client->setAssertionCredentials (
    new Google_AssertionCredentials ( 
        SERVICE_ACCOUNT_NAME, 
        array (
            'https://www.googleapis.com/auth/userinfo.email',
            'https://www.googleapis.com/auth/datastore' 
        ), 
        $key 
    ) 
);
$service = new Google_DatastoreService ( $client );
$datasets = $service->datasets;

无论我在$datasets中使用哪种操作,例如$datasets->lookup,我都会收到一个异常,指出该操作未经授权:

Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/datastore/v1beta1/datasets/<my-appengine-id>/lookup: (403) Unauthorized.'

可能出了什么问题?

我相信

您遇到了以下限制:服务帐户授权不适用于受域限制的 App Engine 应用程序。

因此,您可以对问题发表评论以将您的应用程序列入白名单,请记住,它可以允许域外的帐户使用 OAuth 对您的应用程序进行身份验证,具体取决于应用程序的构建方式。