doulu8446 2016-09-05 07:55
浏览 101
已采纳

(PHP)Oauth2.0 - 错误401 Google Contacts API

i trying the Google API for contacts but when i want retreive all contacts , i have a error 401 ('Invalid Credenticials')... i don't understand because the token is generated but i can't retreive the contacts.

<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

Thanks for your help

</div>
  • 写回答

2条回答 默认 最新

  • dongyu9263 2016-09-05 08:10
    关注

    In order to get data back you need to be authenticated as you appear to know. However you are tagging on the access token using &token=

    the correct way to do it is &access_token=

    Example:

    $curl = curl_init('https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=50&access_token='.$token->access_token);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵