X509Certificate2访问被拒绝错误

时间:2015-05-16 01:36:23

标签: asp.net google-analytics-api x509certificate google-api-dotnet-client

编辑:这是我(@DalmTo)建议后的最终代码:

  public static AnalyticsService Authenticate()
    { 
        string[] scopes = new string[] { AnalyticsService.Scope.Analytics,                 
     AnalyticsService.Scope.AnalyticsManageUsers};    

        string keyFilePath = @"G:\PleskVhosts\mydomainname\httpdocs\App_Data\API Project-2f74017ed363.p12";    // found in developer console
        string serviceAccountEmail = "myconsoleapiaccount@developer.gserviceaccount.com";  // found in developer console

        var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
        ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail) { Scopes = scopes }.FromCertificate(certificate));

        AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "myappname",
        });

非常感谢您关联我的教程,我仔细检查过,而且您的代码比尝试手动完成的工作少得多。我知道您提供给我的提示,并且我在Google Analytics帐户中拥有该帐户的必要权限。我按照你的教程,它就像我的localhost中的魅力,但当我发布我的网站时,这是我收到此错误的当前错误:

{"消息":"访问被拒绝。\ r \ n"," StackTrace":"在System.Security.Cryptography.X509Certificates.X509Store.Add(X509Certificate2证书)\ r \ n在Thunder.Main.Default.Authenticate()\ r \ n在Thunder.Main.Default.GetChartData()",& #34; ExceptionType":" System.Security.Cryptography.CryptographicException"}

我已与我的托管服务提供商联系,他们告诉我他们不会在IIS中进行更改,我已经在web.config中添加了信任级别的完整标记,但我仍然得到这个错误。我目前正在处理它,但如果您有任何建议,请告诉我。我会在这里更新如果我能想出一个解决方案。谢谢!

2 个答案:

答案 0 :(得分:0)

你真的在这里为自己做事更难。我不确定你的代码有什么问题。 TBH我从未尝试过手动操作,因为我使用client library

NuGet套餐

PM> Install-Package Google.Apis.Analytics.v3

使用服务帐户进行身份验证。

您需要发送密钥文件的完整路径,或者有时会抱怨。理想情况下,它应该位于Web根目录的一边,但它必须是Web服务器有权访问它的地方,因为您使用的是asp。

string[] scopes = 
      new string[] { 
         AnalyticsService.Scope.Analytics,                 // view and manage your Google Analytics data
         AnalyticsService.Scope.AnalyticsManageUsers};     // View Google Analytics data      

string keyFilePath = @"c:\file.p12" ;    // found in developer console
string serviceAccountEmail = "xx@developer.gserviceaccount.com";  // found in developer console
//loading the Key file
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail)
                                       {Scopes = scopes }.FromCertificate(certificate));

创建服务

您将上面创建的凭据传递给服务。然后,您的所有请求都将通过该服务。

AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
            {
             HttpClientInitializer = credential,
             ApplicationName = "Analytics API Sample",
            });

<强>提示

  1. 要使用与Google Analytics配合使用的服务帐户,必须有权访问您的Google Analytics帐户。转到Google Analytics网站管理部分,在帐户级别创建新用户,必须是帐户级别。如果不是帐户级别,我提到它不会工作吗?
  2. 代码取自我的教程系列。 Google Analtics with C#享受

答案 1 :(得分:0)

如上所述,您需要配置IIS,但在我们的情况下,您需要检查以下文件夹的权限:

C:\ ProgramData \微软\加密\ RSA \ MachineKeys的

如果设置X509KeyStorageFlags参数,它将在此文件夹中创建一个密钥文件。在我的情况下,此文件夹的权限存在差异。在上述文件夹中未添加池帐户。

相关问题