Quantcast
Channel: Geek O Space » javascript
Viewing all articles
Browse latest Browse all 9

Syncing the contacts from 3rd party web application with google contacts using javascript API

$
0
0

Hello,

It was a kinda of painful job for me to sync the contacts information which is available in my web apps database and allow users to add those contacts to tbe synced with the users google contacts so that they can access this contacts information from a click of the button.

After complete the job I realized its just a non-sense thing. This is what happens when you start programming

 

This can be achieved using google java api’s which is quite complicated to use for the novice , I would be writing about using different api’s of google for different scenarios too in my upcoming posts.

Before you start trying this code make sure you go through the entire document available at this link

http://code.google.com/apis/contacts/docs/1.0/developers_guide_js.html

You can also experiment with the api’s in the same page to try just scroll down in the same page.

Also refer http://code.google.com/apis/gdata/docs/js.html

 

Okay, let dirt our hands with coding which is fun and exciting too,

Create a simple HTML page and add the following code to that page, I know you guys are experts in that Ctrl+C and Ctrl+V

<!DOCTYPE HTML>
<head> <title> Google contacts </title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="auth.js" > </script>
</head>

<body>
<h1> Google contacts </h1>
<img src="http://localhost:8080/Contacts/rss_icon.jpg" width="100" height="100" />
<input type="button" value="login" onclick="logMeIn()" />
<input type="button" value="logout" onclick="logMeOut()" />
<input type="button" value="createContact" onclick="createContact()" />

</body>
</html>

Create  an javascript file and make sure the extension should be .js and just Ctrl+c and Ctrl+v this code,

 

 google.load( 'gdata', '1.x' );

 var contactsService;

function setupContactsService() {
  contactsService = new google.gdata.contacts.ContactsService('GoogleInc-jsguide-1.0');

}
function logMeIn() {
	// contactsService = new google.gdata.contacts.ContactsService('GoogleInc-jsguide-1.0');
	setupContactsService();
	var scope = 'https://www.google.com/m8/feeds';
 var token = google.accounts.user.login(scope);

}


function logMeOut() {
  google.accounts.user.logout();
}

function createContact() {

/*
 * Create a contact entry
 */

// Create the contacts service object

	 contactsService = new google.gdata.contacts.ContactsService('GoogleInc-jsguide-1.0');
// The feed URI that is used to create a contact entry
var feedUri = 'https://www.google.com/m8/feeds/contacts/default/full';

// Create an instance of ContactEntry
var entry = new google.gdata.contacts.ContactEntry();

// Set the name of the contact
entry.setTitle(google.gdata.Text.create('testing'));

// Set the content of the contact
entry.setContent(google.gdata.Text.create('content info here'));

// Create an email instance
var email = new google.gdata.Email();
email.setAddress('i@geekospace.com');
email.setPrimary(true);
// Designate this email as the "home" email
email.setRel(google.gdata.Email.REL_HOME);

// Add the email instance
entry.setEmailAddresses([email]);

// The callback method that will be called after a successful insertion from insertEntry()
var callback = function(result) {
  document.getWriter='contact entry created!';
}

// Error handler will be invoked if there is an error from insertEntry()
var handleError = function(error) {
  document.getWriter='error';
}

// Submit the request using the contacts service object
contactsService.insertEntry(feedUri, entry, callback,
    handleError, google.gdata.contacts.ContactEntry);
	}

Please note: You need to take of one thing when you are working with google javascript client library,you should add
an image in the HTML page and you should provide the absolute path of that image.
I wanna play a prank with this code:  	Change this line of code
var feedUri = 'https://www.google.com/m8/feeds/contacts/default/full';
to
var feedUri = 'http://www.google.com/m8/feeds/contacts/default/full';
and let me know what happens, let see how smart are you???
Cheers!!!

 


Viewing all articles
Browse latest Browse all 9

Trending Articles