2016-11-03 8 views
0

私はPHPでGoogleに連絡を追加しようとしました Googleの開発者に新しいプロジェクトを追加し、このメールの連絡先APIを有効にします: と私は以下のコードを使用しました。 :PHP Googleへの連絡先を追加するには

<?php 
$code=doubleval($_REQUEST['code']); 
echo 'code: '.$code.'<br />'.PHP_EOL; 
$access = get_oauth2_token($code); 
echo 'access: '.$access.'<br />'.PHP_EOL; 
$client_id= 'hazem*********@gmail.com'; 
$client_secret= '***********'; 
$redirect_uri= 'http://************/synchronize.php'; 
$gsm=doubleval($_REQUEST['gsm']); 
echo 'gsm: '.$gsm.'<br />'.PHP_EOL; 
if($gsm){ 
    addContact($access, $gsm); 
} 

//returns session token for calls to API using oauth 2.0 
function get_oauth2_token($code) { 
    global $client_id; 
    global $client_secret; 
    global $redirect_uri; 

    $oauth2token_url = "https://accounts.google.com/o/oauth2/token"; 
    $clienttoken_post = array(
    "code" => $code, 
    "client_id" => $client_id, 
    "client_secret" => $client_secret, 
    "redirect_uri" => $redirect_uri, 
    "grant_type" => "authorization_code" 
    ); 

    $curl = curl_init($oauth2token_url); 

    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post); 
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 

    $json_response = curl_exec($curl); 
    echo 'json_response: '.$json_response.'<br />'.PHP_EOL; 
    curl_close($curl); 

    $authObj = json_decode($json_response); 

    if (isset($authObj->refresh_token)){ 
     //refresh token only granted on first authorization for offline access 
     //save to db for future use (db saving not included in example) 
     global $refreshToken; 
     $refreshToken = $authObj->refresh_token; 
    } 

    $accessToken = $authObj->access_token; 
    return $accessToken; 
} 

function addContact($access, $gsm){ 
    $name =$gsm; 
    $fullName=$gsm; 
    $last= $gsm; 
    /* 

<gd:givenName>'.$name.'</gd:givenName> 
<gd:familyName>'.$last.'</gd:familyName> 

    //*/ 
$contactXML = '<?xml version="1.0" encoding="utf-8"?> 
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005"> 
<atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/> 
<gd:name> 
<gd:fullName>'.$fullName.'</gd:fullName> 
</gd:name> 
<gd:phoneNumber rel="http://schemas.google.com/g/2005#home" primary="true">'.$gsm.'</gd:phoneNumber> 
</atom:entry>'; 
echo $contactXML; 

$headers = array(
'Host: www.google.com', 
'Gdata-version: 3.0', 
'Content-length: '.strlen($contactXML), 
'Content-type: application/atom+xml', 
'Authorization: OAuth '.$access 
); 

    $contactQuery = 'https://www.google.com/m8/feeds/contacts/default/full/'; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $contactQuery); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $contactXML); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_FAILONERROR, true); 
    curl_exec($ch); 


} 



?> 
+0

ホイールを再発明するのではなく、Googleのapiクライアントを使用してみませんか?連絡先を追加しようとすると、https://github.com/google/google-api-php-client –

答えて

0

クライアントIDは番目です:json_response: { "error" : "invalid_request", "error_description" : "Could not determine client ID from request." }

あなたはこの問題を修正

parameters: 
code: id from database for example: 101 
gsm: cell phone 

コードために私を助けることができますeデベロッパーコンソールに登録したクライアントアプリケーションのID(クライアントの秘密で発行された)。ユーザーの電子メールではありません。

+0

、そのエラー:401を返します。これはエラーです。 リクエストに誤りがありました。 –

関連する問題