5

我想知道是否有人通过 .Net 使用 Google Webmaster Tools API 的新搜索分析功能比我更进一步?

我正在使用 Google.Apis.Webmasters.v3 Nuget 包,并且已经进行了身份验证和连接(使用服务帐户)

但是,我很难使用 Search Analytics 去任何地方。

我在网上找不到任何代码示例,因此由https://developers.google.com/resources/api-libraries/documentation/webmasters/v3/csharp/latest/annotated.html上的类信息和很多的猜测。

这是我正在使用的代码:

SearchanalyticsResource mySearchanalyticsResource = new SearchanalyticsResource(service);

SearchAnalyticsQueryRequest myRequest = new SearchAnalyticsQueryRequest(); 
myRequest.StartDate = "2015-08-01"; 
myRequest.EndDate = "2015-08-31"; 
myRequest.RowLimit = 10;

SearchanalyticsResource.QueryRequest myQueryRequest = mySearchanalyticsResource.Query(myRequest, site.SiteUrl); 
SearchAnalyticsQueryResponse myQueryResponse = myQueryRequest.Execute();

当我得到“发生错误,但错误响应无法反序列化”时,它运行正常,直到 Execute 方法。下面的异常详细信息...

Newtonsoft.Json.JsonReaderException {“解析 NaN 值时出错。路径 '',第 0 行,位置 0。”}

任何帮助或代码示例将不胜感激!

4

1 回答 1

4

这可以编译,但它没有为我返回任何数据。

授权:

 public static WebmastersService WMAuthenticateOauth(string clientId, string clientSecret, string userName)
        {

            string[] scopes = new string[] { WebmastersService.Scope.Webmasters };     // View analytics data

            try
            {
                // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                             , scopes
                                                                                             , userName
                                                                                             , CancellationToken.None
                                                                                             , new FileDataStore(".", true)).Result;

                WebmastersService service = new WebmastersService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "WebMasters API Sample",
                });
                return service;
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.InnerException);
                return null;

            }

        }

要求

var service = Authentcation.WMAuthenticateOauth(clientid, secret, "testmmm");


   IList<string> newlist = new List<string> ();
   newlist.Add("country");
   newlist.Add("device");

   SearchAnalyticsQueryRequest body = new SearchAnalyticsQueryRequest();
   body.StartDate = "2015-04-01";
   body.EndDate = "2015-05-01";
   body.Dimensions = newlist;

   var result = service.Searchanalytics.Query(body, "http://www.daimto.com/").Execute();

我还尝试使用此页面底部的 try me 进行测试。它也不返回任何东西。

奇怪的 API 这个。

更新:

我终于找回了数据我将日期设置为

body.StartDate = "2015-09-01";
body.EndDate = "2015-09-15";

我想知道这东西是不是数据有限,它只能追溯到到目前为止。

在此处输入图像描述

于 2015-09-16T13:29:08.120 回答