【发布时间】:2014-10-22 20:12:19
【问题描述】:
我想使用 NodeJS 访问 Gmail API。
我正在使用服务器到服务器的方法(请参阅this),但是当我执行下面的代码时,我得到了一个 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);
});
【问题讨论】:
-
+1 表示好问题。我不知道,我会第二次通过developers.google.com/accounts/docs/OAuth2Login 并查看所有步骤是否符合预期(这部分在您的代码中不可见)。这可能会失败的一种情况是“我”帐户使用两步身份验证。在这种情况下,软件代理需要使用“我”(developers.google.com/google-apps/help/faq/auth#what_twostep)仅为它们创建的特殊令牌
-
我也在尝试使用 JWT 但得到
access_denied Requested client not authorized. -
请问您提供给
new google.auth.JWT的参数?我使用key.client_email作为我的第一个参数,null作为第二个参数,key.private_key作为第三个参数,然后是作用域。我在哪里告诉它我应该从哪个用户那里获取邮件?我需要从域内的各个帐户中获取电子邮件。
标签: node.js oauth-2.0 google-oauth jwt gmail-api