URL Inspection APIは、1分間に600回の呼び出し、1日に2,000回の呼び出し制限があるため、
調査するURL数が多い状況で実運用で利用する場合は、バッチプログラムで構築しURLをローテーションしながら呼び出す必要があります。
Install-Package Google.Apis.SearchConsole.v1 -Version 1.57.0.2792
using Google.Apis.Auth.OAuth2;
using Google.Apis.SearchConsole.v1.Data;
using Google.Apis.SearchConsole.v1;
using Google.Apis.Services;
using System.Text;
namespace SimpleUrlInspectionAPICall
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string credentialsPath = "api-project-604973771331-561b5886ff93.json";
string searchConsoleUrl = "sc-domain:(Google Search Consoleで取得するドメイン名)";
FileStream stream = new FileStream(credentialsPath, FileMode.Open);
GoogleCredential credentials = GoogleCredential.FromStream(stream);
if (credentials.IsCreateScopedRequired) {
credentials = credentials.CreateScoped(new string[] { SearchConsoleService.Scope.Webmasters });
}
SearchConsoleService service = new SearchConsoleService(new BaseClientService.Initializer() {
HttpClientInitializer = credentials,
ApplicationName = "Demo App"
});
InspectUrlIndexRequest iuir = new InspectUrlIndexRequest();
iuir.InspectionUrl = textBox2.Text;
iuir.SiteUrl = "sc-domain:(取得したいドメイン)";
iuir.LanguageCode = "ja-JP";
InspectUrlIndexResponse resp = service.UrlInspection.Index.Inspect(iuir).Execute();
StringBuilder sb = new StringBuilder();
sb.Append(string.Format("Verdict: {0}", resp.InspectionResult.IndexStatusResult.Verdict));
sb.Append("\r\n");
sb.Append(string.Format("CoverageState: {0}", resp.InspectionResult.IndexStatusResult.CoverageState));
sb.Append("\r\n");
sb.Append(string.Format("RobotsTxtState: {0}", resp.InspectionResult.IndexStatusResult.RobotsTxtState));
sb.Append("\r\n");
sb.Append(string.Format("IndexingState: {0}", resp.InspectionResult.IndexStatusResult.IndexingState));
sb.Append("\r\n");
if (resp.InspectionResult.IndexStatusResult.LastCrawlTime != null) {
DateTime LastCrawl = (DateTime)resp.InspectionResult.IndexStatusResult.LastCrawlTime;
sb.Append("LastCrawlTime: " + LastCrawl.ToString("yyyy/MM/dd HH:mm:ss"));
}
else {
sb.Append("LastCrawlTime: none");
}
sb.Append(string.Format("PageFetchState: {0}", resp.InspectionResult.IndexStatusResult.PageFetchState));
sb.Append("\r\n");
sb.Append(string.Format("GoogleCanonical: {0}", resp.InspectionResult.IndexStatusResult.GoogleCanonical));
sb.Append("\r\n");
sb.Append(string.Format("UserCanonical: {0}", resp.InspectionResult.IndexStatusResult.UserCanonical));
sb.Append("\r\n");
if (resp.InspectionResult.IndexStatusResult.Sitemap != null) {
int smapCount = resp.InspectionResult.IndexStatusResult.Sitemap.Count;
for (int i = 0; i < smapCount; i++) {
sb.Append(string.Format("Sitemap {0:d}: {1}", i, resp.InspectionResult.IndexStatusResult.Sitemap[i]));
}
}
else {
sb.Append("Sitemap: none");
}
sb.Append("\r\n");
if (resp.InspectionResult.IndexStatusResult.ReferringUrls != null){
int refCount = resp.InspectionResult.IndexStatusResult.ReferringUrls.Count;
sb.Append(string.Format("Reffering Urls: {0:d}", refCount));
sb.Append("\r\n");
for (int i = 0; i < refCount; i++) {
sb.Append(string.Format("Reffering Url {0:d}: {1}", i, resp.InspectionResult.IndexStatusResult.ReferringUrls[i]));
sb.Append("\r\n");
}
}
else {
sb.Append("Reffering Urls: none");
}
sb.Append(string.Format("CrawledAs: {0:d}", resp.InspectionResult.IndexStatusResult.CrawledAs));
sb.Append("\r\n");
textBox1.Text += sb.ToString();
}
}
}
string credentialsPath = "api-project-604973771331-561b5886ff93.json";
string searchConsoleUrl = "sc-domain:(Google Search Consoleで取得するドメイン名)";
FileStream stream = new FileStream(credentialsPath, FileMode.Open);
GoogleCredential credentials = GoogleCredential.FromStream(stream);
if (credentials.IsCreateScopedRequired) {
credentials = credentials.CreateScoped(new string[] { SearchConsoleService.Scope.Webmasters });
}
SearchConsoleService service = new SearchConsoleService(new BaseClientService.Initializer() {
HttpClientInitializer = credentials,
ApplicationName = "Demo App"
});
InspectUrlIndexRequest iuir = new InspectUrlIndexRequest();
iuir.InspectionUrl = textBox2.Text;
iuir.SiteUrl = "sc-domain:(取得したいドメイン)";
iuir.LanguageCode = "ja-JP";
InspectUrlIndexResponse resp = service.UrlInspection.Index.Inspect(iuir).Execute();
StringBuilder sb = new StringBuilder();
sb.Append(string.Format("Verdict: {0}", resp.InspectionResult.IndexStatusResult.Verdict));
sb.Append("\r\n");
sb.Append(string.Format("CoverageState: {0}", resp.InspectionResult.IndexStatusResult.CoverageState));
sb.Append("\r\n");
sb.Append(string.Format("RobotsTxtState: {0}", resp.InspectionResult.IndexStatusResult.RobotsTxtState));
sb.Append("\r\n");
sb.Append(string.Format("IndexingState: {0}", resp.InspectionResult.IndexStatusResult.IndexingState));
sb.Append("\r\n");
if (resp.InspectionResult.IndexStatusResult.LastCrawlTime != null) {
DateTime LastCrawl = (DateTime)resp.InspectionResult.IndexStatusResult.LastCrawlTime;
sb.Append("LastCrawlTime: " + LastCrawl.ToString("yyyy/MM/dd HH:mm:ss"));
}
else {
sb.Append("LastCrawlTime: none");
}
sb.Append(string.Format("PageFetchState: {0}", resp.InspectionResult.IndexStatusResult.PageFetchState));
sb.Append("\r\n");
sb.Append(string.Format("GoogleCanonical: {0}", resp.InspectionResult.IndexStatusResult.GoogleCanonical));
sb.Append("\r\n");
sb.Append(string.Format("UserCanonical: {0}", resp.InspectionResult.IndexStatusResult.UserCanonical));
sb.Append("\r\n");
if (resp.InspectionResult.IndexStatusResult.Sitemap != null) {
int smapCount = resp.InspectionResult.IndexStatusResult.Sitemap.Count;
for (int i = 0; i < smapCount; i++) {
sb.Append(string.Format("Sitemap {0:d}: {1}", i, resp.InspectionResult.IndexStatusResult.Sitemap[i]));
}
}
else {
sb.Append("Sitemap: none");
}
sb.Append("\r\n");
if (resp.InspectionResult.IndexStatusResult.ReferringUrls != null){
int refCount = resp.InspectionResult.IndexStatusResult.ReferringUrls.Count;
sb.Append(string.Format("Reffering Urls: {0:d}", refCount));
sb.Append("\r\n");
for (int i = 0; i < refCount; i++) {
sb.Append(string.Format("Reffering Url {0:d}: {1}", i, resp.InspectionResult.IndexStatusResult.ReferringUrls[i]));
sb.Append("\r\n");
}
}
else {
sb.Append("Reffering Urls: none");
}
sb.Append(string.Format("CrawledAs: {0:d}", resp.InspectionResult.IndexStatusResult.CrawledAs));
sb.Append("\r\n");
textBox1.Text += sb.ToString();