0

即使我在搜索控制檯中有多個網站,Search Console API也會返回空數組。即使在我的搜索控制檯中有多個網站,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()); 

我的結果是:數組(0){} 即使它出現在搜索控制檯幾個網站。

回答

1

搜索控制檯站點和站點數據被視爲用戶數據,無法通過服務帳戶訪問。您必須創建一個Oauth 2界面並將該標記保存到文件中並在您的應用程序中使用該標記。確保應用程序在其到期時刷新令牌:

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