2014-09-15 35 views
11

Khi tôi gọi API Google từ trang web Azure, tôi nhận được 502 - Máy chủ web nhận được phản hồi không hợp lệ trong khi hoạt động như cổng hoặc máy chủ proxy. Mã chính xác hoạt động từ cả máy cục bộ của tôi cũng như máy ảo Azure.502 Phản hồi không hợp lệ khi gọi Google Api từ Azure Website

Mã này chỉ đơn giản là để có được một tên hiển thị từ một id người dùng Google

private string GetUserDetails(string userId) 
{ 
    var serviceAccountEmail = "[email protected]nt.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; 
} 

này đã được gọi trong một dự án WebAPI, nhưng tôi đã rút ra nó vào một trang duy nhất mẫu web asp.net tại http://testgplus.azurewebsites.net/

Tôi cũng đã thử một ứng dụng REST đơn giản với một ApiKey thay vì sử dụng ở trên. Một lần nữa công trình này trên máy ảo, nhưng không phải trên trang web, nơi tôi nhận được một 403 Cấm. Tôi đã thêm địa chỉ IP của trang web & máy ảo vào Bảng điều khiển dành cho nhà phát triển của Google.

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

Có vẻ như tôi không thể gọi dịch vụ web bên ngoài cho trang web Azure. Tôi đã thấy một số vấn đề tương tự, ví dụ: 502 requesting payment service inside azure 'website', nhưng không có đề xuất nào có hiệu quả. Có ai có bất kỳ ý tưởng về nguyên nhân hoặc sửa chữa có thể là gì?

Trả lời

1

. Hi Colin Mierowsky

Bạn tạo chứng chỉ ở đâu, trong phương thức Application_Start hoặc WebApiConfig Register?

nơi sử dụng mã này?

makecert -r -n "CN=abdullahsargin.com, [email protected]" -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 

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

. phương pháp này chạy thành công trên địa phương nhưng trong xanh được 502 trên mã này, tôi kiểm tra hàng phương pháp này và chèo

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

hoàn toàn của phương pháp này

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

tôi tìm thấy ở trang này giải pháp của tôi

ASP.NET Identity: use GeneratePasswordResetToken on Azure website

cho giải pháp của tôi

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

tôi thấy câu hỏi của bạn trước đây, nhưng đã không nhận thấy giải pháp ... Tôi có nó bây giờ cũng .. Khi tạo thêm chứng chỉ:

var certificate = new X509Certificate2(p12Path, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable); 
//(notice the X509KeyStorageFlags.MachineKeySet |) 
Các vấn đề liên quan