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 ?
Script
I took the liberty to rebuild here the differents parts of your comment:
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:
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:
the request body must look at something like this:
(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)