JS将字符串转换为rfc822

时间:2016-12-20 12:08:20

标签: javascript meteor gmail-api rfc822

我正在尝试gmail apis。我已经完成了认证。现在我想创建一个草稿。但是我收到了这个错误

{ error: 
I20161220-15:53:43.486(4)?       { errors: [Object],
I20161220-15:53:43.487(4)?         code: 400,
I20161220-15:53:43.488(4)?         message: 'Media type \'application/octet-stream\' is not supported. Valid media types: [message/rfc822]' } } }

Gmail api需要带有rfc822标准的base64字符串。我不确定将字符串转换为rfc822的任何好方法。我该怎么做?

我正在为我的应用程序使用meteor,这是我的代码。

import { Meteor } from 'meteor/meteor'
import { HTTP } from 'meteor/http'

Meteor.startup(() => {
  // Meteor.call('createDraft')


  Meteor.methods({
    'createDraft': function () {
      console.log(this.userId)

      const user = Meteor.users.findOne(this.userId)
      const email = user.services.google.email
      console.log(email)
      const token = user.services.google.accessToken
      const dataObject = {
        message: {
          raw: CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse('dddd'))
        },
        headers: {
          Authorization: `Bearer ${token}`
        }
      }
      HTTP.post(`https://www.googleapis.com/upload/gmail/v1/users/${email}/drafts`, dataObject, (error, result) => {
        if (error) {
          console.log('err', error)
        }
        if (result) {
          console.log('res', result)
        }
      })
    }
  })
})

2 个答案:

答案 0 :(得分:1)

Base64对邮件进行编码并将所有+替换为-,将所有/替换为_,并删除尾随=以使其成为网址 - 安全

const rawMessage = btoa(
  "From: sender@gmail.com\r\n" +
  "To: receiver@gmail.com\r\n" +
  "Subject: Subject Text\r\n\r\n" +

  "The message text goes here"
).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')

const dataObject = {
  message: {
    raw: rawMessage
  },
  headers: {
    'Content-Type': 'application/json',
    Authorization: `Bearer ${token}`
  }
};

答案 1 :(得分:0)

我只需将内容类型作为message / rfc822发送。这是工作代码。请注意,原始消息在ts中有问题,因为创建的草稿具有空内容。但草案本身已成功创建。

import { Meteor } from 'meteor/meteor'
import { HTTP } from 'meteor/http'

Meteor.startup(() => {
  // Meteor.call('createDraft')

  Meteor.methods({
    'createDraft': function () {
      console.log(this.userId)
      // CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse('dddd'))
      const user = Meteor.users.findOne(this.userId)
      const email = user.services.google.email
      console.log(email)
      const token = user.services.google.accessToken
      const rawMessage = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(
        'From: sender@gmail.com\r\n' +
        'To: receiver@gmail.com\r\n' +
        'Subject: Subject Text\r\n\r\n' +
        'The message text goes here'
      )).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')

      const dataObject = {
        message: {
          raw: rawMessage
        },
        headers: {
          'Content-Type': 'message/rfc822',
          Authorization: `Bearer ${token}`
        }
      }
      HTTP.post(`https://www.googleapis.com/upload/gmail/v1/users/${email}/drafts`, dataObject, (error, result) => {
        if (error) {
          console.log('err', error)
        }
        if (result) {
          console.log('res', result)
        }
      })
    }
  })
})