I am trying to write a GAS
to migrate some emails which are stored in Google Drive
but I am really struggling to get the POST correct to do this and was hoping someone could help me out and steer me in the right direction.
What I have so far is ..
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);
}
}
This obviously doesn't work and I get a 400 error code. Do you know what could be wrong ?
As spoken in comment here an exemple to do it with the playground api:
step 1:
Authorise: https://www.googleapis.com/auth/email.migration
step 2:
Authorize blbablablabla (you know what to do here)
step 3:
You need to do a POST to hte URL:
https://www.googleapis.com/upload/email/v2/users/EMAILADRESS@DOMAIN.EXT/mail
The Content-type need to be set on: custom...
and the arguments are:
uploadType:media
Content-Type:message/rfc822
the request body must look at something like this:
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!
(you can take exactly this one to do some test and then in your mailbox search for "liz")
If everything worked you should have something like this:
To do this I used the documentation found here:
https://developers.google.com/admin-sdk/email-migration/v2/guides/upload
https://developers.google.com/admin-sdk/email-migration/v2/reference/mail/insert
This is not the solution to your post, but I Hope it will help you to get it! (and by the way if you get to have a nice working code please give the real answer to your question - I'm also interested)
Script
I took the liberty to rebuild here the differents parts of your comment:
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
What I can give you to du multipart: it's a part of a script that is dedicated to do batch request to google api (it was designed to add users to a google group). The batch is done through a multipart file so I suppose it's quite the same thing than for your mail:
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());
}