Hi there,

I have been trying for quite some time to get over an error while trying to 
send an email using a service account, via the Gmail API.
I get:

> googleapi: Error 400: Precondition check failed., failedPrecondition


Code is:
package gmailApi

import (
   "encoding/base64"
   "fmt"
   "net/mail"
   "strings"

    "golang.org/x/net/context"
   "google.golang.org/api/gmail/v1"
   "google.golang.org/api/option"
)

func SendEmailVerification() {
   srv, err := gmail.NewService(context.Background(), option.
WithCredentialsFile("credentials.json"), option.WithScopes(
"https://mail.google.com/";, "https://www.googleapis.com/auth/gmail.modify";, 
"https://www.googleapis.com/auth/gmail.compose";, 
"https://www.googleapis.com/auth/gmail.send";))
   if err != nil {
       fmt.Println(err) // BUG(Vincent): TEST
   }

   // New message for our gmail service to send
   var message gmail.Message
   // Compose the message

// @ symbol modified for publishing on golang-nuts
   messageStr := []byte(
       "From: vincent[at]gamifly.gg\r\n" +
           "To: jouglardv[at]gmail.com\r\n" +
           "Subject: My first Gmail API message\r\n\r\n" +
           "Message body goes here!")

    // Place messageStr into message.Raw in base64 encoded format
   message.Raw = base64.URLEncoding.EncodeToString(messageStr)

    if rslt, err := srv.Users.GetProfile("me").Do(); err != nil {
       fmt.Println(err) // BUG(Vincent): TEST
   } else {
       fmt.Println("111", rslt)
   }

    msg := ComposeMessage()
   _, err = srv.Users.Messages.Send("me", msg).Do()
   if err != nil {
       fmt.Println(err) // BUG(Vincent): TEST
   } else {
       fmt.Println("OKOKOK")
   }
}

func ComposeMessage() *gmail.Message {
   from := mail.Address{"Vincent", "vincent[at]gamifly.gg"} // @ symbol 
modified for publishing on golang-nuts
   to := mail.Address{"Vincent", "jouglardv[at]gmail.com"} // @ symbol 
modified for publishing on golang-nuts

    header := make(map[string]string)
   header["From"] = from.String()
   header["To"] = to.String()
   header["Subject"] = encodeRFC2047("Hello, Gmail!")
   header["MIME-Version"] = "1.0"
   header["Content-Type"] = "text/plain; charset=\"utf-8\""
   header["Content-Transfer-Encoding"] = "base64"

    var msg string
   for k, v := range header {
       msg += fmt.Sprintf("%s: %s\r\n", k, v)
   }
   msg += "\r\n" + "Message"

    return &gmail.Message{
       Raw: base64.RawURLEncoding.EncodeToString([]byte(msg)),
   }
}

func encodeRFC2047(s string) string {
   // use mail's rfc2047 to encode any string
   addr := mail.Address{s, ""}
   return strings.Trim(addr.String(), " <>")
}


 
vincent[at]gamifly.gg is the GSuite user that is being impersonated.
jouglardv[at]gmail.com is a personnal address for testing purpose (plz 
don't spam <3 )

The service account has domain delegation activated, scopes that are passed 
are in API Controls > Domain-Wide Delegation: 

>
> https://www.googleapis.com/auth/gmail.send, https://mail.google.com/, 
> https://www.googleapis.com/auth/gmail.modify, 
> https://www.googleapis.com/auth/gmail.compose



Is there anything wrong that I may be doing ?
Thanks guys

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/91828beb-a29f-46e9-a30c-a35e2757b731o%40googlegroups.com.

Reply via email to