Gmail API和Google APIs Node Client错误500 backendError [英] Error 500 backendError with Gmail API and Google APIs Node Client

查看:107
本文介绍了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资源管理器,然后输入我的电子邮件地址,请求成功,并且我看到包含数据的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).

这篇关于Gmail API和Google APIs Node Client错误500 backendError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆