Focus on IT Recommend

Home > go - Golang - Gmail API 400 BadRequest Failed Precondition

go - Golang - Gmail API 400 BadRequest Failed Precondition



I've been struggling against this error (googleapi: Error 400: Bad Request, failedPrecondition) for about 8 hours now. I've written a simple go program which reproduces the problem for me. Maybe someone familiar with the Google APIs can help me out.

The error is the same as the one mentioned here: Gmail REST API : 400 Bad Request + Failed Precondition. The only difference is that it is in Go. I've followed the steps but still no luck.

I've also updated my code based on How to send email through Gmail Go SDK?

Both questions were helpful but did not help resolve the latest issue. I have also set GOOGLE_APPLICATION_CREDENTIALS to my project's json.

package main

import (


    gmail ""

func main() {
    // Use oauth2.NoContext if there isn't a good context to pass in.
    ctx := context.TODO()

    client, err := google.DefaultClient(ctx, gmail.GmailSendScope)
    if err != nil {

    // create service
    svc, err := gmail.New(client)
    if err != nil {
        log.Fatalf("Unable to create Gmail service: %v", err)

    if err := SendEmail(ctx, svc, <redacted>); err != nil {
        log.Fatalf("failed to send email: %v", err)

func SendEmail(ctx context.Context, svc *gmail.Service, email string) error {
    from := mail.Address{"", ""}

    header := make(map[string]string)
    header["From"] = from.String()
    header["To"] = email
    header["Subject"] = encodeRFC2047("Hello, Gmail!")
    header["MIME-Version"] = "1.0"
    header["Content-Type"] = "text/html; 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" + "Hello, Gmail!"

    gmsg := gmail.Message{
        Raw: encodeWeb64String([]byte(msg)),

    _, err := svc.Users.Messages.Send("me", &gmsg).Do()
    return err

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

func encodeWeb64String(b []byte) string {

    s := base64.URLEncoding.EncodeToString(b)

    var i = len(s) - 1
    for s[i] == '=' {

    return s[0 : i+1]

Running the code above should produce the following error:

Recommend:google api - Sending gmail attachment using api failed

e read some answers and tried to built the request according to what I've read here: Mail attachment wrong media type Gmail API Before coding the function itself, I decided to use a Chrome extension (Simple Rest Client) to simulate the API

2015/12/12 22:16:46 failed to send email: googleapi: Error 400: Bad Request, failedPrecondition
go google-api gmail-api
asked Dec 13 '15 at 4:46 max 821 3 15 28      It looks like there is something malformed in your request. Can you provide the full request? –  Andres Dec 14 '15 at 19:42


1 Answers

To send mail, you must specify a valid address for the From: header ( is unlikely to be such an address given that is a test-only domain per RFC 2606.

answered Dec 21 '15 at 19:09 Brandon Jewett-Hall 598 3 9


Recommend:oauth - Go Gmail API Service Account

a Client ID and Client Secret, but a service account doesn't have a client secret. Here's what I do have: { "type": "service_account", "project_id": "my-project-id", "private_key_id": "107...", "private_key": "-----BEGIN PRIVATE KE


------splitte line----------------------------