Azure上的Google Analytics Api

Phi*_*ins 7 c# asp.net-mvc azure google-analytics-api oauth-2.0

我想在我的MVC网站上使用google analytics api,使用api服务帐户进行身份验证,oauth2在我的localhost上没有问题,但是一旦我部署到Azure,我就会收到502错误:

"502 - Web服务器在充当网关或代理服务器时收到无效响应.您正在查找的页面存在问题,无法显示.当Web服务器(充当网关或代理)时联系在上游内容服务器上,它收到了来自内容服务器的无效响应."

继承我的代码:

const string ServiceAccountUser = "xxxxxxxxxx-cpla4j8focrebami0l87mbcto09j9j6k@developer.gserviceaccount.com";
AssertionFlowClient client = new AssertionFlowClient(
        GoogleAuthenticationServer.Description,
            new X509Certificate2(System.Web.Hosting.HostingEnvironment.MapPath("/Areas/Admin/xxxxxxxxxxxxxxxxxx-privatekey.p12"), 
                "notasecret", X509KeyStorageFlags.Exportable))
        {
            Scope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue(),
            ServiceAccountId = ServiceAccountUser //Bug, why does ServiceAccountUser have to be assigned to ServiceAccountId
            //,ServiceAccountUser = ServiceAccountUser
        };
        OAuth2Authenticator<AssertionFlowClient> authenticator = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚是什么造成的?我在Azure中遗漏了什么?

谢谢你的帮助.

Law*_*Lee 16

我也遇到了同样的问题但是X509KeyStorageFlags.MachineKeySet进入构造函数并为我修复了问题.

X509Certificate2 certificate = new X509Certificate2(file, "key", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
Run Code Online (Sandbox Code Playgroud)


小智 9

在这个完全相同的问题上经过几个小时的痛苦,我找到了一个解决各种信息来源的工作.

尝试从Azure网站读取p12文件时出现问题,即我的代码中的这一行失败

var key = new X509Certificate2(keyFile, keyPassword, X509KeyStorageFlags.Exportable);
Run Code Online (Sandbox Code Playgroud)

不知道为什么,但如果将文件拆分为cer和key.xml文件,它会起作用吗?

首先,提取这些文件,(我刚刚使用了一个控制台应用程序)

// load pfx/p12 as "exportable"
var p12Cert = new X509Certificate2(@"c:\Temp\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable);

// export .cer from .pfx/.p12
File.WriteAllBytes(@"C:\Temp\MyCert.cer", p12Cert.Export(X509ContentType.Cert));

// export private key XML
string privateKeyXml = p12Cert.PrivateKey.ToXmlString(true);

File.WriteAllText(@"C:\Temp\PrivateKey.xml", privateKeyXml);
Run Code Online (Sandbox Code Playgroud)

然后将它们复制到您的网站,然后像这样加载它们

//Store the authentication description
AuthorizationServerDescription desc = GoogleAuthenticationServer.Description;

//Create a certificate object to use when authenticating

var rsaCryptoServiceProvider = new RSACryptoServiceProvider();
rsaCryptoServiceProvider.FromXmlString(File.ReadAllText(keyFile));
var key = new X509Certificate2(certFile) {PrivateKey = rsaCryptoServiceProvider};


//Now, we will log in and authenticate, passing in the description
//and key from above, then setting the accountId and scope
var client = new AssertionFlowClient(desc, key)
{
    //cliendId is your SERVICE ACCOUNT Email Address from Google APIs Console
    //looks something like 12345-randomstring@developer.gserviceaccount.com
    //~IMPORTANT~: this email address has to be added to your Google Analytics profile
    // and given Read & Analyze permissions
    ServiceAccountId = clientId,
    Scope = "https://www.googleapis.com/auth/analytics.readonly"
};

//Finally, complete the authentication process
//NOTE: This is the first change from the update above
var auth = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);

//First, create a new service object
//NOTE: this is the second change from the update
//above. Thanks to James for pointing this out
var gas = new AnalyticsService(new BaseClientService.Initializer { Authenticator = auth });
Run Code Online (Sandbox Code Playgroud)

这对我来说很有用,我希望它可以帮到你.