Dal*_*ngh 2 c# oauth google-webmaster-tools google-oauth google-api-dotnet-client
我正在使用以下代码将站点地图提交给网站管理员工具
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);
Run Code Online (Sandbox Code Playgroud)
但是这段代码没有运气。任何人都可以提供一些示例代码来提交站点地图吗?
从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;
}
}
Run Code Online (Sandbox Code Playgroud)
调用身份验证方法
string CLIENT_ID = "xxx-d0vpdthl4ms0soutcrpe036ckqn7rfpn.apps.googleusercontent.com";
string CLIENT_SECRET = "NDmluNfTgUk6wgmy7cFo64RV";
var service = AuthenticateOauth(CLIENT_ID, CLIENT_SECRET, "TEST");
Run Code Online (Sandbox Code Playgroud)
获取数据
var x = service.Urlcrawlerrorscounts.Query(site).Execute();
Run Code Online (Sandbox Code Playgroud)
该示例项目有站点地图,以及一些辅助方法
| 归档时间: |
|
| 查看次数: |
718 次 |
| 最近记录: |