Integrating Facebook Accounts

This page covers how to allow your user to sign up for and log in to your mobile app with their Facebook account. Just making some small configuration on the developer portal and executing a few REST APIs are all you need to make your application ready!

Configuring Facebook Integration

Create a Facebook App

To get started, you first need to create a Facebook App and get your Facebook Application ID.

Please refer the official guide for the detailed instructions.

You will need to fill in the following fields (please put your AppID in ${your-app-id}):

  • Display Name
  • Namespace
  • Category
  • Valid OAuth redirect URIs (After creating the application, add "Facebook Login" in the Production section and select the "Settings")
    • United States: https://${your-app-id}.us.kiiapps.com/api/apps/${your-app-id}/integration/webauth/callback
    • Japan: https://${your-app-id}.jp.kiiapps.com/api/apps/${your-app-id}/integration/webauth/callback
    • Singapore: https://${your-app-id}.sg.kiiapps.com/api/apps/${your-app-id}/integration/webauth/callback
    • Europe: https://${your-app-id}.eu.kiiapps.com/api/apps/${your-app-id}/integration/webauth/callback

Configuring a Kii application

Take the following steps in the developer portal to configure your application by setting your Facebook Application ID.

  1. Click on the "Edit" button in your application console at the developer portal.

  2. Click on "Settings" to bring up the application's configuration menu.

  3. Set your Facebook App ID and App Secret in the "Social Network Connector" section.

    Note: The section "Native Application" is only there for the backward compatibility. This configuration will be deprecated when Facebook terminates its FQL support (planned to be done in April, 2015). If you were setting your App ID in this section, please consider filling the "Social Network Connector" section as soon as possible. The values set in the "Social Network Connector" section will override the value set in the "Native Application" section.

Get the Facebook Access Token

Your application must get the Facebook access token by itself. See this document for more details.

Logging in with a Facebook account

Here is an example for letting a new user logs in with theirFacebook account. Put the Facebook access token in {ACCESS_TOKEN_PROVIDED_BY_FACEBOOK}.

curl -v -X POST \
  -H "Authorization: Basic {BASE64_ENCODED_APPID_AND_APPKEY}" \
  -H "Content-Type: application/vnd.kii.AuthTokenFacebookRequest+json" \
  "https://api.kii.com/api/apps/{APP_ID}/integration/facebook" \
  -d '{
    "accessToken": "{ACCESS_TOKEN_PROVIDED_BY_FACEBOOK}"
  }'

You log in with Basic Authentication. Replace {BASE64_ENCODED_APPID_AND_APPKEY} with a Base64-encoded string of concatenated AppID and an arbitrary value with a colon (:) in between the two values.

Once the user's identity has been verified with Facebook, Kii Cloud will check if the user is new or existing within your application. If it is a new user, Kii Cloud will automatically create a new account if necessary; Kii Cloud extracts his/her email address from Facebook and checks if there exists a Kii account with the same email address. If such an account exists, Kii Cloud will not execute the sign-up process, but it will link the specified Facebook account with this account.

Upon the successful sign-up/login, Kii Cloud will return an access token for the user along with this user's User ID and the expiration duration of the token in sec:

< 200
< Content-Type: application/json
<
{
  "id" : {USER_ID},
  "access_token" : {ACCESS_TOKEN},
  "expires_in" : 9223372036854775,
  "new_user_created": true
}

The "new_user_created" key will be set as true if Kii Cloud performed a registration for the new user.

Linking a Kii account to a Facebook account

By linking existing applications with their social accounts, they will have an alternative to authentication - in addition to their existing passwords.

curl -v -X POST \
  -H "Authorization: Bearer {ACCESS_TOKEN}" \
  -H "Content-Type: application/vnd.kii.LinkFacebookRequest+json" \
  "https://api.kii.com/api/apps/{APP_ID}/users/me/facebook/link" \
  -d '{
    "accessToken": "{ACCESS_TOKEN_PROVIDED_BY_FACEBOOK}"
  }'

If successful, Kii Cloud will respond as shown below:

< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Date: Mon, 14 May 2012 22:55:41 GMT
<

Unlinking a Kii account from a Facebook account

You can unlink the existing users from their Facebook accounts in the following manner:

curl -v -X POST \
  -H "Authorization: Bearer {ACCESS_TOKEN}" \
  "https://api.kii.com/api/apps/{APP_ID}/users/me/facebook/unlink" \
  -d ""

If successful, Kii Cloud will respond as shown below:

< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Date: Mon, 14 May 2012 22:55:41 GMT
<