2014-01-06 19 views
1

當我嘗試添加聯繫人的Gmail,它總是返回40添加聯繫人到Gmail:我不能使用PHP

這是一個錯誤。您的請求中存在錯誤。我們知道的就這些。

我該如何解決這個問題?

我嘗試下面的代碼

$sClientId = 'ClientID'; 
    $sClientSecret = 'ClientSecret'; 
    $sCallback = 'http://humaclab.com/development/contacts/index.php/home'; // callback url, don't forget to change it to your! 
    $iMaxResults = 500; // max results 
    $sStep = 'auth'; // current step 
    $argarray = array(); 

    // include GmailOath library https://code.google.com/p/rspsms/source/browse/trunk/system/plugins/GmailContacts/GmailOath.php?r=11 
    require_once "GmailOath.php"; 

    session_start(); 

    // prepare new instances of GmailOath and GmailGetContacts 
    $oAuth = new GmailOath($sClientId, $sClientSecret, $argarray, false, $sCallback); 
    $oGetContacts = new GmailGetContacts(); 


    if ($_GET && $_GET['oauth_token']) 
    { 
     $sStep = 'fetch_contacts'; // fetch contacts step 

     // decode request token and secret 
     $sDecodedToken = $oAuth->rfc3986_decode($_GET['oauth_token']); 
     $sDecodedTokenSecret = $oAuth->rfc3986_decode($_SESSION['oauth_token_secret']); 

     // get 'oauth_verifier' 
     $oAuthVerifier = $oAuth->rfc3986_decode($_GET['oauth_verifier']); 

     // prepare access token, decode it, and obtain contact list 
     $oAccessToken = $oGetContacts->get_access_token($oAuth, $sDecodedToken, $sDecodedTokenSecret, $oAuthVerifier, false, true, true); 
     $sAccessToken = $oAuth->rfc3986_decode($oAccessToken['oauth_token']); 
     $sAccessTokenSecret = $oAuth->rfc3986_decode($oAccessToken['oauth_token_secret']); 

     $url = 'https://www.google.com/m8/feeds/contacts/default/full?oauth_token='.$sAccessToken; 

     $doc = new DOMDocument(); 
     $doc->formatOutput = true; 

     $entry = $doc->createElement('atom:entry'); 
     $entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:atom', 'http://www.w3.org/2005/Atom'); 
     $entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gd', 'http://schemas.google.com/g/2005'); 
     $doc->appendChild($entry); 

     $cat = $doc->createElement('atom:category'); 
     $cat->setAttribute('scheme', 'http://schemas.google.com/g/2005#kind'); 
     $cat->setAttribute('term', 'http://schemas.google.com/contact/2008#contact'); 
     $entry->appendChild($cat); 

     // add name element 
     $name = $doc->createElement('gd:name'); 
     $entry->appendChild($name); 
     $givenName = $doc->createElement('gd:givenName', 'First'); 
     $familyName = $doc->createElement('gd:familyName', 'Last'); 
     $fullName = $doc->createElement('gd:fullName', 'First Last'); 
     $name->appendChild($givenName); 
     $name->appendChild($familyName); 
     $name->appendChild($fullName); 

     $content = $doc->createElement('atom:content', 'Notes'); 
     $content->setAttribute('type', 'text'); 
     $entry->appendChild($content); 

     // add email element 
     $email = $doc->createElement('gd:email'); 
     $entry->appendChild($email); 
     $email->setAttribute('address', '[email protected]'); 
     //$email->setAttribute('displayName', $_POST['fname']); 
     //$email->setAttribute('primary', 'true'); 
     $email->setAttribute('rel', 'http://schemas.google.com/g/2005#work'); 


     //insert Address 
     $address = $doc->createElement('gd:structuredPostalAddress'); 
     $address->setAttribute('primary', 'true'); 
     $address->setAttribute('rel', 'http://schemas.google.com/g/2005#work'); 
     $entry->appendChild($address); 
     $postal = $doc->createElement('gd:postcode', '1212'); 
     $country = $doc->createElement('gd:country', 'BD'); 
     $fulladd = $doc->createElement('gd:formattedAddress', '1212, BD'); 
     $address->appendChild($postal); 
     $address->appendChild($country); 
     $address->appendChild($fulladd); 

     $contact_detail = $doc->saveXML(); 


     $curl = curl_init(); 
     curl_setopt($curl, CURLOPT_URL, $url); 
     curl_setopt($curl, CURLOPT_POST, 5); 
     curl_setopt($curl, CURLOPT_POSTFIELDS, $contact_detail); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 

     $curlheader[0] = "Content-Type: application/atom+xml"; 
     $curlheader[2] = "Content-length:" . strlen($contact_detail); 
     $curlheader[1] = "Content-Transfer-Encoding: binary"; 
     curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader); 

     $xmlresponse = curl_exec($curl); 

     print_r($xmlresponse); 
    } 

    else 
    { 
     // prepare access token and set it into session 
     $oRequestToken = $oGetContacts->get_request_token($oAuth, false, true, true); 
     $_SESSION['oauth_token'] = $oRequestToken['oauth_token']; 
     $_SESSION['oauth_token_secret'] = $oRequestToken['oauth_token_secret']; 
     $login_url = 'https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token='.$oAuth->rfc3986_decode($oRequestToken['oauth_token']);   
     echo '<a href="'.$login_url.'">login</a>'; 
    } 
+3

如果這是你真正的clientId和clientSecret,你或許應該刪除並重新發布沒有這些細節(編輯將在問題的歷史記錄中保留它們)。 – bcmcfc

回答

0

我不知道GmailOath庫 - 這似乎很奇怪,我認爲客戶端機密等被存儲在一個會話。我對訪問與OAuth的一個谷歌API的建議是使用谷歌提供的PHP客戶端庫:https://code.google.com/p/google-api-php-client/

上面的鏈接對如何鑑別一個入門例子 - 然後使用該認證,使針對Google+的API的API調用。在你的情況下,刪除該行,而是保存由$client->getAccessToken()返回的值,並將授權頭添加到HTTPS請求(而不是放入查詢字符串)。下面對如何做到這一點一些討論:How to include Authorization header in cURL POST HTTP Request in PHP?