Googleapi:错误403:请求的身份验证范围不足。更多详细信息:原因:权限不足,消息:权限不足 [英] googleapi: Error 403: Request had insufficient authentication scopes. More details: Reason: insufficientPermissions, Message: Insufficient Permission

查看:22
本文介绍了Googleapi:错误403:请求的身份验证范围不足。更多详细信息:原因:权限不足,消息:权限不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Gmail API发送电子邮件。但我收到此错误

Googleapi:错误403:请求的身份验证范围不足。 更多详情: 原因:权限不足,消息:权限不足

我想可能与配置有关,我也关注了Google的Quick Start for 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 + "
"
    subject := "Subject: " + sub + "
"
    mime := "MIME-version: 1.0;
Content-Type: text/html; charset="UTF-8";

"
    msg := []byte(emailTo + subject + mime + "
" + 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,但我收到了相同的错误。

推荐答案

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

意味着授权您的应用程序访问其数据的用户没有授予您的应用程序足够的权限来执行您尝试执行的操作。

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

如果您确实遵循了Googles quick start for go,则您将gmail.GmailReadonlyScope用作作用域,该作用域将只授予您只读访问权限。 然而,您的代码现在似乎包含了应该可以工作的mail.MailGoogleComScope作用域,但是我猜您忽略了重新授权应用程序。并且没有看到教程中的注释

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

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

这篇关于Googleapi:错误403:请求的身份验证范围不足。更多详细信息:原因:权限不足,消息:权限不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆