使用 OAUTH2 获取用户联系人列表



我正在尝试使用谷歌Oauth2访问用户联系人列表,但似乎遇到了一个错误不足的Permis。我的代码如下

<?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::d ecodeHttpResponse(Object(Google_Http_Request), 对象(Google_Client)) #1 [内部函数]: Google_Http_REST::d oExecute(Object(Google_Client), 对象(Google_Http_Request)) #2 C:\xampp\htdocs\tcaller\application\controllers\google-api-php-client\src\Google\Task\Runner.php(172): call_user_func_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 111行

我错过了什么吗

获取和使用已具有客户端密码的 OAuth 2.0 访问令牌时,无需使用单独的 API 密钥并调用$client->setDeveloperKey()。此外,这似乎是不正确的和/或与同一项目无关。因此,请尝试删除该呼叫。

最新更新