0
votes

I am trying to access a users contact list using google Oauth2 but seem to be running into an error insufficient permision. My code is as below

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Google extends CI_Controller {
 public function __construct() {
 parent::__construct();
}
public function index() {
 require_once 'google-api-php-client/autoload.php'; // or wherever
 autoload.php is located

 $client    = new Google_Client();
 $scriptUri = "http://localhost/tcaller/google/";

 $client = new Google_Client();
 $client->setAccessType('online'); // default: offline
 $client->setApplicationName('Tcaller');
 $client->setClientId('******');
 $client->setClientSecret('****');
 $client->setRedirectUri($scriptUri);
 $client->setDeveloperKey('******'); // API key
 $client->setScopes(array('https://www.google.com/m8/feeds' , 'https://www.googleapis.com/auth/contacts.readonly'));


 $plus    = new Google_Service_Plus($client);

if (isset($_GET['logout'])) { // logout: destroy token
 $this->session->unset_userdata("token");
 die('Logged out.');
}

if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
 $client->authenticate($_GET['code']);
 $tokenArr = array('token' => $client->getAccessToken());
 $this->session->set_userdata($tokenArr);//gets valid access token  
 $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; //set into session storage
 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); 
}
if ($this->session->userdata('token')) { // extract token from session and configure client
 $token = $this->session->userdata('token');
 $client->setAccessToken($token);
try {
$me = $plus->people->get('me');
print_r($me);
} catch (apiServiceException $e) {
 // Handle exception. You can also catch Exception here.
 // You can also get the error code from $e->getCode();
 echo 'error';
 }      
}
if (!$client->getAccessToken()) { // auth call to google
   $authUrl = $client->createAuthUrl();
   header("Location: ".$authUrl);
 die();
 }
}
}
 ?> 

I am getting the error

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/plus/v1/people/me?key=%2A%2A%2A%2A%2A%2A: (403) Insufficient Permission' in C:\xampp\htdocs\tcaller\application\controllers\google-api-php-client\src\Google\Http\REST.php:111 Stack trace: #0 C:\xampp\htdocs\tcaller\application\controllers\google-api-php-client\src\Google\Http\REST.php(63): 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 C:\xampp\htdocs\tcaller\application\controllers\google-api-php-client\src\Google\Task\Runner.php(172): call_user_func_array(Array, Array) #3 C:\xampp\htdocs\tcaller\application\controllers\google-api-php-client\src\Google\Http\REST.php(47): Google_Task_Runner->run() #4 C:\xampp\htdocs\tcaller\application\controllers\google-api-php-client\src\Google\Client.php(564): Google_Http_REST::execute(Object(Google_Client), Object(Google_H in C:\xampp\htdocs\tcaller\application\controllers\google-api-php-client\src\Google\Http\REST.php on line 111

Am i missing something

1

1 Answers

0
votes

You should not need to use a separate API key and call $client->setDeveloperKey() as you're obtaining and using an OAuth 2.0 access token already with a client secret. Moreover, it seems that that is incorrect and/or not associated with the same project. So please try with that call removed.