簡體   English   中英

502從Azure網站呼叫Google Api時無效響應

[英]502 Invalid Response when calling Google Api from Azure Website

當我從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項目中調用的,但我已經將它提取到一個頁面的asp.net Web表單http://testgplus.azurewebsites.net/

我還嘗試了一個帶有ApiKey的簡單REST客戶端,而不是使用上面的。 這又適用於VM,但不在網站上,我得到了403 Forbidden。 我已將網站和VM的IP地址添加到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在azure'網站'中請求支付服務 ,但沒有一個建議有效。 有沒有人對原因或解決方案有什么看法?

我之前看過你的問題,但沒有注意到解決方案......我現在也有了..當生成證書時添加:

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

你好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標識:在Azure網站上使用GeneratePasswordResetToken

為我的解決方案

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM