【问题标题】:Google Analytics Api on AzureAzure 上的 Google Analytics API
【发布时间】:2013-06-04 05:29:26
【问题描述】:

我想在我的 MVC 网站中使用谷歌分析 api,我使用 api 服务帐户和 oauth2 进行身份验证,在我的本地主机上没有问题,但是一旦我部署到 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);

我不知道是什么原因造成的?我在 Azure 中遗漏了什么吗?

感谢您的帮助。

【问题讨论】:

    标签: c# asp.net-mvc azure oauth-2.0 google-analytics-api


    【解决方案1】:

    在对这个完全相同的问题进行了数小时的痛苦之后,我通过拼凑各种信息来源找到了解决方法。

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

    var key = new X509Certificate2(keyFile, keyPassword, X509KeyStorageFlags.Exportable);
    

    不知道为什么,但是如果将文件拆分为 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);
    

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

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

    这对我有用,希望对你有所帮助。

    【讨论】:

    • 惊人的完美!非常感谢,如果没有你的帮助,我永远也做不到。
    • Goodone Martyn 在谷歌分析 api 实现中,我们没有太多帮助,最终不得不自己解决问题
    【解决方案2】:

    我也遇到了同样的问题,但将X509KeyStorageFlags.MachineKeySet 传递给构造函数也为我解决了这个问题。

    X509Certificate2 certificate = new X509Certificate2(file, "key", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
    

    【讨论】:

    • 天哪。它对我来说非常有效。您怎么知道将 X509KeyStorageFlags.MachineKeySet 标志添加到构造函数?为什么会这样?
    • 实际上,这对我不起作用。我继续收到间歇性的 502 错误。
    • 为我工作!谢谢
    • 为我节省了数小时的调试时间!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多