Gmail API和Google APIs Node Client错误500 backendError
我正在尝试将新的 Gmail API 与
I'm trying to use the new Gmail API with the Google API Node client. I created a new project from the developer console, set up a new "Service Account" Client ID, and enabled access to the API.
作为概念证明,我只是试图在我的收件箱中列出线程.当我为 API资源管理器启用OAuth 2.0切换时>,然后输入我的电子邮件地址,请求成功,并且我看到包含数据的JSON响应.
As a proof of concept, I am simply trying to list the threads in my inbox. When I enable the OAuth 2.0 toggle for the API explorer and enter my email address, the request succeeds and I see a JSON response with data.
现在,我尝试在Node中执行相同的操作:
Now I try to do the same in Node:
var googleapis = require('googleapis');
var SERVICE_ACCOUNT_EMAIL = '...SNIP...';
// generated by: openssl pkcs12 -in ...SNIP...p12 -out key.pem -nocerts -nodes
var SERVICE_ACCOUNT_KEY_FILE = 'key.pem';
var jwt = new googleapis.auth.JWT(
SERVICE_ACCOUNT_EMAIL,
SERVICE_ACCOUNT_KEY_FILE,
null,
['https://www.googleapis.com/auth/gmail.readonly']);
googleapis
.discover('gmail', 'v1')
.execute(function(err, client) {
jwt.authorize(function(err, result) {
if(err) console.error(err);
else console.log(result);
client.gmail.users.threads.list()
.withAuthClient(jwt)
.execute(function(err, result) {
if(err) console.error(err);
else console.log(result);
});
});
});
首先,我打印出authorize()
调用的结果,看起来它返回了令牌,因此我认为我已经正确设置了所有OAuth内容:
First I print the results of the authorize()
call, which looks like it returns a token, so I think I have all the OAuth stuff setup properly:
{ access_token: '...SNIP...',
token_type: 'Bearer',
expires_in: 1404277946,
refresh_token: 'jwt-placeholder' }
然后我尝试实际使用该API,但出现错误:
Then I try to actually use the API, but I get an error:
{ errors:
[ { domain: 'global',
reason: 'backendError',
message: 'Backend Error' } ],
code: 500,
message: 'Backend Error' }
在这一点上,我不知道还要尝试什么.我认为 OAuth信息正常运行,因为我没有收到任何身份验证错误.我还认为 API本身可以正常工作,并且我的帐户还可以,因为我可以通过API资源管理器使用它.我也没有看到节点库有故障的任何迹象.简而言之,我不知道问题出在哪里.有什么想法吗?
At this point, I don't know what else to try. I think the OAuth stuff is working properly, because I haven't gotten any authentication errors. I also think the API itself is working and my account is fine, because I can use it through the API Explorer. I don't see any indication that the Node library is at fault either. In short, I have no idea what the problem is. Any ideas?
您正在使用服务帐户来验证对GMail的请求.据我所知,您的服务帐户将没有Gmail,只有用户拥有GMail.因此,您需要与用户进行OAuth2流程(参见此处).
You are using the Service Account to authenticate your requests to GMail. Your Service Account will not have a Gmail as far as I know, only users have GMail. For this reason you will need to do the OAuth2 flow with the user (see here for example).