当我从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;
}
Run Code Online (Sandbox Code Playgroud)
这是在一个WebApi项目中调用的,但我已经将它提取到一个页面的asp.net Web表单http://testgplus.azurewebsites.net/
我还尝试了一个带有ApiKey的简单REST客户端,而不是使用上面的.这又适用于虚拟机,但不在网站上,我得到403 Forbidden.我已将网站和VM的IP地址添加到Google Developers Console.
private string GetUserDetails2(string userId)
{ …Run Code Online (Sandbox Code Playgroud) 我有一个使用Azure WebJobs安排的控制台应用程序.尝试读取p12证书的私钥时,执行总是失败.有趣的是,我无法捕获异常,我不得不使用旧Console.WriteLine的调试.
这是我的代码片段:
var certificate = new X509Certificate2(data, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { BigqueryService.Scope.Bigquery }
}.FromCertificate(certificate));
Run Code Online (Sandbox Code Playgroud)
其他帖子提到标志应该X509KeyStorageFlags.MachineKeySet但不幸的是,这会导致Google API出错.它需要设置X509KeyStorageFlags.Exportable标志.
任何人都可以确认X509KeyStorageFlags.Exportable在Azure网站和WebJobs上可用吗?