Releases, supported software, pricing

Releases

Facebook sign in

You can authenticate your App Services requests by logging into Facebook. To access App Services resources, you need to provide an access token with each request (unless you use the sandbox app). You can get an access token by connecting to an appropriate web service endpoint and providing the correct client credentials — this is further described in Authenticating users and application clients. However, you can also obtain an access token by logging into Facebook.

To enable authentication to App Services through Facebook, do the following in your app:

Make a login call to the Facebook API (do this using the Facebook SDK or API). If the login succeeds, a Facebook access token is returned.

Send the Facebook access token to App Services. If the Facebook access token is valid and the user does not already exist in App Services, App Services provisions a new App Services user. It also returns an App Services access token, which you can use for subsequent App Services API calls. Behind the scenes, App Services uses the Facebook access token to retrieve the user's profile information from Facebook.

If the Facebook access token is invalid, Facebook returns an OAuth authentication error, and the login does not succeed.

The request to authenticate to App Services using a Facebook access token is:

GET https://api.usergrid.com/{my_org}/{my_app}/auth/facebook?fb_access_token={fb_access_token}

where:

{my_org} is the organization UUID or organization name.{my_app} is the application UUID or application name.{fb_access_token} is the Facebook access token.

Facebook login example

In brief, here are the steps for JavaScript. You can see these steps implemented in the Facebook login example packaged with the JavaScript SDK for App Services (which you can download in ZIP format or tar.gz format). The Facebook login example is in the /examples/facebook directory of the extracted download. The code example snippets shown below are taken from the Facebook login example.

Step 1: Create a Facebook app

Create a new app on the Facebook App Dashboard. Enter your app's basic information. Once created, note the app ID shown at the top of the dashboard page.

Step 2: Invoke the Facebook OAuth dialog

Invoke the Facebook OAuth Dialog. To do that, redirect the user's browser to a URL by inserting the following Javascript code after the opening <body> tag in your app’s HTML file:

{YOUR_APP_ID} is the app ID.{YOUR_REDIRECT_URL} is the application UUID or application name.{YOUR_STATE_VALUE} is a unique string used to maintain application state between the request and callback.{COMMA_SEPARATED_LIST_OF_PERMISSION_NAMES} is a comma separated list of permission names which you would like the user to grant your application.{YOUR_RESPONSE_TYPE}is the requested response type, either code or token. Defaults to code. Set the response type to token. With the response type set to token, the Dialog's response will include an OAuth user access token in the fragment of the URL the user is redirected to, as per the client-side authentication flow.

Notice that the response type is set to token. As a result, a Facebook access token will be appended to the URL to which the user is redirected.

Step 3: Add the JavaScript SDK for Facebook

Add the following Javascript SDK initialization code after the code that invokes the Facebook OAuth Dialog. The code will load and initialize the JavaScript SDK in your HTML page. Replace YOUR_APP_ID with the App ID noted in Step 1, and WWW.YOUR_DOMAIN.COM with your own domain.

Step 4. Setup FB.login

Whenever a user is either not logged into Facebook or not authorized for an app, it is useful to prompt them with the relevant dialog. The FB.login() Javascript SDK function automatically displays the correct one to the user.

The client.loginFacebook() function is provided by the App Services JavaScript SDK. It uses the Facebook auth token to obtain an App Services auth token. If the Facebook access token is valid and the user does not already exist in App Services, the function creates a user entity for the user. It also uses the Facebook access token to retrieve the user's profile information from Facebook.