我正在尝试按照此YouTube教程使用OAuth 2.0导入Google通讯录:Tutoriel PHP - Importer des contacts Google。但是,目前没有显示按钮链接。
这是我的index.php
文件的代码:
<?php 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 'config.php';
require 'lib/google-api-client/Google_Client.php';
$client_id = '<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbb.cccccccccccc.com>';
$client_secret = '<e3fsfds4gfg23ha93kmKFkfgK>';
$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: ' . $google_redirect_uri);
}
if(!isset($_SESSION['token'])) {
$url = $client->createAuhUrl();
?>
<a href="<?=$url; ?>">Import Google Contacts</a>
<?php
} 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)
只有在if(!isset($_SESSION['token']))
为假时才会显示。
令牌未设置?
echo $_SESSION['token'];
在<h2>Google Contacts API v3.0</h2>
之后确定。
这样做也是浪费:
if(!isset($_SESSION['token'])){
$url = $client->createAuhUrl();
?>
<a href="<?=$url; ?>">Import Google Contacts</a>
<?php
} else {
请改为:
if(!isset($_SESSION['token'])){
$url = $client->createAuhUrl();
echo '<a href="' . $url . '">Import Google Contacts</a>';
} else {
如果HTML内容的数量很大,那么只能打败PHP。
答案 1 :(得分:0)
我不了解PHP,
但解决方案是您必须为href添加一些链接,如
<a href="www.google.com">test</a>
或
<a href="#">test</a>
答案 2 :(得分:0)
您可以像这样放置代码,了解问题
if(isset($_SESSION['token'])){
$url = $client->createAuhUrl();
?>
<a href="<?=$url; ?>">Import Google Contacts</a>
<?php
} else {