电子邮件迁移API v2:Google Apps脚本

时间:2014-02-25 12:14:19

标签: google-apps-script google-email-migration

我正在尝试编写一个GAS来迁移存储在Google Drive中的一些电子邮件,但我真的很难让POST正确地执行此操作并希望有人可以帮助我并引导我朝着正确的方向前进。

到目前为止我所拥有的是......

  var id = "12345678abcdefgh";
  var doc = DocumentApp.openById(id);
  var emlData = doc.getText();
  var api_scope = 'https://www.googleapis.com/auth/email.migration';
  var app_name = "migration";
  var userKey = "someone@mygappsdomain.com";
  var method = "POST";

  var url = "https://www.googleapis.com/upload/email/v2/users/"+userKey+"/mail?uploadType=multipart";


  var fields =  {"MailItem" : 
                  {"properties":
                   {'isInbox': 'true','isUnread': 'true'},
                   'labels': ['MigrateMe']}};

  var options = {payload: {data: JSON.stringify(emlData), fields: fields, contentType: 'multipart/related', boundary : 'part_boundary'}};
  var fetchArgs = googleOauth_(app_name,api_scope,method,options);
  try
  {
    var result = UrlFetchApp.fetch(url, fetchArgs).getResponseCode();
    Logger.log("done");
  }
  catch (ee) 
  {
    Logger.log(ee);
  }
}

这显然不起作用,我得到400错误代码。你知道什么可能是错的吗?

2 个答案:

答案 0 :(得分:1)

正如在评论中所说,这是一个用操场api做的例子:

第1步:
授权:https://www.googleapis.com/auth/email.migration

第2步:
授权blbablablabla(你知道该怎么做)

第3步:
您需要执行POST以获取URL:
https://www.googleapis.com/upload/email/v2/users/ EMAILADRESS@DOMAIN.EXT / mail

需要设置Content-type:custom ...
参数是:

uploadType:media
Content-Type:message/rfc822

请求正文必须查看类似的内容:

Date: Wed, 03 Jan 2013 02:56:03 -0800
From: admin@example.org
To: liz@example.com
Subject: Hello World!
MIME-Version: 1.0
Content-Type: text/html; charset=windows-1252
Content-transfer-encoding: 8bit

And I think to myself... What a wonderful world!

(您可以正好用这个进行一些测试,然后在您的邮箱中搜索“liz”)

如果一切正常,你应该有这样的事情: enter image description here

为此,我使用了此处的文档:
https://developers.google.com/admin-sdk/email-migration/v2/guides/upload
https://developers.google.com/admin-sdk/email-migration/v2/reference/mail/insert

这不是你的帖子的解决方案,但我希望它能帮助你获得它! (顺便说一句,如果你有一个很好的工作代码,请给出你问题的真实答案 - 我也感兴趣)

答案 1 :(得分:0)

脚本

我冒昧地在这里重建你评论的不同部分:

var template = HtmlService.createTemplateFromFile("MailTemplate");
template.sentDate = "Wed, 04 Mar 2014 02:56:03 -0800";
template.from = "sender.address@blahblah.com";
template.to = "recipient.address@blahblah.com";
template.subject = "second test";
template.body = "I was migrated via script !!!!";
template.sentDate = eml_sentDate;
template.from = eml_from;
template.to = eml_to;
template.subject = eml_subject;
template.body = eml_body;
var emlData = template.evaluate().getContent();
var api_scope = 'googleapis.com/auth/email.migration';;
var app_name = "migration";
var userKey = "recipient.address@blahblah.com";
var method = "POST";

var url = "googleapis.com/upload/email/v2/users/" + userKey + "/…"; 
var options = { method : 'POST', contentType: 'message/rfc822', uploadType: 'media', payload: emlData };


var fetchArgs = googleOauth_(app_name, api_scope, method);
fetchArgs.payload = emlData;
fetchArgs.contentType = 'message/rfc822';
fetchArgs.uploadType = 'media';
var result = UrlFetchApp.fetch(url, fetchArgs).getResponseCode();

Multipart

我可以将你带给du multipart:它是一个脚本的一部分,专门用于批量请求google api(它旨在将用户添加到谷歌组)。批处理是通过multipart文件完成的,所以我认为这与你的邮件完全相同:

function batchRequest(userArray,grpEmail){
  var grpEmail = grpEmail || "testgroup@domain.ext";
  //var postUrl = "https://www.googleapis.com/admin/directory/v1/groups/"+grpEmail+"/members";
  var postUrl = 'https://www.googleapis.com/batch';
  var accessToken = refreshAccessToken();
  var userEmail = ""
  var boundary = 'batch_foobarbaz';
  var  payload = "";

  for(var i in userArray){
  payload+= '--'+boundary+'\nContent-Type: application/http\nContent-Transfer-Encoding: binary\n\n';
  payload+='POST /admin/directory/v1/groups/'+grpEmail+'/members HTTP/1.1\n';
  payload+= 'Content-type: application/json\n\n';
  payload+='{"email": "'+userArray[i]+'", "role": "MEMBER"}\n';
  }
  payload+='--'+boundary+'--';

   var params={
     method:'POST',
    contentType:'multipart/mixed; boundary="'+boundary+'"',
     headers:{
       Authorization:" OAuth "+accessToken  
     },
      payload:payload,
     muteHttpExceptions:true
   }
  var requestResponse = UrlFetchApp.fetch(postUrl, params);
  var result = requestResponse.getContentText();
  Logger.log(requestResponse.getResponseCode()+" - "+result);
  return(requestResponse.getResponseCode());

}