服务帐户 p12 文件的 CryptographicException
CryptographicException with Service account p12 file
我正在尝试使用服务帐户 p12 文件对我的 Google 分析帐户进行身份验证。下面的代码应该对我的分析帐户进行身份验证并请求我需要的数据,但是当我 运行 下面的代码时,我收到 X509Certificate2 错误。我已将我的 p12 文件放在我的项目的 bin/debug 文件夹中,并带有给定的凭据。
var certificate = new X509Certificate2(keyFilePath, keyPassword, X509KeyStorageFlags.Exportable);
我收到以下错误:
An unhandled exception of type
'System.Security.Cryptography.CryptographicException' occurred in
mscorlib.dll
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.Apis.Analytics.v3;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
namespace GoogleAnalytics
{
public partial class MainForm : Form
{
private string keyFilePath = @"CSharpApplication-9f9f7643f6.p12";
private string serviceAccountEmail = "56849870-9pqlki56dvp3bn64nais8ndes@developer.gserviceaccount.com";
private string keyPassword = "notasecret";
private string websiteCode = "67881935";
private AnalyticsService service = null;
private List<ChartRecord> visitsData = new List<ChartRecord>();
public MainForm()
{
InitializeComponent();
}
private void Authenticate()
{
//loading the Key file
var certificate = new X509Certificate2(keyFilePath, keyPassword, X509KeyStorageFlags.Exportable);
var scopes =
new string[] {
AnalyticsService.Scope.Analytics, // view and manage your analytics data
AnalyticsService.Scope.AnalyticsEdit, // edit management actives
AnalyticsService.Scope.AnalyticsManageUsers, // manage users
AnalyticsService.Scope.AnalyticsReadonly}; // View analytics data
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
}
private void QueryData()
{
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(
"ga:" + websiteCode,
DateTime.Today.AddDays(-15).ToString("yyyy-MM-dd"),
DateTime.Today.ToString("yyyy-MM-dd"),
"ga:sessions");
request.Dimensions = "ga:year,ga:month,ga:day";
var data = request.Execute();
foreach (var row in data.Rows)
{
visitsData.Add(new ChartRecord(new DateTime(int.Parse(row[0]), int.Parse(row[1]), int.Parse(row[2])).ToString("MM-dd-yyyy"), int.Parse(row[3])));
}
}
private void PopulateChart()
{
analyticsChart.Series[0].XValueMember = "Date";
analyticsChart.Series[0].YValueMembers = "Visits";
analyticsChart.DataSource = visitsData;
analyticsChart.DataBind();
}
private void btnData_Click(object sender, EventArgs e)
{
Authenticate();
QueryData();
PopulateChart();
}
}
}
更新:使用下面的回答代码,我得到类似
的错误
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\GoogleAnalytics2.vshost.exe'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Cannot find or open the PDB file.
The thread 0x3714 has exited with code 0 (0x0).
The thread 0x398c has exited with code 0 (0x0).
The thread 0x289c has exited with code 0 (0x0).
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\GoogleAnalytics2.exe'. Symbols loaded.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\Google.Apis.AnalyticsReporting.v4.dll'. Symbols loaded.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\Google.Apis.dll'. Symbols loaded.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\Google.Apis.Core.dll'. Symbols loaded.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\Google.Apis.Auth.PlatformServices.dll'. Symbols loaded.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\Google.Apis.Auth.dll'. Symbols loaded.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\Newtonsoft.Json.dll'. Cannot find or open the PDB file.
The thread 0x1228 has exited with code 0 (0x0).
The thread 0x30cc has exited with code 0 (0x0).
The program '[10788] GoogleAnalytics2.vshost.exe' has exited with code 0 (0x0).
The program '[10788] GoogleAnalytics2.vshost.exe: Program Trace' has exited with code 0 (0x0).
我的 program.cs 文件在哪里
namespace GoogleAnalytics2
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(ServiceAccountAuthExample.AuthenticateServiceAccount("analyticsdemo@ultimate-triode-151909.iam.gserviceaccount.com", "C:/Users/star/Downloads/CSharpApplication-9f9f557643f6.p12"));
}
}
}
请帮助 this.What 我在这里失踪了。感谢任何帮助。
我以前见过 P12 文件的问题。我还没有能够完全诊断出这个问题。有时下载服务帐户 .json 文件更容易。 Google 正在调用 p12 向后兼容,因此他们最终可能会停止使用 p12 文件。
这是我用于验证服务帐户的方法。它会自行判断您发送的凭证文件类型。
/// <summary>
/// Authenticating to Google using a Service account
/// Documentation: https://developers.google.com/accounts/docs/OAuth2#serviceaccount
/// </summary>
/// <param name="serviceAccountEmail">From Google Developer console https://console.developers.google.com</param>
/// <param name="serviceAccountCredentialFilePath">Location of the .p12 or Json Service account key file downloaded from Google Developer console https://console.developers.google.com</param>
/// <returns>AnalyticsService used to make requests against the Analytics API</returns>
public static AnalyticsService AuthenticateServiceAccount(string serviceAccountEmail, string serviceAccountCredentialFilePath)
{
try
{
if (string.IsNullOrEmpty(serviceAccountCredentialFilePath))
throw new Exception("Path to the service account credentials file is required.");
if (!File.Exists(serviceAccountCredentialFilePath))
throw new Exception("The service account credentials file does not exist at: " + serviceAccountCredentialFilePath);
if (string.IsNullOrEmpty(serviceAccountEmail))
throw new Exception("ServiceAccountEmail is required.");
// These are the scopes of permissions you need. It is best to request only what you need and not all of them
string[] scopes = new string[] { AnalyticsReportingService.Scope.Analytics }; // View your Google Analytics data
// For Json file
if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".json")
{
GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(scopes);
}
// Create the Analytics service.
return new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Analytics Authentication Sample",
});
}
else if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".p12")
{ // If its a P12 file
var certificate = new X509Certificate2(serviceAccountCredentialFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
// Create the Analytics service.
return new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Analytics Authentication Sample",
});
}
else
{
throw new Exception("Unsupported Service accounts credentials.");
}
}
catch (Exception ex)
{
Console.WriteLine("Create service account AnalyticsService failed" + ex.Message);
throw new Exception("CreateServiceAccountAnalyticsServiceFailed", ex);
}
}
从我的教程中窃取的代码Google Service accounts with Json file
用法:
var service = AnalyticsService AuthenticateServiceAccount(.....);
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(
"ga:" + websiteCode,
DateTime.Today.AddDays(-15).ToString("yyyy-MM-dd"),
DateTime.Today.ToString("yyyy-MM-dd"),
"ga:sessions");
request.Dimensions = "ga:year,ga:month,ga:day";
var data = request.Execute();
foreach (var row in data.Rows)
{
visitsData.Add(new ChartRecord(new DateTime(int.Parse(row[0]), int.Parse(row[1]), int.Parse(row[2])).ToString("MM-dd-yyyy"), int.Parse(row[3])));
}
我正在尝试使用服务帐户 p12 文件对我的 Google 分析帐户进行身份验证。下面的代码应该对我的分析帐户进行身份验证并请求我需要的数据,但是当我 运行 下面的代码时,我收到 X509Certificate2 错误。我已将我的 p12 文件放在我的项目的 bin/debug 文件夹中,并带有给定的凭据。
var certificate = new X509Certificate2(keyFilePath, keyPassword, X509KeyStorageFlags.Exportable);
我收到以下错误:
An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.Apis.Analytics.v3;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
namespace GoogleAnalytics
{
public partial class MainForm : Form
{
private string keyFilePath = @"CSharpApplication-9f9f7643f6.p12";
private string serviceAccountEmail = "56849870-9pqlki56dvp3bn64nais8ndes@developer.gserviceaccount.com";
private string keyPassword = "notasecret";
private string websiteCode = "67881935";
private AnalyticsService service = null;
private List<ChartRecord> visitsData = new List<ChartRecord>();
public MainForm()
{
InitializeComponent();
}
private void Authenticate()
{
//loading the Key file
var certificate = new X509Certificate2(keyFilePath, keyPassword, X509KeyStorageFlags.Exportable);
var scopes =
new string[] {
AnalyticsService.Scope.Analytics, // view and manage your analytics data
AnalyticsService.Scope.AnalyticsEdit, // edit management actives
AnalyticsService.Scope.AnalyticsManageUsers, // manage users
AnalyticsService.Scope.AnalyticsReadonly}; // View analytics data
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
}
private void QueryData()
{
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(
"ga:" + websiteCode,
DateTime.Today.AddDays(-15).ToString("yyyy-MM-dd"),
DateTime.Today.ToString("yyyy-MM-dd"),
"ga:sessions");
request.Dimensions = "ga:year,ga:month,ga:day";
var data = request.Execute();
foreach (var row in data.Rows)
{
visitsData.Add(new ChartRecord(new DateTime(int.Parse(row[0]), int.Parse(row[1]), int.Parse(row[2])).ToString("MM-dd-yyyy"), int.Parse(row[3])));
}
}
private void PopulateChart()
{
analyticsChart.Series[0].XValueMember = "Date";
analyticsChart.Series[0].YValueMembers = "Visits";
analyticsChart.DataSource = visitsData;
analyticsChart.DataBind();
}
private void btnData_Click(object sender, EventArgs e)
{
Authenticate();
QueryData();
PopulateChart();
}
}
}
更新:使用下面的回答代码,我得到类似
的错误'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\GoogleAnalytics2.vshost.exe'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Cannot find or open the PDB file.
The thread 0x3714 has exited with code 0 (0x0).
The thread 0x398c has exited with code 0 (0x0).
The thread 0x289c has exited with code 0 (0x0).
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\GoogleAnalytics2.exe'. Symbols loaded.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\Google.Apis.AnalyticsReporting.v4.dll'. Symbols loaded.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\Google.Apis.dll'. Symbols loaded.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\Google.Apis.Core.dll'. Symbols loaded.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\Google.Apis.Auth.PlatformServices.dll'. Symbols loaded.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\Google.Apis.Auth.dll'. Symbols loaded.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Cannot find or open the PDB file.
'GoogleAnalytics2.vshost.exe' (CLR v4.0.30319: GoogleAnalytics2.vshost.exe): Loaded 'c:\users\star\documents\visual studio 2015\Projects\GoogleAnalytics2\GoogleAnalytics2\bin\Release\Newtonsoft.Json.dll'. Cannot find or open the PDB file.
The thread 0x1228 has exited with code 0 (0x0).
The thread 0x30cc has exited with code 0 (0x0).
The program '[10788] GoogleAnalytics2.vshost.exe' has exited with code 0 (0x0).
The program '[10788] GoogleAnalytics2.vshost.exe: Program Trace' has exited with code 0 (0x0).
我的 program.cs 文件在哪里
namespace GoogleAnalytics2
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(ServiceAccountAuthExample.AuthenticateServiceAccount("analyticsdemo@ultimate-triode-151909.iam.gserviceaccount.com", "C:/Users/star/Downloads/CSharpApplication-9f9f557643f6.p12"));
}
}
}
请帮助 this.What 我在这里失踪了。感谢任何帮助。
我以前见过 P12 文件的问题。我还没有能够完全诊断出这个问题。有时下载服务帐户 .json 文件更容易。 Google 正在调用 p12 向后兼容,因此他们最终可能会停止使用 p12 文件。
这是我用于验证服务帐户的方法。它会自行判断您发送的凭证文件类型。
/// <summary>
/// Authenticating to Google using a Service account
/// Documentation: https://developers.google.com/accounts/docs/OAuth2#serviceaccount
/// </summary>
/// <param name="serviceAccountEmail">From Google Developer console https://console.developers.google.com</param>
/// <param name="serviceAccountCredentialFilePath">Location of the .p12 or Json Service account key file downloaded from Google Developer console https://console.developers.google.com</param>
/// <returns>AnalyticsService used to make requests against the Analytics API</returns>
public static AnalyticsService AuthenticateServiceAccount(string serviceAccountEmail, string serviceAccountCredentialFilePath)
{
try
{
if (string.IsNullOrEmpty(serviceAccountCredentialFilePath))
throw new Exception("Path to the service account credentials file is required.");
if (!File.Exists(serviceAccountCredentialFilePath))
throw new Exception("The service account credentials file does not exist at: " + serviceAccountCredentialFilePath);
if (string.IsNullOrEmpty(serviceAccountEmail))
throw new Exception("ServiceAccountEmail is required.");
// These are the scopes of permissions you need. It is best to request only what you need and not all of them
string[] scopes = new string[] { AnalyticsReportingService.Scope.Analytics }; // View your Google Analytics data
// For Json file
if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".json")
{
GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(scopes);
}
// Create the Analytics service.
return new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Analytics Authentication Sample",
});
}
else if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".p12")
{ // If its a P12 file
var certificate = new X509Certificate2(serviceAccountCredentialFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
// Create the Analytics service.
return new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Analytics Authentication Sample",
});
}
else
{
throw new Exception("Unsupported Service accounts credentials.");
}
}
catch (Exception ex)
{
Console.WriteLine("Create service account AnalyticsService failed" + ex.Message);
throw new Exception("CreateServiceAccountAnalyticsServiceFailed", ex);
}
}
从我的教程中窃取的代码Google Service accounts with Json file
用法:
var service = AnalyticsService AuthenticateServiceAccount(.....);
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(
"ga:" + websiteCode,
DateTime.Today.AddDays(-15).ToString("yyyy-MM-dd"),
DateTime.Today.ToString("yyyy-MM-dd"),
"ga:sessions");
request.Dimensions = "ga:year,ga:month,ga:day";
var data = request.Execute();
foreach (var row in data.Rows)
{
visitsData.Add(new ChartRecord(new DateTime(int.Parse(row[0]), int.Parse(row[1]), int.Parse(row[2])).ToString("MM-dd-yyyy"), int.Parse(row[3])));
}