Search code examples
c#google-apigoogle-appsgoogle-groups-migration

Unable to find a rfc 822 message that works with Google Groups Migration API


I'm trying to use the C# Google Groups Migration API and not having much luck.

I have the following code:

var body =
@"Date: 16 Jul 07 10:12 GMT
From: samplesender@example.com
To: samplegroup@googlegroups.com


This is the body of the migrated email message.


";

var bytes = ASCIIEncoding.ASCII.GetBytes(body);

var messageStream = new MemoryStream(bytes);

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    new ClientSecrets { ClientId = "<insert client id here>", ClientSecret = "<insert client secret here>" },
    new[] { "https://www.googleapis.com/auth/apps.groups.migration" },
    "user",
    CancellationToken.None,
    new FileDataStore("GroupsMigration.Auth.Store")).Result;

var service = new GroupsMigrationService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "group migration application"
});

var request = service.Archive.Insert("<insert valid group email here>", messageStream, "message/rfc822");

IUploadProgress uploadStatus = request.Upload();

if (uploadStatus.Exception != null)
{
    Console.WriteLine(uploadStatus.Exception.ToString());
}

I keep getting the following exception :

The service groupsmigration has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Unable to parse the raw message [400]
Errors [
   Message[Unable to parse the raw message] Location[ - ] Reason[invalid] Domain[global]
]

According to the Groups Migration API documentation (https://developers.google.com/admin-sdk/groups-migration/v1/reference/archive/insert see the responseCode section towards the bottom of the page), it indicates that the message I am trying to migrate is rejected as malformed. I tried many different messages and I always get the same error -> Unable to parse the raw message [400].

Did anybody find a message that the Google Groups Migration accepts and would like to share ? Anything else I am doing wrong ?

Any help gratefully appreciated!


Solution

  • Found the solution :

    var body =
    @"Date: 16 Jul 07 10:12 GMT
    From: samplesender@example.com
    To: samplegroup@googlegroups.com
    Message-Id: <12345@acme.com>
    
    
    This is the body of the migrated email message.
    
    
    ";
    
    var bytes = ASCIIEncoding.ASCII.GetBytes(body);
    
    var messageStream = new MemoryStream(bytes);
    
    UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        new ClientSecrets { ClientId = "<insert client id here>", ClientSecret = "<insert client secret here>" },
        new[] { "https://www.googleapis.com/auth/apps.groups.migration" },
        "user",
        CancellationToken.None,
        new FileDataStore("GroupsMigration.Auth.Store")).Result;
            
    var service = new GroupsMigrationService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = "group migration application"
    });
    
    var request = service.Archive.Insert("<insert valid group email here>", messageStream, "message/rfc822");
    
    IUploadProgress uploadStatus = request.Upload();
    
    if (uploadStatus.Exception != null)
    {
        Console.WriteLine(uploadStatus.Exception.ToString());
    }
    

    Bottom line, if you want Google to accept your message you must put a Message-id header with the following format <NNNN@mail.samplegroup.com>