我正在尝试使用以下代码连接到Google Cloud Datastore。我尝试遵循these quidelines和these。我正在使用Composer,我的代码在Wordpress插件中。我已将凭据文件与脚本放在相同的文件夹中,并且可以找到它。
<?php # -*- coding: utf-8 -*-
/*
* Plugin Name: Cloud Datastore Connection
*/
add_shortcode( 'cd_connect', 'cloud_datastore_connection' );
use Google\Cloud\Core\ServiceBuilder;
use Google\Cloud\Datastore\DatastoreClient;
function cloud_datastore_connection( $attributes )
{
// For Composer
require 'vendor/autoload.php';
// Authenticate using keyfile data
$cloud = new ServiceBuilder([
'keyFile' => json_decode(file_get_contents('/www/.../XXX.json'), true)
]);
$datastore = new DatastoreClient(['projectId' => 'ultunaphotons']);
// Create an entity
$bob = $datastore->entity('Person');
$bob['firstName'] = 'Bob';
$bob['email'] = 'bob@example.com';
$datastore->insert($bob);
// Update the entity
$bob['email'] = 'bobV2@example.com';
$datastore->update($bob);
// testing
print bob['email'];
}
?>
我收到此错误消息:
致命错误:未捕获的Google \ Cloud \ Core \ Exception \ ServiceException:{ “错误”:{“代码”:401,“消息”:“缺少请求 身份验证凭证。预期的OAuth 2访问令牌,登录cookie 或其他有效的身份验证凭据。看到 https://developers.google.com/identity/sign-in/web/devconsole-project。”, “ status”:“ UNAUTHENTICATED”}} in /www/.../vendor/google/cloud-core/src/RequestWrapper.php:362 堆栈跟踪:#0 /www/.../vendor/google/cloud-core/src/RequestWrapper.php(206):Google \ Cloud \ Core \ RequestWrapper-> convertToGoogleException(Object(GuzzleHttp \ Exception \ ClientException))#1 / www /。 ../vendor/google/cloud-core/src/RestTrait.php(95): Google \ Cloud \ Core \ RequestWrapper-> send(Object(GuzzleHttp \ Psr7 \ Request), 氩气中 /www/.../vendor/google/cloud-core/src/RequestWrapper.php 在第362行
有什么想法我在做什么错吗?