Gmail API是否支持JWT?

vit*_*ano 5 node.js oauth-2.0 jwt google-oauth gmail-api

我想使用NodeJS访问Gmail API.

我正在使用服务器到服务器的方法(见这个)但是当我执行下面的代码时,我得到一个backEndError,来自Google API的代码500.

有任何想法吗?

var authClient = new google.auth.JWT(
    'email',
    'key.pem',
    // Contents of private_key.pem if you want to load the pem file yourself
    // (do not use the path parameter above if using this param)
    'key',
    // Scopes can be specified either as an array or as a single, space-delimited string
    ['https://www.googleapis.com/auth/gmail.readonly']
);

authClient.authorize(function(err, tokens) {
    if (err) 
        console.log(err);

    gmail.users.messages.list({ userId: 'me', auth: authClient }, function(err, resp) {
    // handle err and response
    if (err) {
        console.log(err);
});
Run Code Online (Sandbox Code Playgroud)

小智 0

是的,我也有同样的问题。如果我使用范围“ https://mail.google.com ”,我得到

403
{
  "error" : "access_denied",
  "error_description" : "Requested client not authorized."
}
Run Code Online (Sandbox Code Playgroud)

如果我使用范围“ https://mail.google.com/ ”(注意末尾的 / ),我得到

403
 'Forbidden'
Run Code Online (Sandbox Code Playgroud)

这似乎与使用 JWT 和服务帐户有关。