400使用Google.Apis.Analytics.v3时出现错误请求

时间:2013-09-23 04:59:13

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

我正在尝试连接到Google Analytics以检索一些信息并将其放入数据库,到目前为止,我的代码是:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

// Google API
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Analytics.v3;
using Google.Apis.Analytics.v3.Data;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Services;
using Google.Apis.Util;


namespace CCQ.GoogleToSharePoint
{
    class Program
    {
        static void Main(string[] args)
        {
            //This is the API url which we're storing to a string
            string scope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue();

            //For whatever reason, this is labelled wrong. It is the email address
            //that you have added as a user to your Analytics account

            string clientId = "<redacted>@gmail.com";


            //This is the physical path to the file we downloaded earlier
            //To demonstrate this, I've kept the full path to my key file.
            //Obviously, you will need to change this to match where you've
            //stored yours.
            string keyFile = @"<redacted>";

            //The password Google gives you, probably the same as the one below
            string keyPassword = "notasecret";


            //Store the authentication description
            AuthorizationServerDescription desc = GoogleAuthenticationServer.Description;

            //Create a certificate object to use when authenticating
            X509Certificate2 key = new X509Certificate2(keyFile, keyPassword, X509KeyStorageFlags.Exportable);

            //Now, we will log in and authenticate, passing in the description
            //and key from above, then setting the accountId and scope
            AssertionFlowClient client = new AssertionFlowClient(desc, key)
            {
                ServiceAccountId = clientId,
                Scope = scope
            };

            //Finally, complete the authentication process
            //NOTE: This is the first change from the update above
            OAuth2Authenticator<AssertionFlowClient> 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
            AnalyticsService gas = new AnalyticsService(new BaseClientService.Initializer() { Authenticator = auth });

            //Create our query
            //The Data.Ga.Get needs the parameters:
            //Analytics account id, starting with ga:
            //Start date in format YYYY-MM-DD
            //End date in format YYYY-MM-DD
            //A string specifying the metrics
            DataResource.GaResource.GetRequest r = gas.Data.Ga.Get("ga:<redacted>", "2013-09-09", "2013-09-23", "ga:visitors");

            //Specify some addition query parameters
            r.Dimensions = "ga:visitorType";
            r.Sort = "-ga:visitors";
            r.MaxResults = 5;

            //Execute and fetch the results of our query
            try
            {
                //Write the column headers
                GaData d = r.Execute();

                foreach (var h in d.ColumnHeaders)
                {
                    Console.WriteLine(h.Name);
                }

                //Write the data
                foreach (var row in d.Rows)
                {
                    Console.WriteLine(row[0] + " ------ " + row[1]);
                }
            }
            catch (ProtocolException webEx)
            {
                Console.WriteLine("Protocol Exception: {0}\n\n", webEx.ToString());
                Console.WriteLine("Stack Trace: {0}", webEx.StackTrace);

                throw;
            }

        }
    }
}

不幸的是我在执行行上抛出了一个错误,特别是这个错误:

//Write the column headers
GaData d = r.Execute();

错误本身是:

  

发送直接消息或收到回复时出错。

Google's API documentation表示将在以下条件下发送400:Bad Response标头:

  

400错误请求错误请求的类型包括:

     
      
  • 无效的维度和/或指标

  •   
  • 数量:无指标或维度/指标太多

  •   
  • 在过滤器上使用OR,其中一侧是公制,另一侧是维度

  •   
  • 无效的过滤器/细分语法

  •   
  • 非法维度/指标组合或有利的细分

  •   

然而,从我所看到的,我没有遇到任何这些 - 所以我总是困惑。我该如何进一步调试此问题?

此错误的堆栈跟踪是:

  

StackTrace“at   Google.Apis.Requests.ClientServiceRequest`1.Execute()in   C:\ code.google.com \谷歌API-DOTNET客户端\ default_3 \工具\ Google.Apis.Release \ BIN \调试\输出\ DEFAULT \ SRC \ GoogleApis \蜜蜂\请求\ ClientServiceRequest.cs:行   96 \ r \ n在CCQ.GoogleToSharePoint.Program.Main(String [] args)中   I:\ dev的\ CCQ.GoogleToSharePoint \ CCQ.GoogleToSharePoint \的Program.cs:行   121 \ r \ n在System.AppDomain._nExecuteAssembly(RuntimeAssembly   assembly,String [] args)\ r \ n at   System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence   assemblySecurity,String [] args)\ r \ n at   Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly(个)\ r \ n   在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)\ r \ n   在System.Threading.ExecutionContext.RunInternal(ExecutionContext   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)\ r \ n at   System.Threading.ExecutionContext.Run(执行上下文   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)\ r \ n at   System.Threading.ExecutionContext.Run(执行上下文   executionContext,ContextCallback回调,对象状态)\ r \ n at   System.Threading.ThreadHelper.ThreadStart()“string

1 个答案:

答案 0 :(得分:1)

我整个周末都遇到了同样的问题,我找到了解决方案,试试这个代码让我工作:)

log4net.Config.XmlConfigurator.Configure();

        const string ServiceAccountUser = "xxxxxxxxxx@developer.gserviceaccount.com";
        AssertionFlowClient client = new AssertionFlowClient(
            GoogleAuthenticationServer.Description, new X509Certificate2(@"C:\xxxxxxxxxxx-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);
        var authenticator = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);
        var service = new AnalyticsService(new BaseClientService.Initializer()
        {
            Authenticator = authenticator
        });


        string profileId = "ga:63494033";
        string startDate = "2010-10-01";
        string endDate = "2010-10-31";
        string metrics = "ga:visits";
        DataResource.GaResource.GetRequest request = service.Data.Ga.Get(profileId, startDate, endDate, metrics);
        request.Dimensions = "ga:date";
        GaData data = request.Execute();   
    }
相关问题