Golang调用Google API时遇到404错误:未找到关联的Bot项目
Golang调用Google API时遇到404错误:未找到关联的Bot项目
msgService := chat.NewSpacesMessagesService(service)
msg := ChatCard("Title", "Subtitle", data)
// msg := "hello"
fmt.Print("Now ChatCard Method called\n")
_, err = msgService.Create("spaces/AAAAwlgqHZg", msg).Do()
if err != nil {
fmt.Println(err)
}
我正在尝试连接到 Google Chat,但 Google API 报错“未关联聊天机器人”。请问有人能提供帮助吗?
更多关于Golang调用Google API时遇到404错误:未找到关联的Bot项目的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复
更多关于Golang调用Google API时遇到404错误:未找到关联的Bot项目的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
根据您提供的代码和错误信息,问题很可能出现在Google Chat API的配置上。404错误“未关联聊天机器人”表明您的服务账号或API密钥没有正确关联到Google Chat机器人。
以下是解决此问题的步骤和示例代码:
1. 首先确认您的Google Chat机器人已正确配置
确保在Google Cloud Console中:
- 已启用Google Chat API
- 已创建服务账号并授予适当权限
- 机器人已关联到您的项目
2. 检查认证配置
确保您的service对象已正确初始化并具有足够权限:
package main
import (
"context"
"fmt"
"log"
"os"
"golang.org/x/oauth2/google"
"google.golang.org/api/chat/v1"
"google.golang.org/api/option"
)
func main() {
ctx := context.Background()
// 方式1:使用服务账号密钥文件
creds, err := google.CredentialsFromJSON(ctx, []byte(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")),
chat.ChatBotScope)
if err != nil {
log.Fatalf("无法加载凭证: %v", err)
}
// 方式2:或直接使用密钥文件路径
// creds, err := google.FindDefaultCredentials(ctx, chat.ChatBotScope)
chatService, err := chat.NewService(ctx, option.WithCredentials(creds))
if err != nil {
log.Fatalf("无法创建Chat服务: %v", err)
}
msgService := chat.NewSpacesMessagesService(chatService)
// 创建消息
msg := &chat.Message{
Text: "Hello from Go bot!",
}
// 确保space名称格式正确
spaceName := "spaces/AAAAwlgqHZg" // 替换为您的实际space ID
_, err = msgService.Create(spaceName, msg).Do()
if err != nil {
log.Fatalf("发送消息失败: %v", err)
}
fmt.Println("消息发送成功")
}
3. 验证机器人配置状态
添加配置验证代码:
func verifyBotConfiguration(service *chat.Service) error {
// 尝试获取机器人信息来验证配置
botService := chat.NewSpacesService(service)
// 列出可访问的spaces(需要适当权限)
spaces, err := botService.List().Do()
if err != nil {
return fmt.Errorf("无法列出spaces,请检查机器人配置: %v", err)
}
fmt.Printf("找到 %d 个可访问的spaces\n", len(spaces.Spaces))
return nil
}
4. 完整的错误处理示例
func sendChatMessage(spaceID, messageText string) error {
ctx := context.Background()
// 初始化认证
creds, err := google.FindDefaultCredentials(ctx, chat.ChatBotScope)
if err != nil {
return fmt.Errorf("认证失败: %v", err)
}
// 创建服务
chatService, err := chat.NewService(ctx, option.WithCredentials(creds))
if err != nil {
return fmt.Errorf("创建服务失败: %v", err)
}
// 验证space格式
if !strings.HasPrefix(spaceID, "spaces/") {
spaceID = "spaces/" + spaceID
}
// 创建消息
msg := &chat.Message{
Text: messageText,
Sender: &chat.User{
Name: "MyGoBot",
DisplayName: "Go语言机器人",
Type: "BOT",
},
}
// 发送消息
msgService := chat.NewSpacesMessagesService(chatService)
response, err := msgService.Create(spaceID, msg).Do()
if err != nil {
// 详细错误信息
if strings.Contains(err.Error(), "404") {
return fmt.Errorf("机器人未正确配置或未关联到space。请检查: %v", err)
}
return fmt.Errorf("API调用失败: %v", err)
}
fmt.Printf("消息发送成功,消息ID: %s\n", response.Name)
return nil
}
关键检查点:
- 确保服务账号具有正确的角色:在IAM中添加
Chat Bot角色 - 验证space ID:确保您有权限访问指定的space
- 检查OAuth范围:确保使用了
chat.ChatBotScope - 在Google Chat中启用机器人:需要在Chat界面中将机器人添加到space
最常见的解决方案是重新配置服务账号权限,并确保在Google Chat中正确添加了机器人。

