title: Call to Google API from Azure: "Access is denied"
body: <p>I'm trying to create a chatbot that can manage the Google Calendar and access to Google People. I developed the code and it works perfectly offline. The problem arises when trying to upload everything online.</p>
  
  <p>The code is in C #, developed above the template provided by Microsoft. When you load the code on Azure the functionality of LUIS works, the problem arises when you make a call to the Google APIs. The bug that appears is the following:</p>
  
  <blockquote>
    <p>System.AggregateException: One or more errors occurred. —>
    System.Net.HttpListenerException: Access is denied at
    System.Net.HttpListener.SetupV2Config() at
    System.Net.HttpListener.Start() at
    Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.StartListener() at
    Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.d__14.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.ThrowIfExceptional(Boolean
    includeTaskCanceledExceptions) at
    System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout,
    CancellationToken cancellationToken) at
    Microsoft.Bot.Sample.LuisBot.Dialogs.LinkContact.ConnectPeople() at
    Microsoft.Bot.Sample.LuisBot.Dialogs.LinkContact.GetUserName() —>
    (Inner Exception #0) System.Net.HttpListenerException (0x80004005):
    Access is denied at System.Net.HttpListener.SetupV2Config() at
    System.Net.HttpListener.Start() at
    Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.StartListener() at
    Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.d__14.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()&lt;—</p>
  </blockquote>
  
  <p>Looking online I found many cases of similar problems, unfortunately, very few have been answered and among the few answers, none has solved the problem. Does anyone have any ideas?</p>
  
  <p>One of the last points on which I focused is linked to this report <a href="https://github.com/google/google-api-dotnet-client/issues/908" rel="nofollow noreferrer">https://github.com/google/google-api-dotnet-client/issues/908</a>, where at some point it says:</p>
  
  <blockquote>
    <p>"Sounds like its trying to launch the browser on the server and not on
    the users browser. It works on localhost because you have the power to
    launch a new browser session on your local pic. The problem comes when
    you try and deploy it and it trys to launch browser on the server
    rather in a new client browser. You should be following this authweb
    apps <a href="https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web-applications-aspnet-mvc" rel="nofollow noreferrer">https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web-applications-aspnet-mvc</a>"</p>
  </blockquote>
  
  <p>I do not know if it could be the problem. I tried to follow the link but I could not integrate the directions with my code.</p>
  
  <p>Here is my autentication code:</p>
  
  <pre><code>  private static PeopleServiceService ConnectPeople()
      {
          // Create OAuth credential.
       CancellationTokenSource tokenSource = new CancellationTokenSource();
      CancellationToken token = tokenSource.Token;
          IDataStore credentialPersistanceStore   = new Dialogs.myDataStore();
  
          Task&lt;UserCredential&gt; result = GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = "*id*",
                ClientSecret = "*secret*"
            },
            new[] { "profile", "https://www.googleapis.com/auth/contacts.readonly" },
          "me",token,
            credentialPersistanceStore);
  
          result.Wait();
          UserCredential credential = result.Result;
  
          // Create the service.
          var service = new PeopleServiceService(new BaseClientService.Initializer()
          {
              HttpClientInitializer = credential,
              ApplicationName = "*nome*",
          });
  
          return service;
      }
  </code></pre>
  
  <p>Then I use "service" to work with Google API.</p>
  
  <p>I have worked following:
  <a href="https://developers.google.com/google-apps/calendar/quickstart/dotnet" rel="nofollow noreferrer">https://developers.google.com/google-apps/calendar/quickstart/dotnet</a>
  and <a href="https://dev.botframework.com/" rel="nofollow noreferrer">https://dev.botframework.com/</a>.</p>
  
  <p>Thanks to everyone</p>