【问题标题】:Google Analytics API exception in C#C# 中的 Google Analytics API 异常
【发布时间】:2014-12-03 16:56:01
【问题描述】:

我是 Google Analytics 的新手,并尝试通过我在网上找到的示例访问分析数据。

它抛出以下异常:

Google.GData.Client.ClientFeedException: Parsing failed ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
  at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
  at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
  at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
  at Google.GData.Client.BaseFeedParser.MoveToStartElement(XmlReader reader)
  at Google.GData.Client.AtomFeedParser.Parse(Stream streamInput, AtomFeed feed)
  --- End of inner exception stack trace ---
  at Google.GData.Client.AtomFeedParser.Parse(Stream streamInput, AtomFeed feed)
  at Google.GData.Client.AtomFeed.Parse(Stream stream, AlternativeFormat format)
  at Google.GData.Client.Service.CreateAndParseFeed(Stream inputStream, Uri uriToUse)
  at Google.GData.Client.Service.Query(FeedQuery feedQuery)
  at Google.GData.Analytics.AnalyticsService.Query(DataQuery feedQuery)
  at GoogleAnalytics.Program.Main(String[] args) in e:\\Code\\GoogleAnalytics\\GoogleAnalytics\\Program.cs:line 40

下面是代码。如果可能的话,有人可以帮助我吗?我找不到有关如何执行此操作的适当文档。提前感谢您的帮助。

string username = "xxx@gmail.com";
string pass = "Password";
string gkey = "?key=<<key>>";

string dataFeedUrl = "https://www.googleapis.com/auth/analytics.readonly/" + gkey;


AnalyticsService service = new AnalyticsService("WebApp");
service.setUserCredentials(username, pass);

DataQuery query1 = new DataQuery(dataFeedUrl);

query1.Ids = "ga:1235466";
query1.Metrics = "ga:visits";
query1.Sort = "ga:visits";
query1.Dimensions = "ga:dimension1";

query1.GAStartDate = new DateTime(2012, 1, 2).ToString("yyyy-MM-dd");
query1.GAEndDate = DateTime.Now.ToString("yyyy-MM-dd");
query1.StartIndex = 1;

DataFeed dataFeedVisits = service.Query(query1);

foreach (DataEntry entry in dataFeedVisits.Entries)
{
    string st = entry.Title.Text;
    string ss = entry.Metrics[0].Value;                   
}

【问题讨论】:

    标签: c# google-analytics-api google-api-dotnet-client


    【解决方案1】:

    首先,我建议您使用 Google.Apis.v3 库。它们让您的生活更轻松。

    您应该使用 Oauth2 来验证访问权限,而不是用户名和密码。在 Google Apis Console 创建应用程序后,您会收到一个客户端 ID 和一个客户端密码,这些用于向 Google 识别您的应用程序。 oauth2 code 看起来像这样。

    UserCredential credential; 
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
              new ClientSecrets { ClientId = "YourClientId", ClientSecret = "YourClientSecret" },
              new[] { AnalyticsService.Scope.AnalyticsReadonly },
              "user",
              CancellationToken.None,
              new FileDataStore("Drive.Auth.Store")).Result; }
    

    上面的代码会询问用户是否允许您访问 Google 分析数据。

    一旦您拥有 UserCredential,您就可以创建用于所有对 API 的请求的服务。

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

    您可以找到关于大多数调用 Google Analtyics api here的教程


    注意:如果您只访问自己的数据,您应该查看Service Accounts

    【讨论】:

    • 感谢 DalmTo :) 根据您的建议,我尝试使用 Google API V3 库,与我使用的库相比,它非常好。再次感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    相关资源
    最近更新 更多