简体   繁体   English

(PHP)Oauth2.0-错误401 Google通讯录API

[英](PHP) Oauth2.0 - Error 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. 我尝试使用Google API进行通讯录,但是当我要检索所有联系人时,出现错误401(“无效凭据”)...我不明白,因为生成了令牌,但我无法检索联系人。

 <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 谢谢你的帮助

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

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

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= 但是,您正在使用&token=标记访问令牌

the correct way to do it is &access_token= 正确的方法是&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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM