A client-side Javascript SDK for authenticating with OAuth2 (and OAuth1 with a oauth proxy) web services and querying their REST API's. HelloJS Standardizes paths and responses to common API's like Google Data Services, Facebook Graph and Windows Live Connect. Its modular so that list is growing. No more spaghetti code!
Looking for more? HelloJS supports a lot more actions than just getting the users profile. Like, matching users with a users social friends list, sharing events with the users social streams, accessing and playing around with a users photos. Lets see if these whet your appetite ...
Windows | More.. | |||
---|---|---|---|---|
Profile: name, picture (email) | ✓ | ✓ | ✓ | |
Friends/Contacts: name, id (email) | ✓ | ✓ | ✓ | |
Albums, name, id, web link | ✓ | ✓ | ✓ | |
Photos in albums, names, links | ✓ | ✓ | ✓ | |
Photo file: url, dimensions | ✓ | ✓ | ✓ | |
Create a new album | ✓ | ✓ | ||
Upload a photo | ✓ | ✓ | ||
Delete an album | ✓ | ✗ | ||
Status updates | ✗ | ✓ | ✓ | |
Update Status | ✓ | ✓ | ✗ |
Items marked with a ✓ are fully working and can be tested here. Items marked with a ✗ aren't provided by the provider at this time. Blank items are work in progress, but there is good evidence that they can be done. Anything not listed i have no knowledge of and would appreciate input.
Quick start shows you how to go from zero to loading in the name and picture of a user, like in the demo above.
Register your app domain Include hello.js script Create the signin buttons Setup listener for login and retrieve user info. Initiate the client_ids and all listeners
Register your application with atleast one of the following networks. Ensure you register the correct domain as they can be quite picky
<script class="pre" src="./dist/hello.all.min.js"></script>
Just add onclick events to call hello.login(network). Style your buttons as you like, i've used zocial css, but there are many other icon sets and fonts
<button onclick="hello.login('windows')">windows</button>
Lets define a simple function, which will load a user profile into the page after they signin and on subsequent page refreshes. Below is our event listener which will listen for a change in the authentication event and make an API call for data.
hello.subscribe('auth.login', function(auth){
// call user information, for the given network
hello.api( auth.network + '/me', function(r){
if(!r.id || !!document.getElementById(r.id) ){
return;
}
var target = document.getElementById("profile_"+ auth.network );
target.innerHTML = '<img src="'+ r.picture +'" /> Hey '+r.name;
});
});
Now let's wire it up with our registration detail obtained in step 1. By passing a [key:value, ...] list into the hello.init function. e.g....
hello.init({
facebook : FACEBOOK_CLIENT_ID,
windows : WINDOWS_CLIENT_ID,
google : GOOGLE_CLIENT_ID
},{redirect_uri:'redirect.html'});
That's it. The code above actually powers the demo at the start so, no excuses.
Initiate the environment. And add the application credentials
hello.init( {facebook: id, windows: id, google: id, .... } )
name | type | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
credentials |
object( key => value, ... )
| ||||||||||||||||||
options | set's default options, as in hello.login() |
hello.init({
facebook : '359288236870',
windows : '000000004403AD10'
});
If a network string is provided: A consent window to authenticate with that network will be initiated. Else if no network is provided a prompt to select one of the networks will open. A callback will be executed if the user authenticates and or cancels the authentication flow.
name | type | example | description | argument | default | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
network | string | windows, | One of our services. | required | null | ||||||||||||||||||||||||||||||
options |
object
| ||||||||||||||||||||||||||||||||||
callback | function | function(){alert("Logged
in!");} | A callback when the users session has been initiated | optional | null |
hello.login('facebook', function(){
alert("You are signed in to Facebook");
});
Remove all sessions or individual sessions.
name | type | example | description | argument | default |
---|---|---|---|---|---|
network | string | windows, | One of our services. | optional | null |
callback | function | function(){alert("Logged
in!");} | A callback when the users session has been initiated | optional | null |
hello.logout('facebook', function(){
alert("Signed out");
});
Get the current status of the session, this is an synchronous request and does not validate any session cookies which may have expired.
name | type | example | description | argument | default |
---|---|---|---|---|---|
network | string | windows, | One of our services. | optional | current |
alert((hello.getAuthResponse('facebook')?"Signed":'Not signed') + ' into FaceBook, ' +( hello.getAuthResponse('windows')?"Signed":'Not signed')+"into Windows Live");
Make calls to the API for getting and posting data
name | type | example | description | argument | default |
---|---|---|---|---|---|
path | string | /me, /me/friends | Path or URI of the resource. See REST API, Can be prefixed with the name of the service | required | null |
method | get, post, delete, put | See type | HTTP request method to use. | optional | get |
data | object | {name: |
A JSON object of data, FormData, HTMLInputElement, HTMLFormElment to be sent along with a get, postor putrequest | optional | null |
callback | function | function(json){console.log(json);} | A function to call with the body of the response returned in the first parameter as an object, else boolean false | optional | null |
hello.api("me", function(json){
if(!json||json.error){
alert("Whoops!");
return;
}
alert("Your name is "+ json.name);
});
Bind a callback to an event. An event maybe triggered by a change in user state or a change in some detail.
event | description |
---|---|
auth | Triggered whenever session changes |
auth.login | Triggered whenever a user logs in |
auth.logout | Triggered whenever a user logs out |
auth.update | Triggered whenever a users credentials change |
var sessionstart = function(){
alert("Session has started");
};
hello.subscribe("auth.login",sessionstart);
Remove a callback, both event name and function must exist
hello.unsubscribe("auth.login",sessionstart);
Scopes define what services your asking the user to grant your app permission to. As you can gather these can be quite intrusive so only ask permission for scopes which are obvious to your app. You may always request more from the user later.
The table shows how the providers have different scopes, however HelloJS sync's the names, so you only have to ever know what HelloJS calls them.
Select the scopes you want to use and update the current session.
scope | Windows Live | ||
---|---|---|---|
default | wl.signin, wl.basic | https://www.googleapis.com/auth/plus.me | |
wl.emails | |||
birthday | user_birthday | wl.birthday | |
events | user_events | wl.calenders | |
photos videos | user_photos,user_videos | wl.photos | |
friends | https://www.google.com/m8/feeds | ||
publish | publish_streams | wl.share | |
create_event | create_event | wl.calendars_update,wl.events_create | |
offline_access | offline_access | wl.offline_access |
google facebook windows
hello.js translates paths if there are common paths across services.
hello.js | Windows Live | ||
---|---|---|---|
OAuth2 endpoint | http://www.facebook.com/dialog/oauth/ | https://oauth.live.com/authorize | https://accounts.google.com/o/oauth2/auth |
REST API base | https://graph.facebook.com/ | https://apis.live.net/v5.0/ | https://www.googleapis.com/ |
me | me | me | plus/v1/people/me?pp=1 |
me/friends | me/friends | me/friends | https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=1000 |
me/share | me/feed | me/share | n/a |
me/albums | me/albums | me/albums | n/a |
These are some of the paths you can use with hello.api( path, callback ). e.g. hello.api("me", function(json){console.log(json);}) Below is a REST playground, clicking the paths on the left will return the results on the right
Windows FaceBook Google
e.g...
- /me
- /me/friends
- /me/feed
- /me/home
- /me/photos
- /me/albums
- /me/videos
- /me/events
Runclear update your scopes and permissions
For hello.api([path], [callback]) the first parameter of callback upon error will be either boolean (false) or be an error object as described below.
name | type | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
error | object
|
Services are added to HelloJS as "modules" for more information about creating your own modules and examples, go to Modules
OAuth 1.0 and OAuth 1.0a require a server side handshake with the secret key. OAuth 1 does not use the redirect_uri to verify the application, unlike OAuth2. Making HelloJS work with OAuth1 therefore requires a proxy server to sign requests.
By default the service uses http://auth-server.herokuapp.com/ as its proxy. Developers may add their own network registration AppID/client_id and secret to this service thus supporting OAuth1.0 and OAuth1.0a web services.
Browser | IE10 | IE9 | IE8 | IE7 | FF | CR | SA | OP | Mob | Mini5 | iOS | WP 7 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
hello.js | ✓ | ✓ | ✓ | ✓1,2 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓3 | ✓ | ✓4 |
IE7: Makes beeping sounds whenever the POST, PUT or DELETE methods are used - because of the XD, IFrame+Form+hack.
- IE7: Requires JSON.js and localStorage shims
- Opera Mini: Supports inline consent only, i.e. reloads original page.
- WP7: Supports inline consent only, i.e. reloads original page.