Alfredo Alvarez
Alfredo Alvarez

Reputation: 334

Getting Error 500 Using the C# APi for Migration

**I've tried a few things even running an example that was in another stack overflow post and keep getting the same error message. I have tried using the byte array directly from the message. And Getting and turning into ASCCII then back into bytes. I have a message id using the <> brackets.

Below is most of my code.

using Google.Apis.Admin.Directory.directory_v1;
using Google.Apis.Auth.OAuth2;
using Google.Apis.GroupsMigration.v1;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GoogleEmailMigration
{
 class Program
 {
    [STAThread]
    static void Main(string[] args)
    {
        /*
         * The purpose of this tool is to migrate users into groups in the google business panel
         */
        Console.WriteLine("Group Migration Tool Using Google Client");
        Console.WriteLine("====================");
        try
        {
            UserCredential credential;
            var one =FillCredential();
            one.Wait();
            credential = one.Result;

            MigrationDetails detail = new MigrationDetails()
            {
                EmailUserName = "***",
                Domain = "****",
                GroupName = "***",
                Password = "***"
            };
            
            detail.LoadGroupId(credential);

            var service = new GroupsMigrationService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Switched On migration tool",
            });
   
                /*Download all emails messages */
                GmailMessageProxy proxy = new GmailMessageProxy(){UserName = detail.EmailUserName+"@"+detail.Domain, Password=detail.Password};
                proxy.ActOnMessagesText((message) =>
                {
                    using (var mem = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(message)))
                    {
                       mem.Seek(0, SeekOrigin.Begin);

                       var uploadObject = service.Archive.Insert(detail.GroupId,mem,"message/rfc822");
                       var result = uploadObject.Upload();
                       Console.WriteLine("Status:");
                       Console.WriteLine(result.Status);
                       Console.WriteLine("Bytes:");
                       Console.WriteLine(result.BytesSent);
                       Console.WriteLine("Exception");
                       Console.WriteLine(result.Exception.Message);
                    }
                }
                    , (ex) => Console.WriteLine(ex));          
            
        }
        catch (AggregateException ex)
        {
            foreach (var e in ex.InnerExceptions)
            {
                Console.WriteLine("ERROR: " + e.Message);
            }
        }
        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }

    private static async Task<UserCredential> FillCredential()
    {
        UserCredential credential;
        using (var stream = new FileStream(@"***", FileMode.Open, FileAccess.Read))
        {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                new[] { @"https://www.googleapis.com/auth/apps.groups.migration", DirectoryService.Scope.AdminDirectoryGroupReadonly, DirectoryService.Scope.AdminDirectoryUserReadonly, DirectoryService.Scope.AdminDirectoryUserschemaReadonly },
                "user", CancellationToken.None, new FileDataStore("GroupsMigration"));
        }
        return credential;
    }

}

}

Upvotes: 1

Views: 193

Answers (1)

Alfredo Alvarez
Alfredo Alvarez

Reputation: 334

I managed to solve it this line in the code above is wrong

var uploadObject = service.Archive.Insert(detail.GroupId,mem,"message/rfc822");

instead of sending the groupid use the group email the api had the wrong description on the example provided in the google page.

Error 500 is not very descriptive of the problem but after a lot of trial and error those were my findings.

Upvotes: 1

Related Questions