2016-12-20 9 views
1

私はgmail apisを試しています。私は認証をしました。今私はドラフトを作りたいと思う。しかし、私はこのエラーが発生していますJS変換文字列をrfc822

{ 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に変換する良い方法がないか分かりません。それ、どうやったら出来るの?

私は流星を私のアプリに使用しています。私のコードはここにあります。

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) 
     } 
     }) 
    } 
    }) 
}) 
+1

[RFC822](https://www.ietf.org/rfc/rfc0822.txt)は、1980年代の初期の電子メールプロトコルです。適切なヘッダーと有効な本文を使用して、適切にフォーマットされたメールメッセージを作成する必要があることは、Gmailの意味です(明らかに)。あなたの文字列のフォーマットについて不平を言うことはありません。 –

答えて

0

私はコンテンツタイプを送信するために必要なメッセージ/ rfc822としてここに作業コードがあります。作成された下書きには空のコンテンツがあるため、未処理のメッセージには何か問題があります。しかし、ドラフト自体は正常に作成されます。

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

Base64では、メッセージをエンコードし、-ですべて+を置き換え、_ですべて/を交換し、それはURLセーフにするために、末尾=を削除します。

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}` 
    } 
}; 
+0

それはうまくいきませんでしたが、 ''Content-Type': 'application/json'''を' Content-Type ':' message/rfc822 'に変更していました。ありがとうございました。 –