duanlanzhi5509 2015-08-03 03:06
浏览 28
已采纳

导入Google联系人时的OAuth问题

Good Morning - I am using the following YouTube tutorial Tutoriel PHP - Importer des contacts Google to try and import Google contacts.

When I click on the link to import Google contacts, instead of asking for permission and getting the contacts and displaying them on screen, I get the following errors:

Warning: Missing argument 1 for Google_Client::authenticate(), called in /var/www/html/restaurant_test/index.php on line 34 and defined in /var/www/html/restaurant_test/lib/google-api-client/Google_Client.php on line 124

Notice: Undefined variable: code in /var/www/html/restaurant_test/lib/google-api-client/Google_Client.php on line 127

Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Invalid code' in /var/www/html/restaurant_test/lib/google-api-client/Auth/OAuth2.php:88 Stack trace: #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} thrown in /var/www/html/restaurant_test/lib/google-api-client/Auth/OAuth2.php on line 88

Please tell me what to do in order to fix the errors. Thank you.

Here is my code:

<?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>
  • 写回答

1条回答 默认 最新

  • dongyun6003 2015-08-03 03:14
    关注

    Looks like you're forgetting to pass the code to the authenticate method. Here's the documentation and implementation of the authenticate method in Google_Client.php:

    /**
     * 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);
    }
    

    So changing your code as below will most likely resolve the issue:

    if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']); // <-- Add code parameter here
        $_SESSION['token'] = $client->getAccessToken();
        header('Location: ' . $redirect_uri);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料