ItGo.me Focus on IT Recommend

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

go - Golang - Gmail API 400 BadRequest Failed Precondition

2020腾讯云8月秒杀活动,优惠非常大!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1040

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

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 (
    "encoding/base64"
    "fmt"
    "log"
    "net/mail"
    "strings"

    "golang.org/x/net/context"
    "golang.org/x/oauth2/google"

    gmail "google.golang.org/api/gmail/v1"
)

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 {
        panic(err)
    }

    // 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{"", "noreply@example.com"}

    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] == '=' {
        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
1

To send mail, you must specify a valid address for the From: header (https://support.google.com/mail/answer/22370). noreply@example.com is unlikely to be such an address given that example.com 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

oriUlr:http://stackoverflow.com/questions/34248076/golang-gmail-api-400-badrequest-failed-precondition

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