Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Access Gmail using Google Api - hangs on GoogleWebAuthorizationBroker.AuthorizeAsync...

$
0
0

Hello

I am really struggling to access my gmail account using google api in sharepoint 2013.

My VM configuration:

  • Windows server 2012 r2 with latest updates.
  • Sharepoint 2013 enterprise with latest updates.
  • Visual Studio 2015 Pro

This is the code snippet which works perfectly with windows application or web application on same machine!

 
   public void ReadEmails()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {	//Gmail access
                    UserCredential credential;
                    var path = SPUtility.GetVersionedGenericSetupPath(@"TEMPLATE\LAYOUTS\mgClient_secret.json", 15);
                    using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                    {
                        string credPath = @"TEMPLATE\LAYOUTS\Gmail";
                        credPath = Path.Combine(credPath, ".credentials/mgSupportgmail");
                        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                            GoogleClientSecrets.Load(stream).Secrets,
                           new[] { GmailService.Scope.GmailReadonly },"user",
                            CancellationToken.None,
                            new FileDataStore(credPath, true)).Result;
                        Console.WriteLine("Credential file saved to: " + credPath);
                    }

                    // Create Gmail API service.
                    var service = new GmailService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = ApplicationName,
                    });


                    // Define parameters of request.
                    UsersResource.LabelsResource.ListRequest request = service.Users.Labels.List("me");

                    List<Message> oMails = ListMessages(service, "support@mygmail.co.uk", "label: auto - support");


                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }


    public static List<Message> ListMessages(GmailService service, String userId, String query)
        {
            List<Message> result = new List<Message>();
            UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List(userId);
            request.Q = query;

            do
            {
                try
                {
                    ListMessagesResponse response = request.Execute();
                    result.AddRange(response.Messages);
                    request.PageToken = response.NextPageToken;
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                }
            }
            while (!String.IsNullOrEmpty(request.PageToken));

            return result;
        }
   


 As I mentioned the code works with a win or web project (.Net Framework 4.5 like sharepoint 2013) with same configuration on same machine but if run it in a simple sharepoint project or webpart it just hangs on 


credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, new[] { GmailService.Scope.GmailReadonly },"user", CancellationToken.None, new FileDataStore(credPath, true)).Result;

and the Websites show a spinning wheel and never finish loading ...

There is no permission problem to write to folder  credPath.

Would appreciate any advice or suggestions

Thanks

 


 




Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>