Php 连接到云数据存储时出现致命错误代码401
我正在尝试使用下面的代码连接到Google云数据存储。我也试着跟随。我正在使用Composer,我的代码在Wordpress插件中。我已将我的凭据文件与脚本放在同一文件夹中,找到了它Php 连接到云数据存储时出现致命错误代码401,php,authentication,google-cloud-platform,google-cloud-datastore,credentials,Php,Authentication,Google Cloud Platform,Google Cloud Datastore,Credentials,我正在尝试使用下面的代码连接到Google云数据存储。我也试着跟随。我正在使用Composer,我的代码在Wordpress插件中。我已将我的凭据文件与脚本放在同一文件夹中,找到了它 <?php # -*- coding: utf-8 -*- /* * Plugin Name: Cloud Datastore Connection */ add_shortcode( 'cd_connect', 'cloud_datastore_connection' ); use Google\Clo
<?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
或其他有效的身份验证凭据。请参阅
.",
“状态”:“未经身份验证”}
/www/../vendor/google/cloud-core/src/RequestWrapper.php:362
堆栈跟踪:#0
/www/../vendor/google/cloud-core/src/RequestWrapper.php(206):google\cloud\core\RequestWrapper->converttogologleexception(Object(GuzzleHttp\Exception\ClientException))\1/www/../vendor/google/cloud-core/src/restrait.php(95):
Google\Cloud\Core\RequestWrapper->send(对象(GuzzleHttp\Psr7\Request),
Ar in
/www/../vendor/google/cloud-core/src/RequestWrapper.php
在线362
知道我做错了什么吗