ИСПОЛЬЗОВАНИЕ OAUTH2 ДЛЯ ПОЛУЧЕНИЯ КОНТАКТНОГО СПИСКА ПОЛЬЗОВАТЕЛЕЙ

0

Я пытаюсь получить доступ к списку контактов пользователей, используя google Oauth2, но, похоже, работает с ошибкой с недостаточным разрешением. Мой код выглядит следующим образом:

<?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();
 }
}
}
 ?> 

Я получаю сообщение об ошибке

Неустранимая ошибка: исключить исключение "Google_Service_Exception" с сообщением "Ошибка вызова GET https://www.googleapis.com/plus/v1/people/me?key=%2A%2A%2A%2A%2A%2A: (403) Недостаточно разрешения 'в C:\xampp\htdocs\tcaller\application\controllers\google-api-php-client\src\Google\Http\REST.php: 111 Трассировка стека: # 0 C:\xampp\htdocs\tcaller\application\controllers\google-api-php-client\src\Google\Http\REST.php(63): Google_Http_REST :: decodeHttpResponse (объект (Google_Http_Request), объект (Google_Client)) # 1 [внутренняя функция]: 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 :: выполнить (объект (Google _Client), Object (Google_H в C:\xampp\htdocs\tcaller\application\controllers\google-api-php-client\src\Google\Http\REST.php в строке 111

Я что-то упускаю

Теги:
oauth-2.0
google-oauth
codeigniter-2

1 ответ

0

Вам не нужно использовать отдельный ключ API и вызывать $client->setDeveloperKey() мере получения и использования токена доступа OAuth 2.0 уже с секретностью клиента. Более того, кажется, что это неверно и/или не связано с одним и тем же проектом. Поэтому, пожалуйста, попробуйте удалить этот звонок.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню