Php 使用OAUTH2获取用户联系人列表
我试图使用google Oauth2访问用户联系人列表,但似乎遇到了权限不足的错误。我的代码如下Php 使用OAUTH2获取用户联系人列表,php,api,oauth-2.0,codeigniter-2,google-oauth,Php,Api,Oauth 2.0,Codeigniter 2,Google Oauth,我试图使用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() {
<?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\u服务\u异常”与
调用GET时出现消息“”错误
:
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.php63:
Google_Http_REST::DecodeHttpResponseObject Google_Http_请求,
ObjectGoogle_客户端1[内部函数]:
Google_Http_REST::DoExecuteObject Google_客户端,
ObjectGoogle_Http_请求2
C:\xampp\htdocs\tcaller\application\controllers\google api php client\src\google\Task\Runner.php172:
调用用户函数数组数组,数组3
C:\xampp\htdocs\tcaller\application\controllers\google api php client\src\google\Http\REST.php47:
谷歌任务运行程序->运行4
C:\xampp\htdocs\tcaller\application\controllers\google api php client\src\google\client.php564:
Google_Http_REST::executeObjectGoogle_客户端,ObjectGoogle_H in
C:\xampp\htdocs\tcaller\application\controllers\google api php client\src\google\Http\REST.php
在线111
如果我遗漏了什么您不需要使用单独的API密钥并调用$client->setDeveloperKey,因为您已经获得并使用了带有客户端密码的OAuth 2.0访问令牌。此外,这似乎是不正确的和/或与同一项目无关。因此,请尝试删除该呼叫