gpt4 book ai didi

go - googleapi : Error 403: Request had insufficient authentication scopes.更多详细信息: Reason: insufficientPermissions,消息:权限不足

转载 作者:行者123 更新时间:2023-12-03 10:09:09 25 4
gpt4 key购买 nike

我正在尝试使用Gmail API发送电子邮件。但是我得到这个错误

googleapi: Error 403: Request had insufficient authentication scopes.More details:Reason: insufficientPermissions, Message: Insufficient Permission


我认为这可能与配置有关,我也遵循了Google的Go快速入门
这是getClient函数:
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)
}
//GmailService
res, err := srv.Users.Messages.Send("me", &message).Do() // change me
if err != nil {
fmt.Println("Res Err")
fmt.Println(err)
return false, err
}
fmt.Println(res)
我尝试了 for config, err := google.ConfigFromJSON(b, gmail.MailGoogleComScope),尝试使用 GmailReadonlyScopegmail.GmailSendScope,但是遇到了同样的错误。

最佳答案

Request had insufficient authentication scopes.


意味着已授权您的应用程序访问其数据的用户未授予您的应用程序足够的权限来执行您尝试做的事情。
您似乎正在使用 user.message.send方法。如果查看文档,则会发现该方法要求用户具有以下作用域之一。
enter image description here
如果您确实遵循 Googles quick start for go,那么您将 gmail.GmailReadonlyScope用作作用域,这只会给您只读访问权限。
但是,您的代码现在似乎包含了 mail.MailGoogleComScope范围,该范围应该可以工作,但是我想您忽略了重新授权该应用程序。而且在教程中没有看到评论

// If modifying these scopes, delete your previously saved token.json.


我建议您删除token.json,然后应用程序将要求您再次对其进行授权,并且您的代码应使用提升的权限进行工作。

关于go - googleapi : Error 403: Request had insufficient authentication scopes.更多详细信息: Reason: insufficientPermissions,消息:权限不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65946707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com