Google PHP Api客户端-我不断收到错误401:未经授权 [英] Google PHP Api Client - I keep getting Error 401: UNAUTHENTICATED

查看:170
本文介绍了Google PHP Api客户端-我不断收到错误401:未经授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为这个问题苦苦挣扎了好几个小时了,即使不是几天,似乎也无法解决.

I've been struggling with this for hours now, if not days and can't seem to fix it.

我对云功能的请求被拒绝,错误代码:401:未授权.

My Requests to Cloud Functions are being denied with error code: 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执行此操作.

I'm currently executing this from localhost as I'm still in development phase.

我的FIREBASE_SERIVCE_PATH常量链接到我的service_account js

My FIREBASE_SERIVCE_PATH constant links to my service_account js

我的云功能index.js:

My Cloud Function 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

我做错了什么,所以我未经身份验证?

What am I doing wrong so I get Unauthenticated?

PS:当我的Flutter移动应用程序使用以下命令时,我的testFunction可以完美工作: https://pub .dartlang.org/packages/cloud_functions

PS: My testFunction works perfectly when invoked from my Flutter mobile app who uses: https://pub.dartlang.org/packages/cloud_functions

更新:

我已遵循此指南: https://developers .google.com/api-client-library/php/auth/service-accounts ,但在将域范围的权限委派给服务帐户"部分中,仅说明我的应用程序是否在Google Apps域中运行,但是我不会使用Google Apps域,而且我在本地主机上.

I have followed this guide: https://developers.google.com/api-client-library/php/auth/service-accounts but in the "Delegating domain-wide authority to the service account" section, it only states If my application runs in a Google Apps domain, however I wont using Google Apps domain, and plus I'm on localhost.

推荐答案

可调用函数施加了 protocol .通常,您使用Firebase客户端SDK调用它们.由于您没有可用于实现该协议的SDK,因此您必须自己遵循该协议.您不能像普通的HTTP函数那样仅仅调用它们.

Callable functions impose a protocol on top of regular HTTP functions. Normally you invoke them using the Firebase client SDK. Since you don't have an SDK to work with that implements the protocol, you'll have to follow it yourself. You can't just invoke them like a normal HTTP function.

如果您不想实施协议,则应该而是使用常规的HTTP函数,并停止在移动应用中使用客户端SDK.

If you don't want to implement the protocol, you should instead use a regular HTTP function, and stop using the client SDK in your mobile app.

这篇关于Google PHP Api客户端-我不断收到错误401:未经授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆