I have got the code for analytics to work so that it manages to query Google Analytics and brings back results however when I try to use the code to query webmaster tools it comes back with Insufficient Permissions.
Google Enabled API's are : Analytics API, Google Search Console API
Is there something I am missing?
Google Analytics Code:
$client = new Google_Client();
$client->setAuthConfigFile($SECRET);
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY); //For analytics stuff
$client->setAccessType('offline');
$client->setPrompt('prompt');
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
//Checking to see if the token is expired
if($client->isAccessTokenExpired()){
$client->refreshToken($refreshToken);
$_SESSION['access_token'] = $client->getAccessToken();
}
$results = $analytics->data_ga->get(
$viewID,
$fromDate,
$toDate,
$metrics,
$optParams);
foreach($results->rows as $data){
echo "<pre>",print_r($data),"</pre>";
}
}else {
$redirect_uri = $redirectURL;
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
Webmaster Tools Code:
$client = new Google_Client();
$client->setAuthConfigFile($SECRET);
$client->addScope(Google_Service_Webmasters::WEBMASTERS_READONLY); //For WebMaster Tools
$client->setAccessType('offline');
$client->setPrompt('prompt');
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
//Checking to see if the token is expired
if($client->isAccessTokenExpired()){
$client->refreshToken($refreshToken);
$_SESSION['access_token'] = $client->getAccessToken();
}
//Creating Webmaster Service
$webmastersService = new Google_Service_Webmasters($client);
$searchanalytics = $webmastersService->searchanalytics;
//Creating Request
$request = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$request->setStartDate('2016-05-01');
$request->setEndDate('2016-05-31');
$request->setDimensions( array('query') );
$qsearch = $searchanalytics->query("http://www.example.co.uk", $request);
$rows = $qsearch->getRows();
echo "<pre>",print_r($rows),"</pre>";
} else {
$redirect_uri = $redirectURL;
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
Webmaster Tools Error Message :
{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}
When you run the first one you ask the user can I access your Google Analytics data the user says yes you can and you get a access token that can be used to access their google analytics data.
In the second one you ask the user can I access your webmaster tools data the user says yes you get an access token to access their web mater tools data.
If you where to put both of the scopes in instead of just one the user would be asked can I access your google analytics data and your web master tools data. If they say yes you get an access token to access them both.
If you try and use authentication from a analytics Auth request to access web master tools you will get a Insufficient permissions.
If you need access to both then request access to both. If you first want one then maybe later want the other one then yes you will have to ask them for the other one later.