【问题标题】:Getting a server to server access (google service account) token. invalid_grant error获取服务器到服务器访问(谷歌服务帐户)令牌。 invalid_grant 错误
【发布时间】:2016-04-19 11:29:18
【问题描述】:

我来了

{ "error": "invalid_grant", "error_description": "Bad Request" }

当我尝试从 Google 获取访问令牌时。我关注https://developers.google.com/identity/protocols/OAuth2ServiceAccount。我想也许我生成 JWT 令牌的方式不正确?

var header = { "alg": "RS256", "typ": "JWT" };
var body = {
    iss:'simplify-dev-calendar@simplify-dev-1188.iam.gserviceaccount.com',
    scope: 'https://www.googleapis.com/auth/calendar',
    aud: 'https://www.googleapis.com/oauth2/v4/token',
    exp: Date.now() + ONE_HOUR,
    iat: Date.now(),
};
var token = jsrsasign.jws.JWS.sign('RS256', header, body, googleServicePrivateKey);
return Q.Promise(function(resolve, reject) {
    superagent.post('https://www.googleapis.com/oauth2/v4/token')
        .set('Content-Type', 'application/x-www-form-urlencoded')
        .send({
            grant_type: encodeURIComponent('urn:ietf:params:oauth:grant-type:jwt-bearer'),
            assertion: token
        })
        .end(function(err, res) {
            if (err) return reject(err);
            resolve(res.body);
        });
});

我的 googleServicePrivateKey 看起来像

'-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCNPsOIG1HGgMhP\nG...

可以吗?那些\ns?

【问题讨论】:

    标签: node.js google-api jwt


    【解决方案1】:

    好的,使用kjwt 解决了问题并省略了到期/发布日期。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 2013-04-18
      • 1970-01-01
      • 2019-02-02
      • 2021-02-23
      • 2017-10-07
      • 1970-01-01
      相关资源
      最近更新 更多