为了账号安全,请及时绑定邮箱和手机立即绑定

googleapi: 错误 403: 请求没有足够的身份验证作用域。

googleapi: 错误 403: 请求没有足够的身份验证作用域。

Go
弑天下 2022-08-01 18:52:33
我正在尝试使用 Gmail API 发送电子邮件。但是我收到此错误googleapi: 错误 403: 请求没有足够的身份验证作用域。更多详细信息:原因:权限不足,消息:权限不足我认为它可能与配置有关,我也遵循了Google的快速入门,因为Go这里是getClient func:func getClient(config *oauth2.Config) *http.Client {    // The file token.json stores the user's access and refresh tokens, and is    // created automatically when the authorization flow completes for the first    // time.    tokFile := "token.json"    tok, err := tokenFromFile(tokFile)    if err != nil {            tok = getTokenFromWeb(config)            saveToken(tokFile, tok)    }    return config.Client(context.Background(), tok)}这是代码发送:case "pass":        templateData := struct {        VerPass string            }{        VerPass: cont1,            }    emailBody, err := parseTemplate("ver.html", templateData)    if err != nil {        fmt.Println("Parse Err")        return false, err     }         var message gmail.Message     emailTo := "To: " + to + "\r\n"    subject := "Subject: " + sub + "\n"    mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"    msg := []byte(emailTo + subject + mime + "\n" + emailBody)     message.Raw = base64.URLEncoding.EncodeToString(msg)     // Send the message    fmt.Println("OOOOOYYYYY")    //here is the problem    b, err := ioutil.ReadFile("credentials.json")if err != nil {        log.Fatalf("Unable to read client secret file: %v", err)}// If modifying these scopes, delete your previously saved token.json.config, err := google.ConfigFromJSON(b, gmail.MailGoogleComScope)if err != nil {        log.Fatalf("Unable to parse client secret file to config: %v", err)}    client := getClient(config)srv, err := gmail.New(client)if err != nil {        log.Fatalf("Unable to retrieve Gmail client: %v", err)}我试过了,我试过使用 和 ,但我得到了同样的错误。for config, err := google.ConfigFromJSON(b, gmail.MailGoogleComScope)GmailReadonlyScopegmail.GmailSendScope
查看完整描述

1 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

请求的身份验证作用域不足。

表示已授权您的应用程序访问其数据的用户尚未授予您的应用程序足够的权限来执行您尝试执行的操作。

您正在使用 user.message.send 方法。如果查看文档,您会发现该方法要求用户获得以下作用域之一的授权。

//img1.sycdn.imooc.com//62e7b0980001ee4706450275.jpg

如果您确实遵循了Google的快速入门,那么您将其用作范围,该范围仅为您提供只读访问权限。但是,您的代码现在可以包含应该工作的范围,但是我猜您忽略了重新授权应用程序。并且在教程中没有看到评论gmail.GmailReadonlyScopemail.MailGoogleComScope

如果修改这些作用域,请删除以前保存的 token.json。

我建议你删除 token.json,然后应用程序将要求你再次授权它,你的代码应该使用提升的权限。


查看完整回答
反对 回复 2022-08-01
  • 1 回答
  • 0 关注
  • 227 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号