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:
Invite friends from Google

Then Click on API Access to Create an OAuth 2.0 Client ID
Invite Friends

Fill up the form to create your project
Gmail friend list

Setting your Client ID
Gmail contact list in php

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>";
}
?>

Other Related Topics

Import Contacts from Hotmail

Import Contacts from Yahoo!



 

Download all files from here 

Download Project Files Total Downloads: 3395

Comments
05 March
2014
nilesh k
Good example. Can anyone help with the code which will retrive user name or email within this code.
02 January
2014
kannan
Nice this code ,it's working fine
05 December
2013
Soutam Saha
Its working fine now. Thanks. Nice work.
04 December
2013
Soutam Saha
I'm getting error. It's saying invalid client, no application name. i got the application name. but cnt find the place where to put. can you please suggest. please check the error.


Error: invalid_client
no application name
Request Details
from_login=1
cookie_policy_enforce=false
scope=https://www.google.com/m8/feeds/
response_type=code
access_type=online
redirect_uri=http://www.mindpaceacademy.com/Test/Gmail/result.php
as=542bb964ac53249f
display=page
pli=1
client_id=515145740670-m2ulvdhlr6hd48fq1f7qvjj3toq58eaj.apps.googleusercontent.com
authuser=0
hl=en
23 September
2013
george
How can I grab the name and email address of the user so that I send it alongside the invitation email
24 August
2013
george
Now that we have retrieved the contact and listed them numerically, how can we grab them and put them in the mail() function so that we send them to the specified email addresses.
25 August
2013
Rishi
Please use the following code to send mail. You can call the mail function in foreach().

$subject = "Your mail subject here.";
$message = "You can use your HTML text content here";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . "\r\n";

foreach ($result as $title) {
$count++;
echo $count.". ".$title->attributes()->address . "

";
$to = $title->attributes()->address;
$mail = mail( $to, $subject , $message, $headers );
}
06 August
2013
Satyajit Patnaik
Now Google suggests to get authorization from
https://developers.google.com/accounts/docs/OAuth2
instead of
http://developers.google.com/accounts/docs/OAuth_ref#SigningOAuth
21 December
2013
digvijay singh
Please give me idea,how can find gmail friend Name and email Id;
08 August
2013
Site Admin
Thanks for your update.

Now the code is using OAuth 2.0.. Please download and check.
comments powered by Disqus