2016-07-14 5 views
1

私はウェブマスターツールAPIに問題があります。スクリプトにユーザーの操作がなく、必要な情報を取得しようとしています。 Googleアナリティクスでサービスアカウントを使用していますが、これはウェブマスターツールではサポートされていません。ウェブマスターツールAPI

私はそれを返すAPIクエリしようとしたとき、私は現在、しかし、APIサーバキーを使用しようとしている:

コード403: しかし私

「ユーザーは、サイトの十分な権限がありません」私が照会しようとしているサイトの所有者です。 www.google.com/webmasters/tools/homeを確認し、サイトが確認されました。

私のコードは正しいですか?

$KEY_FILE_LOCATION = "privateKey.json"; 
$viewID = "view id"; //Website View ID - including ga: 
$scopes = array(
    "https://www.googleapis.com/auth/analytics.readonly", 
    "https://www.googleapis.com/auth/webmasters.readonly" 
); 

$emailAddr = 'Email'; //Got off google developer console 
$apiKey = 'API Server Key'; //Got off google developer console 

//Create Google Client 
$client = new Google_Client(); 
$client->setApplicationName('Logging in Attempt'); 
$client->setAuthConfig($KEY_FILE_LOCATION); 
$client->setScopes($scopes); 
$client->setAccessType('offline'); 
$client->setDeveloperKey($apiKey); 

//Creating Webmaster Service 
$webmastersService = new Google_Service_Webmasters($client); 

//Creating Request 
$request = new Google_Service_Webmasters_SearchAnalyticsQueryRequest(); 
$request->setStartDate('2016-07-01'); 
$request->setEndDate('2016-07-10'); 
$request->setDimensions(array('query')); 

$requestWebsite = 'Website'; //The website I have access to 

//Querying Webmaster 
$qsearch = $webmastersService->searchanalytics->query($requestWebsite, $request); 

$rows = $qsearch->getRows(); 

echo "<pre>",print_r($rows),"</pre>"; 
+0

http://stackoverflow.com/questions/28080416/webmasters-api-user-does-not-have-sufficient-permission-for-siteが問題を解決していますか? – Gordon

+0

私はこれを試しました.Googleデベロッパーコンソールからのメールは、ビューが存在するプロパティに追加されました。 – MrHappySandwich

答えて

0

私が間違っていた箇所を発見しました。コードはうまくいきました。問題は検索コンソール上の問題でした。ウェブサイトは確認されましたが、サービスアカウントのメールアドレスをウェブサイトに追加する必要があり、アクセスを制限しました。それがそこにあると、スクリプトが機能しました。

関連する問題