2016-09-05 9 views
1

私は連絡先のGoogle APIを試していますが、すべての連絡先を取得したい場合は、エラー401( '無効な信用状')があります。トークンは生成されますが、私は連絡先を検索できません。(PHP)Oauth2.0 - エラー401 GoogleコンタクトAPI

<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

データは、あなたが知っているように見えるとして、あなたが認証される必要が戻って取得するためにあなたの助け

+0

変更&トークン= to&access_token = – DaImTo

+1

ありがとうございます! –

答えて

2

いただきありがとうございます。あなたは&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); 
3

は、URLではなくトークンのaccess_tokenはあなたのCURLのURLの使用を修正する必要があります。

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