JS convert string to rfc822

1k views Asked by At

I am trying the gmail apis. I've done the auth. Now I want to create a draft. But I am getting this error

{ 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 require base64 string with rfc822 standard. I am not sure of any good way to convert a string to rfc822. How do I do that?

I am using meteor for my app and here is my code.

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

There are 2 answers

0
Hayk Safaryan On BEST ANSWER

I just needed to send content type as message/rfc822. Here is the working code. Note that the raw message has something wrong in ts because the draft that is created has empty content. But the draft itself is created successfully.

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: [email protected]\r\n' +
        'To: [email protected]\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)
        }
      })
    }
  })
})
1
Tholle On

Base64 encode the message and replace all + with -, replace all / with _, and remove the trailing = to make it URL-safe:

const rawMessage = btoa(
  "From: [email protected]\r\n" +
  "To: [email protected]\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}`
  }
};