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ì?