2

I am trying to implement Google's new Webmaster PHP API. I am already using the Connector for Google Analytics where all works fine.

I have the API enabled, the gmail User is also owner of the site. The gmail User also created the Client ID and the Service Account.

Then I added for my Service Account both WebMaster scopes. --> For this I took my Service Account email and Changed it from

 946@developer.gserviceaccount.com =>946.apps.googleusercontent.com

and added the scopes via the admin menu.

-- Probaly I am doing something wrong here?

I guess I hust forgot to add or set some permissions, but i really dont know which one. Somebody got help on this?

My Error is

Uncaught exception 'Google_Service_Exception' with message 'Error calling POST 
https://www.googleapis.com/webmasters/v3/sites/SITEURL/searchAnalytics/query: (403) 
User does not have sufficient permission for site 'SITEURL'.
 See also: https://support.google.com/webmasters/answer/2451999.'  
in E:\xampp\htdocs\google\google-api-php-client\src\Google\Http\REST.php:110 Stack trace: 
#0 E:\xampp\htdocs\google\google-api-php-client\src\Google\Http\REST.php(62):
 Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client)) 
#1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request)) 
#2 E:\xampp\htdocs\google\google-api-php-client\src\Google\Task\Runner.php(174): call_user_func_array(Array, Array) 
#3 E:\xampp\htdocs\google\google-api-php-client\src\Google\Http\REST.php(46): Google_Task_Runner->run() 
#4 E:\xampp\htdocs\google\google-api-php-client\src\Google\Client.php(593): 
Google_Http_REST::execute(Object(Google_Cl in E:\xampp\htdocs\google\google-api-php-client\src\Google\Http\REST.php on line 110

My Conector:

public function connect() {

        $this->setProfileId($this->options['ga_profile_id']);
        $this->client = new Google_Client();
        $this->client->setApplicationName("XXXNAMEXXX");
        $this->client->setClientId('XXXX946.apps.googleusercontent.com');
        $this->service = new Google_Service_Webmasters($this->client);

        if (isset($this->serviceToken)) {
            $this->client->setAccessToken($this->serviceToken);
        }
        $key = file_get_contents($this->options['ga_key_location']);

        $cred = new Google_Auth_AssertionCredentials(
            $this->options['ga_service_account_name'], array('https://www.googleapis.com/auth/webmasters.readonly','https://www.googleapis.com/auth/webmasters'), $key
        );

        $this->client->setAssertionCredentials($cred);

        if ($this->client->getAuth()->isAccessTokenExpired()) {
            $this->client->getAuth()->refreshTokenWithAssertion($cred);
        }

        $this->serviceToken = $this->client->getAccessToken();
    }

My Query Call:

public function get_searchanalytics( $url, $limit = false, $dimensions = array( 'query' ), $aggregation_type = 'auto', $options = array() ) {
    $search = new Google_Service_Webmasters_SearchAnalyticsQueryRequest;
    $search->setStartDate( $start ? $start : date( 'Y-m-d', strtotime( '1 month ago' ) ) );
    $search->setEndDate( $end ? $end : date( 'Y-m-d', strtotime( 'now' ) ) );
    $search->setDimensions( $dimensions );
    if ( $limit ) $search->setRowLimit( $limit );
    $search->setAggregationType( $aggregation_type );
    return $this->service->searchanalytics->query( $url, $search, $options )->getRows();
}
Markus_DE_HH
  • 1,061
  • 3
  • 13
  • 29
  • Did you ever get this to work? I too am having a problem getting the GWT Webmaster Tools to work. You said **I have the API enabled**. How did you do this? What is it known as? Becuase when I go **https://console.developers.google.com/apis/enabled?project=xxyyzz** I don't see anything resembling GWT. – M Schenkel Dec 05 '15 at 17:02
  • Yes answer you tommorow – Markus_DE_HH Dec 06 '15 at 17:58
  • 2
    @MSchenkel it's called Google Search Console API in the console – Taavi Randmaa Dec 08 '15 at 22:07

0 Answers0