0
votes

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'
            });

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

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

I get this error

Error in the process: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. - Invalid Credentials

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
}
1
What info does oauth2.tokeninfo tell you about the token you are trying to use?David
@David my oAuth2 is a JSON, I added the structure to the questionTamir Klein

1 Answers

0
votes

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

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

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.

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.