0
votes

im using gmail api in my mvc 5 project and it works fine on local (debug mode)

Heres my code:

UserCredential credential1; var folder = System.Web.HttpContext.Current.Server.MapPath("/App_Data/MyGoogleStorage"); credential1 = await GoogleWebAuthorizationBroker.AuthorizeAsync( new ClientSecrets { ClientId = "xxxxxxxxxxxx.apps.googleusercontent.com", ClientSecret = "xxxxxxxxxxxxxxxxxxx" }, //GoogleClientSecrets.Load(stream).Secrets, // This OAuth 2.0 access scope allows for read-only access to the authenticated // user's account, but not other types of account access. new[] { GmailService.Scope.GmailReadonly, GmailService.Scope.MailGoogleCom, GmailService.Scope.GmailModify }, "user", CancellationToken.None, new FileDataStore(folder) );

but when i publish it and try to run from iis i got:

[ System.AggregateException: One or more errors occurred. ---> System.Net.HttpListenerException: Access is denied at System.Net.HttpListener.AddAllPrefixes() at System.Net.HttpListener.Start() at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.StartListener() at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.d__8.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp.d__8.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.d__1.MoveNext() --- End of inner exception stack trace --- at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) at DataOtomasyon.Helpers.gmailApi.d__2.MoveNext() in C:\Users\Administrator\Desktop\DataOtomasyon\DataOtomasyon\Helpers\gmailApi.cs:line 58 ---> (Inner Exception #0) System.Net.HttpListenerException (0x80004005): Access is denied at System.Net.HttpListener.AddAllPrefixes() at System.Net.HttpListener.Start() at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.StartListener() at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.d__8.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp.d__8.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.d__1.MoveNext()<--- ]

I checked permission to all available iis users and grant read/write permission to all of them i really confused and any help would be appreciated

1

1 Answers

0
votes
 credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true),
                new PromptCodeReceiver()).Result;

This might have been asked quite a while ago but I ran into the same issue and what solved it was using PromptCodeReceiver instead of not setting a code receiver parameter. It turns out that GoogleWebAuthorizationBroker uses LocalServerCodeReceiver by default when a code receiver is not defined.