代码之家  ›  专栏  ›  技术社区  ›  Paul Sanders

.Net与Webmaster Tools(Google.Apis.Webmasters.v3 Nuget包)斗争

  •  5
  • Paul Sanders  · 技术社区  · 9 年前

    我想知道是否有人比我更喜欢通过.Net使用Google站长工具API的新搜索分析功能?

    我正在使用Google.Apis.Webmasters。v3 Nuget软件包,并已完成身份验证和连接(使用服务帐户)

    然而,我很难在搜索分析方面取得进展。

    我在网上找不到任何代码示例,因此已在 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方法。异常详细信息如下。。。

    新泽西州纽顿市。JsonReaderException{“分析NaN值时出错。路径“”,行0,位置0。”}

    任何帮助或代码示例都将非常感激!

    1 回复  |  直到 9 年前
        1
  •  4
  •   Linda Lawton - DaImTo    9 年前

    这会编译,但不会为我返回任何数据。

    授权:

     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进行测试 page 。它也不会返回任何内容。

    奇怪的API。

    更新:

    我终于拿回了数据,我把日期设定为

    身体开始日期=“2015-09-01”;
    身体结束日期=“2015-09-15”;

    我想知道这件事的数据是否有限,它只能追溯到目前为止。

    enter image description here