我正在尝试使用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 \程序\控制器\谷歌API的PHP客户端的\ src \谷歌\ HTTP \ REST.php:111 堆栈跟踪:#0 C:\ XAMPP \ htdocs中\ tcaller \应用\控制器\ Google处理API-PHP-客户\ SRC \谷歌\ HTTP \ REST.php(63): Google_Http_REST :: decodeHttpResponse(对象(Google_Http_Request) 对象(Google_Client))#1 [内部函数]: Google_Http_REST :: doExecute(对象(Google_Client) 对象(Google_Http_Request))#2 C:\ XAMPP \ htdocs中\ tcaller \应用\控制器\ Google处理API-PHP-客户\ SRC \谷歌\任务\ Runner.php(172): call_user_func_array(Array,Array)#3 C:\ XAMPP \ htdocs中\ tcaller \应用\控制器\ Google处理API-PHP-客户\ SRC \谷歌\ HTTP \ REST.php(47): Google_Task_Runner-&gt; run()#4 C:\ XAMPP \ htdocs中\ tcaller \应用\控制器\ Google处理API-PHP-客户\ SRC \谷歌\ Client.php(564): Google_Http_REST :: execute(Object(Google_Client),Object(Google_H in。) C:\ XAMPP \ htdocs中\ tcaller \程序\控制器\谷歌API的PHP客户端的\ src \谷歌\ HTTP \ REST.php 在第111行
我错过了什么
答案 0 :(得分:0)
当您获取并使用已具有客户端密钥的OAuth 2.0访问令牌时,您不需要使用单独的API密钥并调用$client->setDeveloperKey()
。而且,似乎这是不正确的和/或与同一项目无关。所以请尝试删除该电话。