在尝试授权脚本时获取错误:unauthorized_client

时间:2016-09-11 21:29:48

标签: node.js google-api authorization jwt google-api-nodejs-client

我已经创建了具有域范围委派及其范围的服务帐户(在管理控制台和开发人员控制台中),如文档中所述。我已经尝试了一个星期了,我被困住了。这是我的代码:

const google = require('googleapis');
const gmail = google.gmail('v1');
const directory = google.admin('directory_v1');
const scopes = [
  'https://www.googleapis.com/auth/gmail.readonly',
  'https://www.googleapis.com/auth/admin.directory.user.readonly'
];
const key = require('./service_key.json');

var authClient = new google.auth.JWT(
  key.client_email,
  key,
  key.private_key,
  scopes,
  "kruno@example.com"
);

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

// API call methods here...

});

我收到此错误:

  

错误:unauthorized_client

我无法理解:

  • 这是从服务器端脚本调用Google API方法而无需任何用户交互的正确方法吗? (仅限域名)
  • 服务帐户和实际用户帐户如何通过这种方式进行通信?
  • 我听说过回调URI,我错过了吗?

1 个答案:

答案 0 :(得分:0)

我认为您错过了最后一步,即在您域中的控制面板中访问您的应用程序。

您可以正确使用doc来使用您的应用程序激活它

https://developers.google.com/+/domains/authentication/delegation

此外,您可以从这里的第一个通话步骤开始

https://developers.google.com/adwords/api/docs/guides/first-api-call

相关问题