【发布时间】:2019-07-16 00:26:55
【问题描述】:
我已经为此苦苦挣扎了几个小时,如果不是几天的话,而且似乎无法解决它。
我对 Cloud Functions 的请求被拒绝,错误代码为:401: UNAUTHENTICATED。
我的代码如下:
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . FIREBASE_SERIVCE_PATH);
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_CloudFunctions::CLOUD_PLATFORM);
$httpClient = $client->authorize();
$promise = $httpClient->requestAsync("POST", "<MyCloudFunctionExecutionUri>", ['json' => ['data' => []]]);
$promise->then(
function (ResponseInterface $res) {
echo "<pre>";
print_r($res->getStatusCode());
echo "</pre>";
},
function (RequestException $e) {
echo $e->getMessage() . "\n";
echo $e->getRequest()->getMethod();
}
);
$promise->wait();
我目前正在从 localhost 执行此操作,因为我仍处于开发阶段。
我的 FIREBASE_SERIVCE_PATH 常量链接到我的 service_account js
我的云函数 index.js:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
// CORS Express middleware to enable CORS Requests.
const cors = require('cors')({
origin: true,
});
exports.testFunction = functions.https.onCall((data, context) => {
return new Promise((resolve, reject) => {
resolve("Ok:)");
});
});
// [END all]
我的云功能日志:
Function execution took 459 ms, finished with status code: 401
我做错了什么,所以我没有通过身份验证?
PS:当从我的 Flutter 移动应用程序调用我的 testFunction 时,我的 testFunction 可以完美运行,该应用程序使用:https://pub.dartlang.org/packages/cloud_functions
更新:
我已遵循此指南:https://developers.google.com/api-client-library/php/auth/service-accounts,但在“将域范围内的权限委派给服务帐户”部分中,它仅说明如果我的应用程序在 Google Apps 域中运行,但我不会使用 Google Apps 域,并且另外我在本地主机上。
【问题讨论】:
标签: php google-api google-cloud-functions google-api-php-client