502从Azure网站呼叫Google Api时无效响应

时间:2014-09-15 23:24:14

标签: azure google-api

当我从Azure网站调用Google API时,我得到 502 - Web服务器在充当网关或代理服务器时收到无效响应。确切的代码可以在我的本地计算机和Azure VM上运行。

代码只是为了从Google用户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项目中调用的,但我已将其提取到http://testgplus.azurewebsites.net/的单页asp.net Web表单

我还尝试过一个带有ApiKey的简单REST客户端,而不是使用上面的。这又适用于VM,但不在网站上,我得到了403 Forbidden。我添加了网站的IP地址& VM到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服务。我见过一些类似的问题,例如502 requesting payment service inside azure 'website',但这些建议都没有奏效。有没有人对原因或解决方案有什么想法?

2 个答案:

答案 0 :(得分:4)

之前我看过你的问题,但没有注意到解决方案......我现在也有了..当生成证书时添加:

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

答案 1 :(得分:1)

。嗨Colin Mierowsky

在Application_Start或WebApiConfig Register方法中,您在哪里创建证书?

哪里使用这段代码?

makecert -r -n "CN=abdullahsargin.com, E=sargin48@gmail.com" -sky exchange -b 11/01/2015 -pe -sv myhost.pvk myhost.cer

pvk2pfx -pvk myhost.pvk -spc myhost.cer -pfx myhost.pfx -po Test.123

在global.asax application_start

         try
        {
            var certFile = Server.MapPath("~/App_Data/myhost.pfx");
            var cert = new X509Certificate2(certFile, "Test.123",
                X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
        }
        catch (Exception exc)
        {
            _tools.LogError(exc);
        }

。 这个方法在本地运行成功但在azure上获得502这段代码,我测试了这个方法的行和行

  var code = await _userManager.GeneratePasswordResetTokenAsync(user.Id);

完成此方法

    [HttpGet, AllowAnonymous]
    public async Task<HttpResponseMessage> ForgotPassword([FromUri] ForgotPasswordViewModel model)
    {
        try
        {               
            var code = await _userManager.GeneratePasswordResetTokenAsync(user.Id);

            return Request.CreateResponse(HttpStatusCode.OK, new { model = user });

            var url = "http://abdullahsargin.com#/account/resetPassword/" + user.Id + "/" + code;

            await _userManager.SendEmailAsync(user.Id, "Reset Password",
            "Please reset your password by clicking here: <a href=\"" + url + "\">link</a>");

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (Exception exc)
        {
            MyTools.LogError(exc.GetBaseException());
            return Request.CreateResponse(HttpStatusCode.BadRequest, exc.GetBaseException());
        }
    }

我在这个页面找到了我的解决方案

ASP.NET Identity: use GeneratePasswordResetToken on Azure website

我的解决方案

public UserManager() : base(new UserStore<ApplicationUser>(new MyDbContext()))
{
    // other setup
    this.UserTokenProvider = new TotpSecurityStampBasedTokenProvider<ApplicationUser, string>();
}