通过Node.js的OAuth2对BigQuery REST API进行身份验证

时间:2022-09-26 15:33:03

Newbie trying to figure out how to get a Node.js application to authenticate and query Google BigQuery, trying to adapt this CodeLab tutorial from Java. What step might i be missing?

新手想知道如何获得一个节点。使用js应用程序对谷歌BigQuery进行身份验证和查询,并尝试使用Java编写的CodeLab教程。我可能错过了什么步骤?

First I create this Oauth2 URL using my clientid:

首先,我使用我的clientid创建Oauth2 URL:

https://accounts.google.com/o/oauth2/auth?
    client_id=1047877053699-den6kbs4v3f2bft6clonsirkj1pc7t6j.apps.googleusercontent.com
    &scope=https://www.googleapis.com/auth/bigquery
    &redirect_uri=http://localhost:3000/oauth2callback
    &access_type=offline
    &response_type=code

This successfully reaches Google, which prompts

它成功到达谷歌,提示

A third party service is requesting permission to access your Google Account.

第三方服务正在请求访问您的谷歌帐户的权限。

Agreeing that generates a second prompt:

同意产生第二个提示:

Nodejs_Test is requesting permission to: View and manage your data in Google BigQuery

Nodejs_Test请求允许:在谷歌BigQuery中查看和管理数据

Agreeing to that, the callback URL is called, with a parameter accessToken.

同意这一点,调用回调URL,并带有一个参数accessToken。

I think the following url should list tables in my BigQuery project/dataset:

我认为下面的url应该列出我的BigQuery project/dataset中的表:

https://www.googleapis.com/bigquery/v2/projects/1047877053699/datasets/visits&accessToken=4%2FC196NizZwlNgWSt5oNqQwendmLNW.0vgUrlGJ6kMRshQV0ieZDApig3NfcgI

https://www.googleapis.com/bigquery/v2/projects/1047877053699/datasets/visits&accessToken=4%2FC196NizZwlNgWSt5oNqQwendmLNW.0vgUrlGJ6kMRshQV0ieZDApig3NfcgI

But calling with or without the accessToken returns the following message that "Login Required".

但是调用或不调用accessToken会返回“登录需要”的以下信息。

 {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

I know you can't repeat the code because of permissions, expired tokens, etc. But I wonder what step I might be missing conceptually.

我知道您不能重复代码,因为权限、过期的令牌等等。但是我想知道在概念上我可能遗漏了什么步骤。

2 个解决方案

#1


3  

Have you tried sending the accesstoken as an authorization header rather than as a url parameter?

您是否尝试过将accesstoken作为授权头而不是url参数发送?

as in

就像在

https://www.googleapis.com/bigquery/v2/projects/1047877053699/datasets/visits
Authorization: OAuth Your-access-token-here-not-urlencoded

#2


1  

FYI - looks like you originally used the parameter accessToken in the URL. It should instead by access_token, which looks like it works fine. Of course, Jordan's suggestion of using a Header is better if you're able to do it though-- it's more secure as it's unlikely to get logged in access logs, proxy server logs, etc.

看起来您最初在URL中使用了参数accessToken。它应该使用access_token,看起来它工作得很好。当然,如果您能够使用Header,那么Jordan的建议会更好一些——它更安全,因为它不太可能登录到访问日志、代理服务器日志等等。

#1


3  

Have you tried sending the accesstoken as an authorization header rather than as a url parameter?

您是否尝试过将accesstoken作为授权头而不是url参数发送?

as in

就像在

https://www.googleapis.com/bigquery/v2/projects/1047877053699/datasets/visits
Authorization: OAuth Your-access-token-here-not-urlencoded

#2


1  

FYI - looks like you originally used the parameter accessToken in the URL. It should instead by access_token, which looks like it works fine. Of course, Jordan's suggestion of using a Header is better if you're able to do it though-- it's more secure as it's unlikely to get logged in access logs, proxy server logs, etc.

看起来您最初在URL中使用了参数accessToken。它应该使用access_token,看起来它工作得很好。当然,如果您能够使用Header,那么Jordan的建议会更好一些——它更安全,因为它不太可能登录到访问日志、代理服务器日志等等。