繁体   English   中英

获取服务器到服务器的访问权限(Google服务帐户)令牌。 invalid_grant错误

[英]Getting a server to server access (google service account) token. invalid_grant error

我正进入(状态

{ "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...

可以吗? 用那些\\n s?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM