使用服务帐户和OAuth连接到Google API

时间:2019-05-26 05:02:14

标签: google-cloud-platform google-iam

我使用的环境没有对GCP客户端库的本机支持。因此,我试图找出如何使用手工制作的JWT令牌直接进行身份验证。

从这里开始,我使用nodeJS测试环境修改了任务,并使用jwa来实现算法。

https://developers.google.com/identity/protocols/OAuth2ServiceAccount

私钥取自服务帐户文件的JSON版本。

运行测试时,它会捕获一个非常基本的400错误,即“无效请求”。我不确定如何解决它。

有人可以帮忙确定我在做什么错吗?

var assert = require('assert');
const jwa = require('jwa');
const request = require('request-promise');

const pk = require('../auth/tradestate-2-tw').private_key;

const authEndpoint = 'https://www.googleapis.com/oauth2/v4/token';


describe('Connecting to Google API', function() {

    it('should be able to get an auth token for Google Access', async () => {
      assert(pk && pk.length, 'PK exists');
      const header = { alg: "RS256", typ: "JWT" };
      const body = {
        "iss":"salesforce-treasury-wine@tradestate-2.iam.gserviceaccount.com",
        "scope":"https://www.googleapis.com/auth/devstorage.readonly",
        "aud":"https://www.googleapis.com/oauth2/v4/token",
        "exp": new Date().getTime() + 3600 * 1000,
        "iat": new Date().getTime()
      };
      console.log(JSON.stringify(body, null, 2));
      const encodedHeader = Buffer.from(JSON.toString(header)).toString('base64')
      const encodedBody = Buffer.from(JSON.toString(body)).toString('base64');
      const cryptoString = `${encodedHeader}.${encodedBody}`;
      const algo = jwa('RS256');
      const signature = algo.sign(cryptoString, pk);
      const jwt = `${encodedHeader}.${encodedBody}.${signature}`;
      console.log('jwt', jwt);
      const headers = {'Content-Type': 'application/x-www-form-urlencoded'};
      const form = {
        grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
        assertion: jwt
      };
      try { 
        const result = await request.post({url: authEndpoint, form, headers});
        assert(result, 'Reached result');
        console.log('Got result', JSON.stringify(result, null, 2));
      } catch (err) {
        console.log(JSON.stringify(err, null, 2));
        throw (err);
      }

    });
});

1 个答案:

答案 0 :(得分:1)

使用JSON.stringify而不是JSON.toString。通过您问题中的链接:

  

{“ alg”:“ RS256”,“ typ”:“ JWT”}

     

Base64url表示如下:

     

eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9

使用JSON.toString()然后使用base64编码时,您将得到W29iamVjdCBKU09OXQ ==,它说明了无效请求的400,因为它无法解密您发送的任何内容。

相关问题