如何将OAuth2与node

如何将OAuth2与node

本文介绍了如何将OAuth2与node.js结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想使用@ google-cloud客户端库向BigQuery插入数据.
由于我有多个客户端,并且每个客户端具有不同的IAM角色,因此无法使用这样的服务帐户:

I would like to use @google-cloud client lib to insert data to BigQuery.
Since I have multiple clients and each has different IAM role I can't use a service account like this:

const bigquery = new BigQuery({
                projectId: `myProject`,
                keyFilename: '/Users/services/ssh/myProject-111.json'
            });

相反,我想像这样使用特定于客户端的oauth2:

rather I would like to use client-specific oauth2 like this:

const bigquery = new BigQuery({
                projectId: `mydata-1470162410749`,
                token: clientOauth2Token
            });

我收到此错误

这是我正在使用的完整的摩卡测试代码:

This is the full mocha test code I'm using:

import BigQuery from '@google-cloud/bigquery';
import {GoogApi} from "../apiManager" //Private code to get Token from client DB

if (!global._babelPolyfill) {
    var a = require("babel-polyfill")
}

describe('Check routing', async () => {

    it('Test stack  ', async (done) => {

        //Fetch client Auth from local Database
        let apiManager = new GoogApi({query: {integrationTest: 'on'}}, {}, (err, httpResults) => {});
        let clientAuth = await apiManager.getGoogleTokensByUserId(`[email protected]`);

        //Replace the 2 value below with real values
        const tableName = "myTest";
        const dataset = "EVALUEX_DEV";

        try {
            const bigquery = new BigQuery({
                projectId: `myProject`,
                token: clientAuth.credentials.access_token
            });
            await bigquery.createDataset(dataset)
                .then(
                    args => {
                        console.log(`Create dataset, result is: ${args}`)
                    })
                .catch(err => {
                    console.log(`Error in the process: ${err.message}`)
                })
        } catch (err) {
            console.log("err", err)
        }
    })
})

这是我检查令牌时的样子

This is how my Token looks like when I Inspect it

{
  "domain": null,
  "_events": {},
  "_eventsCount": 0,
  "transporter": {},
  "credentials": {
    "access_token": "My Token",
    "refresh_token": "my Refresh Token"
  },
  "certificateExpiry": null,
  "refreshTokenPromises": [],
  "_clientId": "my Client Id",
  "_clientSecret": "My client secret",
  "redirectUri": "https://s1dg0yjhih.execute-api.us-east-1.amazonaws.com/stage1/goog/saveToken",
  "eagerRefreshThresholdMillis": 300000
}

推荐答案

您共享的JSON对象不是OAuth令牌,它看起来像@ google-auth-library中的OAuth2Client对象.

The JSON object you shared is not an OAuth token, it looks like a OAuth2Client object from @google-auth-library.

实际的OAuth令牌只是一个字符串,例如"ya29.ABC123ABC123_cG123ABCDETCETCETC".

An actual OAuth token is simply a string, like "ya29.ABC123ABC123_cG123ABCDETCETCETC".

该令牌"实际上是一个OAuth2Client,然后您可以使用getAccessToken方法获取令牌.如果它只是一个普通的JSON对象,那么您可以获取credentials.access_token字段,它是实际的访问令牌.

That "token" is actually an OAuth2Client then you can fetch the token with the getAccessToken method. If it is just a plain JSON object, then you can get the credentials.access_token field, is the actual access token.

请注意,访问令牌已过期. getAccessToken会在必要时获得一个新的,但不会从该对象获取access_token,并且在过期后可能会导致403s.

Note that access tokens expire. getAccessToken will get a new one if necessary, but just getting the access_token from the object will not, and can result in 403s after it expires.

这篇关于如何将OAuth2与node.js结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 12:22