使用C#提交网站地图
本文关键字:地图 网站 提交 使用 | 更新日期: 2023-09-27 17:59:46
我正在使用以下代码向网站管理员工具提交网站地图
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);
但这个代码没用。有人能给我提供一些提交网站地图的样本代码吗?
从谷歌网站管理员工具中提取的代码示例
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();
示例项目有一些用于站点地图以及的辅助方法