即使我的搜索控制台中有多个站点,Search Console API也会返回站点列表的空数组(Search Console API returns Empty array for site list even though I have several sites in my search console)

即使我的搜索控制台中有多个站点,Search Console API也会返回站点列表的空数组。

我的搜索控制台中有几个站点。 当我使用api Explorer时,它们会在数组中返回。 一旦我使用PHP或Python客户端库,使用我的服务帐户凭据获得一个空结果。 这似乎是一个权限问题,但我使用的服务帐户具有分配给它的所有者角色权限,所以这没有意义。 任何帮助,将不胜感激!

这是我的代码:

$client = new Google_Client(); $credentials_file = 'path/to/file.json'; $client->setAuthConfig($credentials_file); $client->setScopes(['https://www.googleapis.com/auth/webmasters']); $service = new Google_Service_Webmasters($client); var_dump($service->sites->listSites()->getSiteEntry());

我的结果:array(0){}即使它在搜索控制台中有几个网站。

Search Console API returns Empty array for site list even though I have several sites in my search console.

I have several sites in my search console. When I use the api Explorer, they are returned in the array. Once I use the PHP or Python client library, get an empty result with my Service Account Credentials. This seems to be a permissions issue but the service account I am using has owner role permissions assigned to it, so that doesn't make sense. Any help would be appreciated!

This is my code:

$client = new Google_Client(); $credentials_file = 'path/to/file.json'; $client->setAuthConfig($credentials_file); $client->setScopes(['https://www.googleapis.com/auth/webmasters']); $service = new Google_Service_Webmasters($client); var_dump($service->sites->listSites()->getSiteEntry());

My result: array(0) {} even though it have several websites in the search console.

最满意答案

搜索控制台站点和站点数据被视为用户数据,服务帐户无法访问。 您必须创建一个Oauth 2接口并将令牌保存到文件中并在您的应用中使用该令牌。 确保应用程序在到期时刷新令牌:

if ($client->isAccessTokenExpired()) { $client->refreshToken($this->token['refresh_token']); $token = $client->getAccessToken(); // save this to a file }

Search console sites and site data is considered user data and cannot be accessed by service accounts. You have to make an Oauth 2 interface and save the token to a file and use that token in your app. Make sure the app refreshes the token when it expires:

if ($client->isAccessTokenExpired()) { $client->refreshToken($this->token['refresh_token']); $token = $client->getAccessToken(); // save this to a file }

更多推荐