Tag Archives: RESTful

Mastering Your Inbox with the Gmail JavaScript API


Mastering Your Inbox with the Gmail JavaScript API
// SitePoint

In this article, we’re going to build a basic Gmail inbox and message viewing app using the Gmail RESTful API. The purpose of this article is to give you a good starting point to create cool new JavaScript apps of your own using this API. We’re going to use jQuery and Bootstrap to take some weight out of the code base so we can focus on getting something working quickly without worrying about cross-browser JavaScript inconsistencies and basic styling.

As ever the complete code for this article can be found on our GitHub repo.

Gmail Logo

Enabling the Gmail API on Your Google Account

First off, we need to enable Gmail API access to get our API credentials. To do this, we need to visit Google’s Developer Console in our favourite web browser. From there, we need to create a project (or choose an existing one) and go to the APIs section. Choose “Gmail API” under the Google Apps APIs section and click the “Enable API” button.

Now we need to create two sets of credentials, one for an OAuth 2.0 client ID for a web application and the other to create a browser API key. This can be done in the credentials section of the Google Developer Console by clicking the “Add Credentials” button.

Add credentials screenshot

For the browser API key we need to only fill in the “name” field. However, for production I’d recommend adding a HTTP referrer (this will prevent abuse of our API key from non-authorized domains). For the OAuth 2.0 client ID we must enter at least one authorized JavaScript origin. For a local development environment this will likely be http://localhost or similar. We do not need to enter an authorized redirect URI.

Once we’ve filled in the necessary fields we should be able to see our credentials back in the Credentials section. Keep this info open in a browser tab for later.

Connecting to the Gmail API

[author_more]

Even though the Gmail API is a standard REST API using OAuth 2.0 we recommend using Google’s own JavaScript libraries for connecting to and working with any Google-authored APIs. This is because Google has already packaged up authentication logic and the required dependencies into a single include file — less work for us!

So, first things first – let’s set up our HTML file which we’re going to use as the base of our app. For the purpose of this app we’re going to include all of our code in a single HTML file. In a production environment Id recommend splitting out HTML, CSS and JavaScript into separate files.

<!doctype html> <html> <head> <title>Gmail API demo</title> <meta charset="UTF-8"> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> <style> .hidden{ display: none; } </style> </head> <body>

Gmail API demo

Authorize

//ahref= //ahref= var clientId = ‘xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com‘; var apiKey = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’; var scopes = ‘https://www.googleapis.com/auth/gmail.readonly’; http://ahref= </body> </html>

Right at the bottom (above the closing </body> tag) is where we include Google’s JavaScript client library. Notice the query string on the end, this contains the callback function which will be executed once the script has loaded — we’ll use that to initialize our app a little later. Just above that is where we are defining our API credentials, we need to paste these from the Google Developer Console Credentials section. We also define which permissions we’ll require from the user, these are known as scopes. For the purpose of this app we only require read-only Gmail access. It’s good practice to request as few permissions as possible from the user — this provides the user peace of mind that we’re not going to do something nefarious like send emails on their behalf without them knowing.

Other than that we’ve got a button which will allow the user to authorize us to access their Gmail account, and we’ve also stubbed out a table to hold our inbox data once we fetch it. And, as mentioned earlier, we’ve included the necessary files for jQuery and Bootstrap.

Authenticating the User

Now we’re going to provide a mechanism for the user to authenticate us to access their Gmail account. As mentioned above, we need to build out a function named handleClientLoad() which will automatically be called once Google’s JavaScript client library has loaded into the page. This function will then call a chain of other functions which will eventually lead us to fetching their inbox.

function handleClientLoad() { gapi.client.setApiKey(apiKey); window.setTimeout(checkAuth, 1); } function checkAuth() { gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: true }, handleAuthResult); } function handleAuthClick() { gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: false }, handleAuthResult); return false; } function handleAuthResult(authResult) { if(authResult && !authResult.error) { loadGmailApi(); $('#authorize-button').remove(); $('.table-inbox').removeClass("hidden"); } else { $('#authorize-button').removeClass("hidden"); $('#authorize-button').on('click', function(){ handleAuthClick(); }); } } function loadGmailApi() { gapi.client.load('gmail', 'v1', displayInbox); } 

We should insert this code directly below where we set the API credentials, within the same SCRIPT block.

To summarise the process this chain of function calls goes through:

  1. handleClientLoad() simply sets the API key and passes off to checkAuth() after 1 millisecond.
  2. checkAuth() checks if the user has previously authenticated our app with Google. Setting the immediate parameter to true here means that we do not prompt the user with a login/permissions modal if they are not authenticated. We then pass the authentication result to handleAuthResult().
  3. handleAuthResult() then does one of two things; if the user is already authenticated it’ll load the Gmail API using loadGmailApi(), alternatively it’ll display the authorize button on the UI and attach a click event to it which will trigger handleAuthClick()
  4. handleAuthClick() simply executes the same authentication function as checkAuth() but will present the user with a login/permissions modal. Once the user authenticates the same handleAuthResult() function from before is triggered.
  5. Once those series of functions have been executed and the user has authenticated we should always find ourselves at the loadGmailApi() function. This simply loads the Gmail API functionality from Google’s JavaScript client library and then calls our displayInbox() function.

Continue reading %Mastering Your Inbox with the Gmail JavaScript API%

Part 2: Creating RESTful APIs With NodeJS and MongoDB Tutorial


via Creating RESTful APIs With NodeJS and MongoDB Tutorial (Part II) – Adrian Mejia’s Blog.

Welcome to this RESTful API using Node.js (Express.js) and MongoDB (mongoose) tutorial. You can follow alone to make a stand alone API endpoint, or you could also check out our AngularJS or BackboneJS tutorials to build a javascript-client that connects with the endpoint we are going to built.

Creating a lightweight API wrapper with Refit


via Creating a lightweight API wrapper with Refit | Jerrie Pelser.

With the explosion of web APIs it becomes much more common for applications to integrate in some fashion with external APIs. These APIs are typically RESTful APIs, and normally there will be an official wrapper available for a wide range of programming languages and platforms to allow other developers to more easily integrate with the API.

Sometimes however this may not be the case. It may be that the service in question simply do not see providing a .NET API wrapper as a priority, or that the official wrapper may be targeting the full .NET Framework, and you may be looking for something that you can use from WinRT, Xamarin or PCL libraries in general.

It may also be that you simply do not want to include a large external dependency in your application, or you may actually need an API wrapper for one of your own APIs.

In all of these cases you may be required (or tempted) to write you own wrappers. Writing API wrappers can however be a tedious process.

In these sort of situations I revert to using Refit, which is an open-source PCL library developed by Paul Betts, and it works across just about every flavour of the .NET framework – Xamarin, WinRT, Windows Phone 8, UPW, etc.

A pure JS client for interacting with server-side RESTful resources.


via marmelab/restful.js · GitHub.

A pure JS client for interacting with server-side RESTful resources. Think Restangular without Angular.

Blindsist – Assisting Visually Impaired People using IBM Bluemix Visual Recognition


via Blindsist – Assisting Visually Impaired People using IBM Bluemix Visual Recognition | Abdul Muhaymin – Being Student!.

This blog post will cover how I developed Blindsist app for Xhacknight.

The app was developed using Xamarin.Forms, targeting 3 major platforms (iOS, Android and Windows Phone) same time.

Blindsist is a simple one page application. Objective of the application is taking a photo using Smartphone, The photo is then uploaded to IBM Bluemix using REST Api. IBM Bluemix returns the data in Json format, which will be parsed and spoken out using Native Text to Speech services.

Open a new Xamarin.Forms Portable Class Project.

NodeJS ABC’s – P is for POST


via Node.js ABC’s – P is for POST.

With the web services that are popping from your corporate networks to the wild-internet, accessing HTTP based resources is becoming a critical component to any and all application development.  One of the more popular API application design styles is Representational State Transfer  (or REST).   REST implementations are typically developed on top of HTTP with the objects you are effecting being in the URI and the actions, or verbs, being the HTTP methods.  The HTTP methods follow the four basic methods of persistent storage as referenced in the CRUD  acronym of Create, Read, Update, and Delete acronym.

Create RESTful API for Symfony2 with Apigility


via Create RESTful API for Symfony2 with Apigility | Enrico Zimuel.

Apigility is an open source project to simplify the implementation of HTTP API for PHP applications. The project has been implemented in Zend Framework 2 but it can be used to create API for any PHP application. To prove this aspect, I show in this post how to use Apigility to create a RESTful API for an existing Symfony2 project.

This use case is an extension of the proof of concept example that has been presented at thePHP Forum 2013 in Paris, during the talk “Symfony2 and Zend Framework 2: the perfect team”of Stefan Koopmanschap and myself.

I would like to thanks Alessandro Nadalin for the suggestion about the bootstrap part of Symfony2.