我正在使用GitHub中的示例code。
#region code
AnalyticsService service;
// Service account Authentication
String SERVICE_ACCOUNT_EMAIL =" ";// removed the account email
string SERVICE_ACCOUNT_KEYFILE = @"C:\Projects\Test\TMBWebsite-7f016e7c3562.p12";
service = DaimtoAnalyticsAuthenticationHelper.AuthenticateServiceAccount(SERVICE_ACCOUNT_EMAIL,SERVICE_ACCOUNT_KEYFILE);
//////Get account summary and display them.
foreach (AccountSummary account in DaimtoAnaltyicsManagmentHelper.AccountSummaryList(service).Items)
{
// Account
Console.WriteLine("Account: " + account.Name + "(" + account.Id + ")");
foreach (WebPropertySummary wp in account.WebProperties)
{
// Web Properties within that account
Console.WriteLine("\tWeb Property: " + wp.Name + "(" + wp.Id + ")");
//Don't forget to check its not null. Believe it or not it could be.
if (wp.Profiles != null)
{
foreach (ProfileSummary profile in wp.Profiles)
{
// Profiles with in that web property.
Console.WriteLine("\t\tProfile: " + profile.Name + "(" + profile.Id + ")");
}
}
}
}
#endregion
//验证服务
if (!File.Exists(keyFilePath))
{
Console.WriteLine("An Error occurred - Key file does not exist");
return null;
}
string[] 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
X509Certificate2 certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
// X509Certificate2 certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
try
{
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
// Create the service.
AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Analytics API Sample",
});
return service;
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
return null;
}
#region帐户摘要
/// <summary>
/// Lists account summaries (lightweight tree comprised of accounts/properties/profiles) to which the user has access.
/// Documentation: https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/accountSummaries/list
/// </summary>
/// <param name="service">Valid authenticated Analytics Service</param>
/// <returns>List of Account Summaries resource - https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/accountSummaries</returns>
public static AccountSummaries AccountSummaryList(AnalyticsService service)
{
//List all of the activities in the specified collection for the current user.
// Documentation: https://developers.google.com/+/api/latest/activities/list
//ManagementResource.AccountSummariesResource.ListRequest list = service.Management.AccountSummaries.List();
ManagementResource.AccountSummariesResource.ListRequest list = service.Management.AccountSummaries.List();
list.Alt = ManagementResource.AccountSummariesResource.ListRequest.AltEnum.Json;
list.MaxResults = 1000; // Maximum number of Account Summaries to return per request.
AccountSummaries feed = list.Execute();
List<AccountSummary> allRows = new List<AccountSummary>();
//var getRequest = settingsService.Groups.Get("samplegroup@example.com");
//getRequest.Alt = "json";
//var settings = getRequest.Execute();
//// Loop through until we arrive at an empty page
while (feed.Items != null)
{
allRows.AddRange(feed.Items);
// We will know we are on the last page when the next page token is
// null.
// If this is the case, break.
if (feed.NextLink == null)
{
break;
}
// Prepare the next page of results
list.StartIndex = feed.StartIndex + list.MaxResults;
// Execute and process the next page request
feed = list.Execute();
}
feed.Items = allRows;
return feed;
}
#endregion
AccountSummaries feed = list.Execute(); 抛出异常:{“解析值时遇到意外字符:&lt;。路径'',第0行,位置0.”}在google analytics api中
答案 0 :(得分:2)
我明白了。防火墙阻止了我的request.Added在我的配置文件中给出了代码,并且工作正常
<system.net> <defaultProxy useDefaultCredentials="true"> <proxy proxyaddress="My proxy address here" usesystemdefault="True"/> </defaultProxy> </system.net>