使用服务帐户进行 Google 身份验证失败 (node.js) [英] Google Authentication with Service Account fails (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
});
});
身份验证失败:
{ error: 'unauthorized_client',
error_description: 'Unauthorized client or scope in request.' }
我不是 100% 确定my.personal@gmail.com".使用我的客户 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 Drive API 在 Google Developer Console 中启用.我还激活了试用帐户.
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).
这篇关于使用服务帐户进行 Google 身份验证失败 (node.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!