在 C# 中使用 Search Console API 从 google Search Console 检索信息

Retrieve information from google search console using Search Console APIs in C#

我能够成功地从 Google 分析报告 API 中检索详细信息,但是当我尝试从 Google 搜索控制台访问网站(属性)详细信息时在 C# 中使用 Search Console APIs,它不会检索任何信息。基本上我需要获取网站列表(用户的 Search Console 网站)和每个关键字的访问者。 我正在使用 Google Webmaster Tool APIs Search Console API v3。在 APIs Expolorer 中,它检索站点列表 (webmasters.sites.list)。当我尝试在 C# 控制台应用程序中实现相同的方法时,它不会获取任何数据。请检查下面的代码和附图。

 GoogleCredential credentials;
        using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
        {
            string[] scopes = {
                //AnalyticsService.Scope.Analytics, // view and manage your Google Analytics data
                //AnalyticsService.Scope.AnalyticsEdit, // Edit and manage Google Analytics Account
                //AnalyticsService.Scope.AnalyticsManageUsers, // Edit and manage Google Analytics Users
                //AnalyticsReportingService.Scope.AnalyticsReadonly,
                 //AnalyticsReportingService.Scope.Analytics,
                WebmastersService.Scope.WebmastersReadonly,
                WebmastersService.Scope.Webmasters
           };

            var googleCredential = GoogleCredential.FromStream(stream);
            credentials = googleCredential.CreateScoped(scopes);

        }

 WebmastersService service1 = new WebmastersService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credentials
        });

        SitesResource.ListRequest res = service1.Sites.List();
        var sites = res.Execute();

我最终通过将具有完全权限的 google "service account" 添加到 google 所选 属性 搜索控制台来解决此问题。以下是步骤。

  1. 已在 google

  2. 中使用管理员帐户登录
  3. 转到搜索控制台站点 (https://search.google.com/u/0/search-console)

  4. Select左上角的属性“https://www.----.com
  5. "Settings" 在左侧的菜单中
  6. "Users and rights"
  7. 右上"Add user"
  8. 添加了我们的 google "SERVICE account" 并拥有全部权利。