早上好 - 我正在使用以下YouTube教程Tutoriel PHP - Importer des contacts Google尝试导入Google联系人。
当我点击链接导入Google通讯录时,我收到以下错误,而不是要求获得许可并获取联系人并在屏幕上显示这些联系人:
警告:缺少Google_Client :: authenticate()的参数1,名为 在第34行的/var/www/html/restaurant_test/index.php中定义 /var/www/html/restaurant_test/lib/google-api-client/Google_Client.php 在第124行
注意:未定义的变量:代码输入 /var/www/html/restaurant_test/lib/google-api-client/Google_Client.php 在第127行
致命错误:带有消息的未捕获异常'Google_Auth_Exception' '无效代码' /var/www/html/restaurant_test/lib/google-api-client/Auth/OAuth2.php:88 堆栈跟踪:#0 /var/www/html/restaurant_test/lib/google-api-client/Google_Client.php(127): Google_Auth_OAuth2-> authenticate(NULL,false)#1 /var/www/html/restaurant_test/index.php(34): Google_Client-> authenticate()#2 {main}引入 /var/www/html/restaurant_test/lib/google-api-client/Auth/OAuth2.php on 第88行
请告诉我该怎么做以修复错误。谢谢。
这是我的代码:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
session_start(); ?>
<!DOCTYPE html>
<html class="no-js" lang="en"/>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Google Contacts API</title>
</head>
<body>
<h2>Google Contacts API v3.0</h2>
<?php
require_once 'lib/google-api-client/autoload.php';
require 'lib/google-api-client/Config.php';
require 'lib/google-api-client/Google_Client.php';
$client_id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbb.cccccccccccc.com';
$client_secret = 'oUe3fsfds4gfg23ha93kmKFkfgKZ';
$redirect_uri = 'http://ccccccccccccccccccc.com/rddddddddddd/index.php';
$client = new Google_Client();
$client -> setApplicationName('contact');
$client -> setClientid($client_id);
$client -> setClientSecret($client_secret);
$client -> setScopes('https://www.google.com/m8/feeds');
$client -> setRedirectUri($redirect_uri);
$client -> setAccessType('online');
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
header('Location: ' . $redirect_uri);
}
if(!isset($_SESSION['token']))
{
$url = $client->createAuthUrl();
echo '<a href="' . $url . '">Import Google Contacts</a>';
}else{
$client->setAccessToken($_SESSION['token']);
$token = json_decode($_SESSION['token']);
$token->access_token;
$curl = curl_init("https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=50&access_token=" . $token->access_token);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$contacts_json = curl_exec($curl);
curl_close($curl);
$contacts = json_decode($contacts_json, true);
$return = array();
foreach($contacts['feed']['entry'] as $contact){
$return[] = array(
'name' => $contact['title']['$t'],
'email' => isset($contact['gd$email'][0]['address']) ? $contact['gd$email'][0]['address'] : false,
'phone' => isset($contact['gd$phoneNumber'][0]['$t']) ? $contact['gd$phoneNumber'][0]['$t'] :false,
);
}
var_dump($return);
}
?>
</body>
</html>
答案 0 :(得分:0)
您似乎忘记了将code
传递给authenticate
方法。以下是Google_Client.php
中authenticate
方法的文档和实施:
/**
* Attempt to exchange a code for an valid authentication token.
* If $crossClient is set to true, the request body will not include
* the request_uri argument
* Helper wrapped around the OAuth 2.0 implementation.
*
* @param $code string code from accounts.google.com
* @param $crossClient boolean, whether this is a cross-client authentication
* @return string token
*/
public function authenticate($code, $crossClient = false)
{
$this->authenticated = true;
return $this->getAuth()->authenticate($code, $crossClient);
}
因此,更改以下代码很可能会解决问题:
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']); // <-- Add code parameter here
$_SESSION['token'] = $client->getAccessToken();
header('Location: ' . $redirect_uri);
}