【问题标题】:(PHP) Oauth2.0 - Error 401 Google Contacts API(PHP) Oauth2.0 - 错误 401 Google 联系人 API
【发布时间】:2017-01-12 13:23:36
【问题描述】:

我尝试使用 Google API 获取联系人,但是当我想检索所有联系人时,出现错误 401('Invalid Credentials')...我不明白,因为生成了令牌,但我无法检索联系人.

<p style="background-color:red;">
<a href="destroy.php">se deconnecter</a>
<p>
<?php
 session_start();

  require 'lib/google-api-client/Google/autoload.php';
  $client = new Google_Client();
  $client->setApplicationName('Application de test');
  $client->setClientId('xxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com');
  $client->setClientSecret('xxxxxxxxxxxxxxxxxxxx');
  $client->setRedirectUri('http://localhost/Gmail/index.php');
  //Acces aux données seulement quand l'user est en ligne
  $client->setAccessType('online');
  //configuration des données auxquels on veut avoir accés
  $client -> setScopes('https://www.google.com/m8/feeds');

  if(isset($_GET['code'])){
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
    header('Location:http://localhost/Gmail/index.php');
  }

  if(!isset($_SESSION['token'])){
    //Generation du lien pour s'authentifier via l'api Google
    $url =  $client->createAuthUrl();
 ?>

 <a href="<?= $url ?>">Importer Google contacts</a>
<?php
  }else{
    $client->setAccessToken($_SESSION['token']);
    $token = json_decode($_SESSION['token']);
    var_dump($token->access_token);
    var_dump($client->getAccessToken());
    $curl = curl_init('https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=50&token='.$token->access_token);
    curl_setopt($curl , CURLOPT_RETURNTRANSFER , true);
    curl_setopt($curl , CURLOPT_SSL_VERIFYPEER , false);
    curl_setopt($curl , CURLOPT_TIMEOUT , 10);
    $contact_json = curl_exec($curl);
    var_dump($contact_json);
    curl_close($curl) ;
    $contacts = json_decode($contact_json);
    var_dump($contacts);
  }
 ?>
enter image description here

感谢您的帮助

【问题讨论】:

  • 将 &token= 更改为 &access_token=
  • 谢谢,成功了!

标签: php oauth-2.0 google-api google-api-php-client


【解决方案1】:

为了取回数据,您需要进行身份验证,正如您所知道的那样。但是,您正在使用 &amp;token=

标记访问令牌

正确的做法是&amp;access_token=

例子:

$curl = curl_init('https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=50&access_token='.$token->access_token);

【讨论】:

    【解决方案2】:

    您必须更正您的 CURL url,使用 access_token 而不是 url 中的令牌。

    $curl = curl_init('https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=50&access_token ='.$token->access_token);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      相关资源
      最近更新 更多