从Azure的网站称谷歌API时502响应无效 [英] 502 Invalid Response when calling Google Api from Azure Website

查看:214
本文介绍了从Azure的网站称谷歌API时502响应无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打电话从Azure的网站谷歌的API,我得到的 502 - 作为网关或代理服务器 Web服务器接收到一个无效响应。确切的code工作来自我的本地机器以及一个Azure的VM。

When I call Google APIs from an Azure website, I get 502 - Web server received an invalid response while acting as a gateway or proxy server. The exact code works from both my local machine as well as an Azure VM.

在code是简单地从谷歌的用户ID获得的显示名称

The code is simply to get a display name from a Google user id

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;
}

这是被称为一个项目的WebAPI,但我把它解压缩到一个单一页面asp.net web表单在 HTTP ://testgplus.azurewebsites.net/

This was being called in a WebApi project, but I have extracted it to a single page asp.net web form at http://testgplus.azurewebsites.net/

我也尝试了简单的REST客户端与ApiKey而不是使用上面。同样,这适用于虚拟机,而不是在网站上,在那里我得到禁止403。我加入的网站和放大器的IP地址;虚拟机的谷歌开发者控制台。

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 & the VM to the Google Developers Console.

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();
}

它看起来像我不能为Azure的网站调用外部Web服务。我已经看到了一些类似的问题,例如<一href=\"https://stackoverflow.com/questions/12057646/502-requesting-payment-service-inside-azure-website\">502请求蔚蓝的网站',但没有建议内支付服务工作过。是什么原因或修复可能是有没有人有什么想法?

It looks like I cannot call an outside web service for an Azure website. I have seen some similar issues, e.g. 502 requesting payment service inside azure 'website', but none of the suggestions has worked. Has anyone got any ideas on what the cause or fix might be?

推荐答案

我以前看到你的问题,但并没有注意到这个解决方案...我现在也。当生成证书添加:

I saw your question before, but didn't noticed the solution... I have it now also.. When generating the certificate add:

var certificate = new X509Certificate2(p12Path, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
//(notice the X509KeyStorageFlags.MachineKeySet |)

这篇关于从Azure的网站称谷歌API时502响应无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆