使用Discord OAuth API操作消息:Go API遇未授权错误求助
核心问题分析
你遇到的unauthorized错误主要来自两个关键点:
- OAuth2授权时缺少必要的权限范围(Scope):当前只请求了
identify权限,这个权限仅能获取用户基本信息,完全不具备读取频道消息的权限。 - 即使Scope配置正确,还需确保授权用户(或关联Bot账户)在目标服务器/频道中拥有读取消息历史的角色权限。
具体修复步骤
添加正确的OAuth2 Scope
根据频道类型添加对应权限范围:- 公域服务器频道:添加
discordgo.ScopeGuildMessages - DM频道/群组DM:添加
discordgo.ScopeMessagesRead
建议同时添加两个Scope以覆盖所有场景。
- 公域服务器频道:添加
验证服务器权限
确认授权用户在目标频道所在服务器中,被分配了包含读取消息历史的角色;如果使用Bot账户,需确保Bot已加入该服务器且权限配置正确。修改代码实现
以下是调整后的完整代码,重点修正了Scope配置,并完善了错误处理逻辑:
import ( "context" "fmt" "io/ioutil" "net/http" "golang.org/x/oauth2" "github.com/bwmarrin/discordgo" ) func HelloDiscord(w http.ResponseWriter, r *http.Request) { conf := &oauth2.Config{ RedirectURL: "http://localhost:8080/auth/discord", ClientID: "clientid", // 替换为你的实际ClientID ClientSecret: "clientsecret", // 替换为你的实际ClientSecret // 添加消息读取的必要权限范围 Scopes: []string{discordgo.ScopeIdentify, discordgo.ScopeGuildMessages, discordgo.ScopeMessagesRead}, Endpoint: discordgo.Endpoint, } // 处理Token交换,此处不再忽略错误 token, err := conf.Exchange(context.Background(), r.FormValue("code")) if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("Token交换失败: " + err.Error())) return } fmt.Println("获取到的Token:", token) // 创建带Token的客户端发起请求 client := conf.Client(context.Background(), token) // 添加limit参数,默认返回最近50条消息,最多可指定100条 resp, err := client.Get("https://discord.com/api/channels/1021410887191507004/messages?limit=50") if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("获取消息失败: " + err.Error())) return } defer resp.Body.Close() fmt.Println("API响应状态:", resp.Status) if resp.StatusCode != http.StatusOK { body, _ := ioutil.ReadAll(resp.Body) w.WriteHeader(resp.StatusCode) w.Write([]byte("Discord API错误: " + string(body))) return } body, err := ioutil.ReadAll(resp.Body) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("读取响应内容失败: " + err.Error())) return } w.Header().Set("Content-Type", "application/json") w.Write(body) }
额外注意事项
- 不要忽略
token.Exchange的错误:之前的代码直接用_忽略错误,会导致无法排查Token获取失败的问题。 - 使用
discordgo库的内置方法可以更便捷地操作Discord资源,比手动调用HTTP接口更高效。
内容的提问来源于stack exchange,提问作者Juliette D
相关产品推荐
相关产品推荐

