goland gomail api 获取邮件全文

12 min read

源代码

// Format sets the optional parameter "format": The format to return the
// message in.
//
// Possible values:
//   "minimal" - Returns only email message ID and labels; does not
// return the email headers, body, or payload.
//   "full" (default) - Returns the full email message data with body
// content parsed in the `payload` field; the `raw` field is not used.
// Format cannot be used when accessing the api using the gmail.metadata
// scope.
//   "raw" - Returns the full email message data with body content in
// the `raw` field as a base64url encoded string; the `payload` field is
// not used. Format cannot be used when accessing the api using the
// gmail.metadata scope.
//   "metadata" - Returns only email message ID, labels, and email
// headers.
func (c *UsersMessagesGetCall) Format(format string) *UsersMessagesGetCall {
	c.urlParams_.Set("format", format)
	return c
}

有full 没raw 返回的都是 base64url encoded string

API Format 传参方式

content, err := srv.Users.Messages.Get(user, lastMessage.Id).Format("full").Do()

golang base64 加解密

import  "encoding/base64"

var str = "ZHONGGUOnihao123"
strbytes := []byte(str)
encoded := base64.StdEncoding.EncodeToString(strbytes)

decoded, err := base64.StdEncoding.DecodeString(encoded)
decodestr := string(decoded)
fmt.Println(decodestr, err)

golang base64 解码报错

报错如下:

illegal base64 data at input byte 340

改用使用 base64.RawURLEncoding.DecodeString, 会在base64.URLEncoding.DecodeString(encrypt)基础上将 - _ 等符号还原成base64字符同时末尾=

package main
 
import(
	"encoding/base64"
	"fmt"
)
 
func main(){
	encrypt := "Cf1WA2nBMo3H9G2UPhlLBBVBsMDl4udWr7__e6Iy93eIqLKi3EOjGhk8TkHujL1Uj6aGfZJNBzIbVE2NfNaz4pob8uiQvGaeTZdWP-8lFmAm6J1sz8N15xQkO7ADa5bNLCCqtlQbN2z7JcNenvFuID_rZGqb_1gmr-BGubGRMiMSK7RdjQYrMHaBcHLPB0UteakzcQwgKxCW7u0ECHqPJ39ne9JUG22JBWRo1ORuX5r30J_XrW3SQcdPSxfe0kvd61y12QOYh8VlOBBdBeDNnyDXefI_tDJDBFeqTXCgKu9wFkkWIZiM7WwqogaY-bvjUisbrPO4_fjJ1c0nWDOqRA"
	_,err := base64.RawURLEncoding.DecodeString(encrypt)
	if err != nil {
		fmt.Println(err)
	}
}