Quantcast
Viewing latest article 2
Browse Latest Browse All 33

Invite Friends from Gmail

Courtesy:-http://www.a2zwebhelp.com/invite-friends-from-gmail

Google has introduced OAuth 2.0 and OAuth 1.0 has been officially deprecated as of April 20, 2012. So let us create a program to invite friends from Gmail using OAuth 2.0. Before you can use OAuth 2.0, you must register your application using the  Google APIs Console. First you need to create a project and follow the steps.

Create a New project:
Image may be NSFW.
Clik here to view.

Then Click on API Access to Create an OAuth 2.0 Client ID
Image may be NSFW.
Clik here to view.

Fill up the form to create your project
Image may be NSFW.
Clik here to view.

Setting your Client ID
Image may be NSFW.
Clik here to view.

Now you will get your Client ID, Client secret and Redirect URIs that we will use in our project. You can edit your Redirect URIs by clicking on Edit Setting in the right side. Once you have all info lets create our conig.php file.

config.php

In this file you have to enter Client ID, Client secret and Redirect URIs.

<?php
$clientid = 'Your Client here';
$clientsecret = 'Your Client Secret';
$redirecturi = 'http://your path/result.php'; 
$maxresults = 50; // Number of mailid you want to display.
?>


index.php

<?php include('config.php'); ?>

<a href="https://accounts.google.com/o/oauth2/auth?client_id=
<?php print $clientid;?>&redirect_uri=<?php print $redirecturi; ?>
&scope=https://www.google.com/m8/feeds/&response_type=code">
Invite Friends From Gmail</a>

 

result.php

<?php
include('config.php');
$authcode = $_GET["code"];
$fields=array(
'code'=> urlencode($authcode),
'client_id'=> urlencode($clientid),
'client_secret'=> urlencode($clientsecret),
'redirect_uri'=> urlencode($redirecturi),
'grant_type'=> urlencode('authorization_code') );

$fields_string = '';
foreach($fields as $key=>$value){ $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');

$ch = curl_init();//open connection
curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($ch,CURLOPT_POST,5);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);

$response = json_decode($result);
$accesstoken = $response->access_token;
if( $accesstoken!='')
$_SESSION['token']= $accesstoken;
$xmlresponse= file_get_contents('https://www.google.com/m8/feeds/contacts/
default/full?max-results='.$maxresults.'&oauth_token='. $_SESSION['token']);

$xml= new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$result = $xml->xpath('//gd:email');
$count = 0;
foreach ($result as $title) {
$count++;
echo $count.". ".$title->attributes()->address . "<br><br>";
}
?>

Download all files from here

Download Project Files


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing latest article 2
Browse Latest Browse All 33

Trending Articles