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

我正在尝试使用 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


弑天下
浏览 228回答 1
1回答

慕盖茨4494581

请求的身份验证作用域不足。表示已授权您的应用程序访问其数据的用户尚未授予您的应用程序足够的权限来执行您尝试执行的操作。您正在使用 user.message.send 方法。如果查看文档,您会发现该方法要求用户获得以下作用域之一的授权。如果您确实遵循了Google的快速入门,那么您将其用作范围,该范围仅为您提供只读访问权限。但是,您的代码现在可以包含应该工作的范围,但是我猜您忽略了重新授权应用程序。并且在教程中没有看到评论gmail.GmailReadonlyScopemail.MailGoogleComScope如果修改这些作用域,请删除以前保存的 token.json。我建议你删除 token.json,然后应用程序将要求你再次授权它,你的代码应该使用提升的权限。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go