title: 502 Invalid Response when calling Google Api from Azure Website
body: <p>When I call Google APIs from an Azure website, I get <strong>502 - Web server received an invalid response while acting as a gateway or proxy server</strong>. The exact code works from both my local machine as well as an Azure VM.</p>
  
  <p>The code is simply to get a display name from a Google user id</p>
  
  <pre><code>private string GetUserDetails(string userId)
  {
      var serviceAccountEmail = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com";
      var certFile = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/googlekey.p12");
      var certificate = new X509Certificate2(certFile, "notasecret", X509KeyStorageFlags.Exportable);
  
      var credential = new ServiceAccountCredential(
         new ServiceAccountCredential.Initializer(serviceAccountEmail)
         {
             Scopes = new[] { PlusService.Scope.PlusMe }
         }.FromCertificate(certificate));
  
      var service = new PlusService(new BaseClientService.Initializer()
      {
          HttpClientInitializer = credential,
          ApplicationName = "Bayfront"
      });
  
      var request = service.People.Get(userId);
      var person = request.Execute();
      return person.DisplayName;
  }
  </code></pre>
  
  <p>This was being called in a WebApi project, but I have extracted it to a single page asp.net web form at <a href="http://testgplus.azurewebsites.net/" rel="nofollow">http://testgplus.azurewebsites.net/</a> </p>
  
  <p>I have also tried a simple REST client with an ApiKey instead of using the above. Again this works on the VM, but not on the web site, where I get a 403 Forbidden. I have added the IP addresses of the website &amp; the VM to the Google Developers Console.</p>
  
  <pre><code>private string GetUserDetails2(string userId)
  {
      var client = new RestClient("https://www.googleapis.com/plus/v1/people/" + userId);
      var request = new RestRequest(Method.GET);
      request.AddParameter("key", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
      var response = client.Execute(request);
      if (response.StatusCode == HttpStatusCode.OK)
      {
          dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(response.Content);
  
          return result["name"]["givenName"];
      }
      return response.StatusCode.ToString();
  }
  </code></pre>
  
  <p>It looks like I cannot call an outside web service for an Azure website. I have seen some similar issues, e.g. <a href="https://stackoverflow.com/questions/12057646/502-requesting-payment-service-inside-azure-website">502 requesting payment service inside azure &#39;website&#39;</a>, but none of the suggestions has worked. Has anyone got any ideas on what the cause or fix might be?</p>