2014-08-28 116 views
5

我想使用NodeJS访问Gmail API。Gmail API是否支持JWT?

我正在使用服务器到服务器的方法(请参阅this),但是当我执行下面的代码时,我从Google API得到一个backEndError代码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); 
}); 
+0

+1为好问题。我不知道,我会第二次浏览https://developers.google.com/accounts/docs/OAuth2Login并检查是否所有步骤都符合预期(此部分在您的代码中不可见)。一种情况下,这可能会失败,如果'我'帐户使用两步验证。在这种情况下,软件代理需要使用'me'为其创建的特殊令牌(https://developers.google.com/google-apps/help/faq/auth#what_twostep) – xmojmr 2014-08-30 03:27:26

+0

我也试图使用JWT但得到'access_denied请求的客户端没有授权.' – webjay 2014-09-15 18:24:45

+0

我可以问你关于'new google.auth.JWT'的参数吗?我使用'key.client_email'作为第一个参数,'null'作为第二个参数,'key.private_key'作为第三个参数,然后作用于作用域。我应该从哪个用户那里获取邮件?我需要从域内的各个帐户获取电子邮件。 – Kunok 2016-09-09 13:10:47

回答

0

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

403 
{ 
    "error" : "access_denied", 
    "error_description" : "Requested client not authorized." 
} 

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

这似乎是相关的使用JWT和服务帐户。