Google API explorer
Google has APIs for multiple purposes. For example, I could use a Google API to create short urls (like http://goo.gl/Mp8EjW)) or communicate with Google Plus. Google has set up an API explorer webpage where every developer can find information about any Google API. Although this is nice, I could not find the API I was looking for. In this case, it seems that information about Google contacts is missing, even though an API for managing contacts does exist.
Google Apps documentation
- Load the library
- Set the API key
- Set the scopes
- Set the client ID
- Call an authentication method to get an access token
- Load an API with an http request
- Call the API
In the first 4 steps I did not run into any issues. However, when trying to load the contacts API, the server responded that it did not exist.
Loading any other API that was listed in the API explorer was no problem though as loading the urlshortener did not result in an error.
1. Load the library
2. Set the API key
3. Set the scopes
4. Set the client ID
5. Call an authentication method to get an access token
Now, instead of using the
gapi library, use this access token to call the API.
6. Call the API using an http request with the access token retrieved
For example, to retrieve the contacts of our user (I used the
get method from jQuery):
7. Process the results to serve the information you need.
I do recommend to use the library whenever possible as this will reduce the amount of boilerplate code you have to write.
As a result, this code snippet will provide you with the contacts of a user. To serve this file, I used a simple python command to start an http server in the correct directory with:
python -m SimpleHTTPServer
When running this example, check the console’s log to see the result. For Magnet.me we changed this to an implementation with AngularJS but the approach remains the same.