[英]502 Invalid Response when calling Google Api from Azure Website
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 . 当我从Azure网站调用Google API时,我得到502 - Web服务器在充当网关或代理服务器时收到无效响应 。 The exact code works from both my local machine as well as an Azure VM. 确切的代码适用于我的本地计算机和Azure VM。
The code is simply to get a display name from a Google user id 代码只是从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;
}
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/ 这是在一个WebApi项目中调用的,但我已经将它提取到一个页面的asp.net Web表单http://testgplus.azurewebsites.net/
I have also tried a simple REST client with an ApiKey instead of using the above. 我还尝试了一个带有ApiKey的简单REST客户端,而不是使用上面的。 Again this works on the VM, but not on the web site, where I get a 403 Forbidden. 这又适用于VM,但不在网站上,我得到了403 Forbidden。 I have added the IP addresses of the website & the VM to the Google Developers Console. 我已将网站和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();
}
It looks like I cannot call an outside web service for an Azure website. 看起来我无法为Azure网站调用外部Web服务。 I have seen some similar issues, eg 502 requesting payment service inside azure 'website' , but none of the suggestions has worked. 我已经看到了一些类似的问题,例如502在azure'网站'中请求支付服务 ,但没有一个建议有效。 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 |)
. 。 Hi Colin Mierowsky 你好Colin Mierowsky
Where are you create certificate, in Application_Start, or WebApiConfig Register method ? 在Application_Start或WebApiConfig Register方法中,您在哪里创建证书?
where use this code ? 在哪里使用这段代码?
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
In global.asax application_start 在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);
}
. 。 this method running success on local but in azure get 502 on this code, i test this method row and row 这个方法在本地运行成功但在azure上获得502这段代码,我测试了这个方法的行和行
var code = await _userManager.GeneratePasswordResetTokenAsync(user.Id);
complete of this method 完成这种方法
[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());
}
}
i find at this page my solution 我在这个页面找到了我的解决方案
ASP.NET Identity: use GeneratePasswordResetToken on Azure website ASP.NET标识:在Azure网站上使用GeneratePasswordResetToken
for my solution 为我的解决方案
public UserManager() : base(new UserStore<ApplicationUser>(new MyDbContext()))
{
// other setup
this.UserTokenProvider = new TotpSecurityStampBasedTokenProvider<ApplicationUser, string>();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.