错误 400:invalid_scope“https://www.googleapis.com/auth/chat.bot”

2023-12-26

新的谷歌环聊聊天的文档说你需要授权范围https://www.googleapis.com/auth/chat.bot https://developers.google.com/hangouts/chat/reference/rest/v1/spaces/get#authorization-scopes做几乎任何事情。

这是错误:

使用它们生成身份验证 URL 时OAuth2 客户端 https://github.com/google/google-api-nodejs-client#generating-an-authentication-url我收到消息说范围无效。如果我使用的话就没有这个问题https://www.googleapis.com/auth/chat https://www.googleapis.com/auth/chat或其他一些范围,例如谷歌加 https://developers.google.com/+/web/api/rest/oauth#authorization-scopes.

当我尝试在 google 上搜索内容时API浏览器 http://%20%20https://developers.google.com/apis-explorer/#search/bots/chat/v1/URL 或 URL 部分的组合也不起作用。

这是我用来获取 URL 的代码,似乎对其他所有内容都工作得很好:

var {google} = require('googleapis');
var OAuth2 = google.auth.OAuth2;

var oauth2Client = new OAuth2(
  "clientid-idididid.apps.googleusercontent.com",
  "_secretsuff",
  "http://localhost:3000/auth/google/callback"
);

var scopes = [
    "https://www.googleapis.com/auth/chat", //Works
    "https://www.googleapis.com/auth/chat.bot"  // Does not work
];

var url = oauth2Client.generateAuthUrl({
  access_type: 'offline',
  scope:  scopes,
});

console.log(url);

如果其他人遇到这个问题,我想我已经解决了。 Google 似乎不需要域用户启用此身份验证范围,因为在测试机器人时它已经在域上获得了授权。这些范围的“授权”由域中从空间添加/删除机器人的用户决定。

如果您感到困惑,我会详细介绍一下。

当您在控制台中为组织创建机器人时https://console.cloud.google.com/apis/api/chat.googleapis.com/ https://console.cloud.google.com/apis/api/chat.googleapis.com/您的机器人已添加到域中,并且可以由用户添加到空间中。如果然后转到凭据并创建一个服务帐户,您可以使用该 json 文件凭据作为您的机器人访问 API。下面的代码获取空间中人员的列表。

var { google } = require('googleapis');
var chat = google.chat("v1");

var key = require('./google_service-account-credentials.json');

var jwtClient = new google.auth.JWT(
  key.client_email,
  null,
  key.private_key,
  ['https://www.googleapis.com/auth/chat.bot'], // an array of auth scopes
  null
);

jwtClient.authorize(function (err, tokens) {
  chat.spaces.members.list({
    auth: jwtClient,
    parent: "spaces/AAAAD4xtKcE"
  }, function (err, resp) {
    console.log(resp.data);
  });
});

如果您尝试获取其他空间(和其他域)上的成员列表,机器人将失败并显示完全相同的错误消息:

“Bot 不是该空间的成员。”

我假设,如果您在市场上列出您的机器人,并将其添加到不同的域和空间,谷歌的 API 将确保您的机器人可以在逐个空间的基础上执行其尝试执行的操作。在添加机器人来完成其工作后,必须设置一些身份验证流程会很烦人。这也可能是为什么当前的 REST API 不允许您在域下列出空格的原因,这不是此 API 工作的范例。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误 400:invalid_scope“https://www.googleapis.com/auth/chat.bot” 的相关文章

随机推荐