2014-12-03 16 views
6

Voglio ottenere la lista contatti di GMail nel mio sito web usando PHP.API Google - Ottieni lista contatti usando PHP

E tutorial di cui Here

CODICE:

<?php 

// disable warnings 
if (version_compare(phpversion(), "5.3.0", ">=") == 1) 
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 
else 
error_reporting(E_ALL & ~E_NOTICE); 

$sClientId = '564766218700- 
fgtj5fba9h15g8na4khdho1uavo0rtjq.apps.googleusercontent.com'; 
$sClientSecret = 'GldOKp2S2ABdp-7owp3ZO_cE'; 
$sCallback = 'http://localhost/GmailContact/index.php'; // callback url, don't forget 
to change it to your! 
$iMaxResults = 20; // max results 
$sStep = 'auth'; // current step 

// include GmailOath library https://code.google.com/p/rspsms/source/browse/trunk 
/system/plugins/GmailContacts/GmailOath.php?r=11 
include_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']); 
$aContacts = $oGetContacts->GetContacts($oAuth, $sAccessToken, $sAccessTokenSecret, 
false, true, $iMaxResults); 

// turn array with contacts into html string 
$sContacts = $sContactName = ''; 
foreach($aContacts as $k => $aInfo) { 
    $sContactName = end($aInfo['title']); 
    $aLast = end($aContacts[$k]); 
    foreach($aLast as $aEmail) { 
     $sContacts .= '<p>' . $sContactName . '(' . $aEmail['address'] . ')</p>'; 
    } 
} 
} 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']; 
} 

?> 
<!DOCTYPE html> 
<html lang="en" > 
<head> 
    <meta charset="utf-8" /> 
    <title>Google API - Get contact list | Script Tutorials</title> 
    <link href="css/main.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 
    <header> 
     <h2>Google API - Get contact list</h2> 
     <a href="http://www.script-tutorials.com/google-api-get-contact-list/" 
class="stuts">Back to original tutorial on <span>Script Tutorials</span></a> 
    </header> 
    <img src="oauthLogo.png" class="google" alt="google" /> 

<?php if ($sStep == 'auth'): ?> 
    <center> 
    <h1>Step 1. OAuth</h1> 
    <h2>Please click <a href="https://www.google.com/accounts 
/OAuthAuthorizeToken?oauth_token=<?php echo 
$oAuth->rfc3986_decode($oRequestToken['oauth_token']) ?>">this link</a> in order to 
get access token to receive contacts</h2> 
    </center> 
<?php elseif ($sStep == 'fetch_contacts'): ?> 
    <center> 
    <h1>Step 2. Results</h1> 
    <br /> 
    <?= $sContacts ?> 
    </center> 
<?php endif ?> 

sto incontrando con un errore come ad esempio:

OAuth parametro token mancante. Questo è tutto ciò che sappiamo.

Domande: 1. Come ottenere token di accesso OAuth?

Per favore aiutatemi.

+0

Controllare questo collegamento per il client PHP oauth http://25labs.com/import-gmail-or-google-contacts-using-google-contacts-data-api-3-0-and-oauth-2-0- in-php/ – SGC

+0

@SGC ho usato il tutorial sopra specificato. Ma i miei indirizzi email sono vuoti. –

+0

@SGC echo ($ xmlrespose) restituisce: usageLimits accessNotConfigured Access Not Configured. L'API non è abilitata per il tuo progetto, oppure esiste una restrizione per-IP o per-Referer configurata sulla tua chiave API e la richiesta non corrisponde a queste restrizioni. Utilizza Google Developers Console per aggiornare la configurazione. https://console.developers.google.com. –

risposta

0

Esistono diversi modi per rendere la richiesta di accesso token e variano in base al tipo di applicazione che si sta creando.

Ad esempio, un'applicazione JavaScript può richiedere un token di accesso utilizzando un reindirizzamento del browser a Google, mentre un'applicazione installata su un dispositivo privo di browser utilizza richieste di servizi Web.

Alcune richieste richiedono una fase di autenticazione in cui l'utente effettua l'accesso con il proprio account Google. Dopo l'accesso, all'utente viene chiesto se sono disposti a concedere le autorizzazioni richieste dall'applicazione. Questo processo è chiamato consenso dell'utente.

Se l'utente concede l'autorizzazione, il server di autorizzazione di Google invia all'applicazione un token di accesso (o un codice di autorizzazione che l'applicazione può utilizzare per ottenere un token di accesso). Se l'utente non concede l'autorizzazione, il server restituisce un errore.

Qui è per il terreno di gioco oauth che aiuta a comprendere i concetti di Oauth.

Inoltre, controllare questo link per contatti API.

Problemi correlati