【问题标题】:Google AppEngine + PHP + Google Cloud Datastore: receiving UnauthorizedGoogle AppEngine + PHP + Google Cloud Datastore:接收未经授权
【发布时间】:2013-10-22 15:44:43
【问题描述】:

我有一个在 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.'

可能出了什么问题?

【问题讨论】:

  • 你的项目ID是什么? XXX的值是多少?
  • 项目id是“flytour-intranet”,SERVICE_ACCOUNT_NAME是317403747968-mciqlisucn3o49ov1qfpk99rjievkld2@developer.gserviceaccount.com。

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


【解决方案1】:

我相信您遇到了以下限制:service account authorization doesn't work with domain-restricted App Engine application

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

【讨论】:

  • 首先我将尝试使用 Identity API。但是当我尝试它时,我收到:“未捕获的异常 'Google_ServiceException' 并带有消息'调用 POST googleapis.com/datastore/v1beta1/datasets/flytour-intranet/… 时出错:(403) 已超过未经身份验证使用的每日限制。继续使用需要注册。”。我已将应用程序帐户名称“flytour-intranet@appspot.gserviceaccount.com”添加为 Cloud Console 项目的成员。
  • @dudinha-dedalus 您应该确保为您的项目启用了 Google Cloud Datastore API,请参阅 developers.google.com/datastore/docs/…
  • 是的,它被激活了......我什至创建了一个新应用程序,激活了“Google Cloud Datastore API”,将其停用并重新激活它,它仍然无法正常工作。
  • 我不知道这是否相关,AppIdentityService::getAccessToken$client-&gt;setScopes 我正在使用 https://www.googleapis.com/auth/datastorehttps://www.googleapis.com/auth/userinfo.email
  • @dudinha-dedalus 因为您是从应用程序本身查询 Google Cloud Datastore,所以您必须确保在与此应用程序(而不是其他项目)关联的 Cloud Project 上启用了 Google Cloud Datastore。您可以在以下页面中找到该项目的链接:appengine.google.com/settings?&app_id=s~flytour-intranet
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2019-08-07
相关资源
最近更新 更多