使用服务帐户进行Google身份验证失败(node.js)

使用服务帐户进行Google身份验证失败(node.js)

问题描述:

我正在尝试(node.js)示例应用程序以针对Google API进行身份验证,然后发出Google Drive请求.我尝试运行的示例来自googleapis node.js库的github自述文件使用jwt :

I'm trying out a (node.js) sample app to authenticate against Google API and then make a Google Drive request. The sample I am trying to run is from the github readme of the googleapis node.js library using jwt:

var jwtClient = new googleapis.auth.JWT(
  '123...xyz@developer.gserviceaccount.com',
  './key.pem',
  null,
  ['https://www.googleapis.com/auth/drive'],
  'my.personal@gmail.com');

jwtClient.authorize(function(err, tokens) {
  if (err) {
    console.log(err);
    return;
  }

  // Make an authorized request to list Drive files.
  drive.files.list({ auth: jwtClient }, function(err, resp) {
    // handle err and response
  });
});

身份验证失败,并显示以下信息:

Authentication fails with:

{ error: 'unauthorized_client',
  error_description: 'Unauthorized client or scope in request.' }

对于"my.personal@gmail.com",我不是100%知道.使用我的客户ID,我收到错误消息无效的模拟PRN电子邮件地址.".

I'm not 100% sure about the 'my.personal@gmail.com'. Using my Client ID, I receive the error 'Invalid impersonation prn email address.'.

我已经根据文档创建了服务帐户客户ID,服务电子邮件和证书指纹.我是否需要指定其他内容?我的范围不正确吗?如果是,应该是什么?

I have created service account client ID, service email and certificate fingerprints according to documentation. Do I have to specify additional things? Is my scope incorrect? If it is, what should it be?

在Google Developer Console中启用了Google Drive API.我还激活了试用帐户.

Google Drive API is enabled in the Google Developer Console. I also activated the trial account.

好吧,尝试了很多事情之后,结果很简单:在没有模拟"的情况下进行上述示例-只是通过电子邮件发送即可.代码:

Ugh, after trying many things, the result is rather simple: doing the above sample without 'impersonate'-email it just worked. Code:

var jwtClient = new googleapis.auth.JWT(
  '123...xyz@developer.gserviceaccount.com',
  './key.pem',
  null,
  ['https://www.googleapis.com/auth/drive']);

自述文件中的示例可作为示例中的完整文件使用(这里).

The example from the readme is available as a complete file inside examples (here).