DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Google API – Get Contact List

Google API – Get Contact List

Andrey Prikaznov user avatar by
Andrey Prikaznov
·
Aug. 21, 12 · Interview
Like (1)
Save
Tweet
Share
7.64K Views

Join the DZone community and get the full member experience.

Join For Free
in our new tutorial i am going to tell you about inviting friends. i think that this is the most important part for every website, even a key to success. today i will show you how to create a simple and effective gmail contact importer using oauth authorization and api. also, i will talk about obtaining google api access too.

as the first step – lets prepare our own project in google api console, please open this link and create your project. then we need goto ‘api access’ section and click ‘create an oauth 2.0 client id’ button. now we should fill a name for our new project:

google contacts api - step 1

click next, and, at the second step we should set url of our destination page:

google contacts api - step 2

finally, we’ve got our client id and secret (or – consumer key and secret):

google contacts api - step 3

now – download the source files and lets start coding!

live demo
download in package

now, please create empty index.php file and put next code:

index.php

<?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 = 'your_google_client_id';
$sclientsecret = 'your_google_client_secret';
$scallback = 'http://www.script-tutorials.com/demos/291/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('classes/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 ?>

</body>
</html>

as you can see – in the beginning we include ‘gmailoath.php’ library. this library you can download here . once you have downloaded it – pay attention to the code. as you can see – the main functionality is separated into 2 sections: authorization and fetching of contact list. as usual – i put my comments in this code to better understanding.

when we click authorization button, it will open google authorization page, where we should grant access for our application to get our contact list:

google contacts api - step 4


live demo
download in archive

conclusion

if you have any suggestions about further ideas for articles – you are welcome to share them with us. good luck in your work!

API Google (verb) Contacts (Apple)

Published at DZone with permission of Andrey Prikaznov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Real Democratization of AI, and Why It Has to Be Closely Monitored
  • Best Practices for Writing Clean and Maintainable Code
  • Implementing Adaptive Concurrency Limits
  • GPT-3 Playground: The AI That Can Write for You

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: