使用C#提交站点地图

时间:2015-06-25 13:26:48

标签: c# google-oauth google-webmaster-tools google-api-dotnet-client

我正在使用follwoign代码将网站地图提交给网站管理员工具

Google.GData.WebmasterTools.WebmasterToolsService service = 
    new Google.GData.WebmasterTools.WebmasterToolsService("www.test1.com");
service.setUserCredentials("email", "password");
String lWebsite = "http%3A%2F%2Fwww%2Etest1%2Ecom%2F";
query.Uri = new Uri("https://www.google.com/webmasters/tools/feeds/sites/");

Google.GData.WebmasterTools.SitemapsEntry se = 
    new Google.GData.WebmasterTools.SitemapsEntry();
se.Content.Src = "http://www.test1.com/Sitemap.xml";
se.Content.Type = "text/xml";
Google.GData.WebmasterTools.SitemapsEntry ret = 
    service.Insert(
        new Uri("https://www.google.com/webmasters/tools/feeds/sites/" + lWebsite + "/sitemaps/"), se);

但是这段代码没有运气。任何人都可以提供一些示例代码来提交站点地图吗?

1 个答案:

答案 0 :(得分:3)

Google-webmastertools示例

中删除的代码
  

PM>安装包Google.Apis.Webmasters.v3

使用Oauth2进行身份验证:

/// <summary>
        /// Authenticate to Google Using Oauth2
        /// Documentation https://developers.google.com/accounts/docs/OAuth2
        /// </summary>
        /// <param name="clientId">From Google Developer console https://console.developers.google.com</param>
        /// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param>
        /// <param name="userName">A string used to identify a user.</param>
        /// <returns></returns>
        public static WebmastersService  AuthenticateOauth(string clientId, string clientSecret, string userName)
        {

            string[] scopes = new string[] { WebmastersService.Scope.WebmastersReadonly};     // 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("Daimto.GoogleWebMasters.Auth.Store")).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;

            }

        }

致电身份验证方法

string CLIENT_ID = "xxx-d0vpdthl4ms0soutcrpe036ckqn7rfpn.apps.googleusercontent.com";
            string CLIENT_SECRET = "NDmluNfTgUk6wgmy7cFo64RV";
            var service = AuthenticateOauth(CLIENT_ID, CLIENT_SECRET, "TEST");

获取数据

   var x =  service.Urlcrawlerrorscounts.Query(site).Execute();

sample project还有一些辅助方法用于站点地图