2016-11-03 7 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. 오류입니다. 요청하신 내용에 오류가 있습니다. –