简体   繁体   English

Google AppEngine + PHP + Google Cloud数据存储:接收未经授权

[英]Google AppEngine + PHP + Google Cloud Datastore: receiving Unauthorized

I have a PHP application running at Google App Engine and I want to use the App Engine's datastore in it. 我有一个在Google App Engine上运行的PHP应用程序,我想在其中使用App Engine的数据存储区。

I am using the google-api-php-client, and at the Google Cloud Console I've enabled the Google Cloud Datastore API, and registered a new app, downloading the private certificate. 我正在使用google-api-php-client,在Google Cloud Console上我启用了Google Cloud Datastore API,并注册了一个新应用,下载了私有证书。

For the authentication, I'm using the following code ( xxxx has the real values): 对于身份验证,我使用以下代码( 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;

Whichever operation I use in $datasets , like $datasets->lookup , I receive an exception stating that the operation is Unauthorized: 无论我在$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.'

What might be wrong? 可能有什么问题?

I believe you are hitting the following limitation: service account authorization doesn't work with domain-restricted App Engine application . 我相信您遇到以下限制: 服务帐户授权不适用于受域限制的App Engine应用程序

So you can either comment on the issue to get your application whitelisted, keep in mind that it can allow account outside of your domain to auth with your application using OAuth depending on how your application is built. 因此,您可以对问题进行评论,以便将您的应用程序列入白名单,请注意,根据您的应用程序的构建方式,可以允许您的域外帐户使用OAuth对您的应用程序进行身份验证。

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

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