结合使用Google服务帐户和Google API JavaScript客户端 [英] Use Google Service Account with google API JavaScript Client

查看:58
本文介绍了结合使用Google服务帐户和Google API JavaScript客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用服务帐户来从任何Google API请求数据,但要使用以下库: https://github.com/google/google-api-javascript-client

I wanted to know if it was possible possible to use a service account to request data from any google API but using this library: https://github.com/google/google-api-javascript-client

我设法从Google云端控制台中找到如何将OAuth2.0凭证与该库配合使用.但是我的真正需求是我需要使用服务帐户来获取这些数据.

I managed to find out how to use the library with OAuth2.0 credential from google cloud console. But my real need requires me to use a service account to fetch these data.

这是我用来从OAuth2.0凭据中获取数据的代码:

Here's the code I used to fetch data from OAuth2.0 credentials:

initClient() {
      return gapi.client.init({
        apiKey: this.GSC_API_KEY, // already defined in the application
        client_id:
          "xxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
        scope:
          "https://www.googleapis.com/auth/webmasters https://www.googleapis.com/auth/webmasters.readonly",
        discoveryDocs: [
          "https://www.googleapis.com/discovery/v1/apis/webmasters/v3/rest"
        ]
      });
    },

   gapiList() {
      this.initClient()
        .then(() => {
          // Executes an API request, and returns a Promise.
          // The method name `webmasters.sites.list` comes from the API webmasters.
          return gapi.client.webmasters.sites.list();
        })
        .then(
          response => {
            console.log(response.body);
          },
          err => {
            console.error(err.details);
          }
        );
    },

以下是请求API的代码:

Here's the code that request the API:

gapi.load("client", this.gapiList);

它确实返回了我很好的数据.

It does return me good data.

但是我的最终目的是要求我使用服务帐户.

But my final purpose requires me to use a service account.

initClient函数确实需要一个client_id才能正确加载.如果我要提供服务帐户的client_id,它确实会向我返回错误.

The initClient function does need a client_id to load correctly. If I'm giving the client_id of the service account it does return me an error.

"Not a valid origin for the client: http://localhost:8080/ has not been whitelisted for client ID xxxxxxxxxxxxx. Please go to https://console.developers.google.com/ and whitelist this origin for your project's client ID.

该错误消息告诉我将本地主机(我目前正在使用的主机)列入白名单,但是我找不到如何将服务帐户的本地主机列入白名单.

The error message is telling me to whitelist the localhost (where I am currently working) but I don't find how to whitelist localhost for a service account.

希望我提供了足够的信息.

Hope I gave enough informations.

感谢您的回复和帮助.

推荐答案

您不能将服务帐户与Google JavaScript客户端库一起使用.

You can not use service account with Google JavaScript client library.

您应该使用Oauth2身份验证.如果必须使用服务帐户,则需要使用服务器端语言,例如node.js或python等.

You should use to Oauth2 authentication. If you have to use service account you need to use server sided language such like node.js or python etc.

这篇关于结合使用Google服务帐户和Google API JavaScript客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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