google search console api的发现文档(discovery document)是客户端库用来了解api结构、方法和参数的关键元数据文件。google已宣布对search console api的发现文档进行强制性更新,并设定了截止日期。未能在指定日期前更新其发现文档的项目将不再受支持,并可能停止正常工作。这意味着,如果您的应用程序依赖于旧版的search console api发现文档(例如,通过webmasters/v3服务),您必须及时进行迁移,以确保服务持续可用。
本次更新主要涉及两个关键方面的变更:发现文档的URL以及API服务名称和版本。
旧版Search Console API的发现文档URL为: https://www.googleapis.com/discovery/v1/apis/webmasters/v3/rest
新版Search Console API的发现文档URL已更新为: https://searchconsole.googleapis.com/$discovery/rest
尽管大多数官方客户端库会内部处理发现文档的获取,但了解此变更有助于问题排查和理解底层机制。
这是在代码层面最直接的体现。API的服务名称从webmasters变更为searchconsole,版本从v3变更为v1。
根据您使用的Google API客户端库类型,具体的迁移步骤会有所不同。以下将分别针对PHP和JavaScript客户端库提供指导。
对于使用google-api-php-client库的PHP应用程序,核心任务是确保您的库版本足够新,并且代码中引用的服务类名称已相应更新。
步骤一:更新客户端库
首先,请确保您的google-api-php-client库是最新版本。这通常通过Composer完成:
composer update google/apiclient
更新库后,它将能够获取并使用最新的发现文档。
步骤二:调整服务及请求对象实例化
在旧版中,您可能实例化Google_Service_Webmasters及其相关的请求对象。更新后,这些类名需要更改为Google_Service_SearchConsole。
旧版PHP代码示例:
// 实例化Google客户端 $client = new Google_Client(); $client->setApplicationName('Your Application Name'); $client->setAuthConfig('path/to/your/credentials.json'); $scopesArray = array('https://www.googleapis.com/auth/webmasters'); $client->setScopes($scopesArray); // 实例化Webmasters服务 $service = new Google_Service_Webmasters($client); // 实例化SearchAnalyticsQueryRequest $searchAnalyticsQueryRequest = new \Google_Service_Webmasters_SearchAnalyticsQueryRequest(); $searchAnalyticsQueryRequest->setStartDate('2023-01-01'); $searchAnalyticsQueryRequest->setEndDate('2023-01-31'); $searchAnalyticsQueryRequest->setDimensions(['page', 'date']); $searchAnalyticsQueryRequest->setSearchType('web'); // 执行查询 (示例) // $queryResponse = $service->searchanalytics->query('https://example.com/', $searchAnalyticsQueryRequest);
新版PHP代码示例:
更新库后,您需要将代码中的Webmasters替换为SearchConsole。
// 实例化Google客户端 (保持不变) $client = new Google_Client(); $client->setApplicationName('Your Application Name'); $client->setAuthConfig('path/to/your/credentials.json'); // 作用域可能保持不变,或者根据具体API需求调整 $scopesArray = array('https://www.googleapis.com/auth/webmasters'); // 注意:此作用域仍然有效,对应Search Console API $client->setScopes($scopesArray); // 实例化SearchConsole服务 // 注意:类名从 Google_Service_Webmasters 变为 Google_Service_SearchConsole $service = new Google_Service_SearchConsole($client); // 实例化SearchAnalyticsQueryRequest // 注意:类名从 Google_Service_Webmasters_SearchAnalyticsQueryRequest 变为 Google_Service_SearchConsole_SearchAnalyticsQueryRequest $searchAnalyticsQueryRequest = new \Google_Service_SearchConsole_SearchAnalyticsQueryRequest(); $searchAnalyticsQueryRequest->setStartDate('2023-01-01'); $searchAnalyticsQueryRequest->setEndDate('2023-01-31'); $searchAnalyticsQueryRequest->setDimensions(['page', 'date']); $searchAnalyticsQueryRequest->setSearchType('web'); // 执行查询 (示例) // $queryResponse = $service->searchanalytics->query('https://example.com/', $searchAnalyticsQueryRequest);
重要提示: 尽管API服务名称发生了变化,但许多情况下,API的作用域(Scope)可能保持不变,例如https://www.googleapis.com/auth/webmasters仍然是Search Console API的有效作用域。请务必查阅最新的Google Search Console API官方文档以确认。
对于使用Google JavaScript客户端库(gapi.client)的应用程序,主要的修改点在于API的加载方法。
旧版JavaScript代码示例:
gapi.client.load('webmasters', 'v3').then(function() { // API加载成功后的操作 // 例如:gapi.client.webmasters.searchanalytics.query(...) });
新版JavaScript代码示例:
您需要将gapi.client.load()调用中的服务名称和版本进行替换。
gapi.client.load('searchconsole', 'v1').then(function() { // API加载成功后的操作 // 现在应使用 gapi.client.searchconsole.searchanalytics.query(...) });
Google Search Console API发现文档的更新是一项强制性要求,旨在确保API服务的稳定性和现代化。通过将旧版webmasters/v3服务迁移到新版searchconsole/v1,并相应地更新您的客户端库和代码,您可以确保应用程序能够持续、稳定地访问Google Search Console数据。务必遵循本文提供的步骤,并进行充分的测试,以保障您的应用程序平稳过渡。
以上就是应对Google Search Console API发现文档更新:迁移指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号