Site icon Tutorial

Login API

Facebook Login for Apps is a fast and convenient way for people to create accounts and log into your app across multiple platforms. It’s available on iOS, Android, Web, Windows Phone, desktop apps and devices such as Smart TVs and Internet of Things objects.

Use Cases

Facebook Login is used to enable the following experiences:

Features

Permissions

When a person logs into your app via Facebook Login you can access a subset of that person’s data stored on Facebook. Permissions are how you ask someone if you can access that data. A person’s privacy settings combined with what you ask for will determine what you can access.

Facebook Login Example – Permissions are strings that are passed along with a login request or an API call. Here are two examples of permissions:

For example, if you add the login button to a web app and ask for email and user_likes via the scope parameter, a person would be prompted with this dialog when logging in for the first time:

Your app has requested a person’s email address and the things they like but that request also automatically asks for access to a person’s public profile.

When to ask for Permissions – Your app can ask for additional permissions at any time, even after a person logs in for the first time. For example, the publish_actions permission lets you post to a person’s Facebook Timeline. It’s recommended you ask for this permission only when a person is ready to publish a story to Facebook. When you ask for new permissions, the person using your app will be asked about those new permissions and has the ability to opt out.

Permissions only need to be granted once per app, i.e. permissions granted on one platform are effectively granted on all the platforms your app supports.

User Control – Facebook Login allows a person to grant only a subset of permissions that you ask for to your app, except for public profile, which is always required. This is available as a separate screen in the login dialog when you ask for permissions:

Your app should handle the case where someone had declined to grant your app one of the permissions you requested.

Revoked Permissions – People can also revoke permissions granted to your app in Facebook’s interface at any time after they have logged in. It is important that your app regularly checks which permissions have been granted, especially when launching on a new platform.

Granular Permissions – People can grant your app permissions for Pages, Groups, and business assets they manage at the individual level. For example, someone who manages several Pages, may grant your app permission for only a particular page or for only some of the Page.

People choose which permissions they grant through a permission request flow. For example, if an app requests Page and Groups permission, people receive a request to grant those permissions.

If they don’t grant all the requested permissions, they can manage what sorts of permissions they grant.

They can also choose which Pages, Groups, or business assets they grant permissions for.

If someone initially grants only some of the requested permissions, they can later change which permissions they allow through the app settings page. However, if they update this to grant all permissions, they will no longer be able use the app settings page to change the permissions they have granted.

People can manage the following permissions at the individual level:

Read and Write Permissions

Each permission has its own set of requirements and suggested use cases. All these permissions, except the default fields, require that you have Client OAuth Login enabled for your app on the Facebook Login tab of your app dashboard.

The default fields and the email permission do not require Login Review, but all other permissions do. Access to the Public Feed API is restricted to a limited set of media publishers and usage requires prior approval by Facebook.

Read Permissions – User Attributes

Read Permissions – User Activity

Read Permissions – User Events and Groups

Write Permission – User Events and Groups

Default Fields – The default fields are a subset of the parts of a person’s public profile. These fields are the following properties on the User object:

On the web, the default fields are implied with every request and you don’t need to declare them, although the best practice is to declare them with public_profile. On iOS and Android, you must manually request these fields with public_profile, as part of your login flow. The id field is an app-scoped ID.

Access Tokens

When someone connects with an app using Facebook Login and approves the request for permissions, the app obtains an access token that provides temporary, secure access to Facebook APIs.

An access token is an opaque string that identifies a user, app, or Page and can be used by the app to make graph API calls. Access tokens are obtained via a number of methods, each of which are covered later in this document. The token includes information about when the token will expire and which app generated the token. Because of privacy checks, the majority of API calls on Facebook need to include an access token. There are different types of access tokens to support different use cases:

Access Token TypeDescription
User Access TokenThe user token is the most commonly used type of token. This kind of access token is needed any time the app calls an API to read, modify or write a specific person’s Facebook data on their behalf. User access tokens are generally obtained via a login dialog and require a person to permit your app to obtain one.
App Access TokenThis kind of access token is needed to modify and read the app settings. It can also be used to publish Open Graph actions. It is generated using a pre-agreed secret between the app and Facebook and is then used during calls that change app-wide settings. You obtain an app access token via a server-to-server call.
Page Access TokenThese access tokens are similar to user access tokens, except that they provide permission to APIs that read, write or modify the data belonging to a Facebook Page. To obtain a page access token you need to start by obtaining a user access token and asking for the manage_pages permission. Once you have the user access token you then get the page access token via the Graph API.
Client TokenThe client token is an identifier that you can embed into native mobile binaries or desktop apps to identify your app. The client token isn’t meant to be a secret identifier because it’s embedded in apps. The client token is used to access app-level APIs, but only a very limited subset. The client token is found in your app’s dashboard. Since the client token is used rarely, we won’t talk about it in this document. Instead it’s covered in any API documentation that uses the client token.

Refreshing User Access Tokens – User access tokens are either short-lived or long-lived. However, all access tokens must be renewed every 90 days with the consent of the person using your app. This means that every 90 days you must send a person through the login process and they must agree to the login by clicking the Continue button.

When you use iOS, Android, or our JavaScript SDK, the SDK will handle making sure that tokens are refreshed before they expire during this 90-day period. Native mobile apps using Facebook’s SDKs get long-lived access tokens, good for about 60 days. These tokens will be refreshed once per day, for up to 90 days, when the person using your app makes a request to Facebook’s servers. If no requests are made, the token will expire after about 60 days and the person will have to go through the login flow again to get a new token.

Access tokens on the web often have a lifetime of about two hours, but will automatically be refreshed when required. If you want to use access tokens for longer-lived web apps, especially server side, you need to generate a long-lived token. A long-lived token generally lasts about 60 days. This is what the process for generating a long-lived token looks like:

To generate a long-lived token:

GET /oauth/access_token?

grant_type=fb_exchange_token&

client_id={app-id}&

client_secret={app-secret}&

fb_exchange_token={short-lived-token}

Make this call from your server, not a client. The app secret is included in this API call, so you should never actually make the request client-side. Instead implement server-side code that makes the request, then pass the response containing the long-lived token back to your client-side code. This will be a different string than the original token, so if you’re storing these tokens, replace the old one.

Once you’ve retrieved the long-lived token, you can use it from your server or ship it back down to the client to use there.

An important note: Apps are unable to exchange an expired short-lived token for a long-lived token. The flow above only works with short-lived tokens that are still valid. Once they expire, your app must send the user through the login flow again to generate a new short-lived token.

Extending Page Access Tokens – Apps can retrieve a page access token from Page admin users when they authenticate with the manage_pages permission. If the user access token used to retrieve this page access token is short-lived, the page access token will also be short-lived.

To get a longer-lived page access token, exchange the User access token for a long-lived one and then request the Page access token. The resulting page access token will not have any expiry time.

Login Security

The features of Facebook Login such as access tokens and permissions make it safe and secure for people and apps to use, but there are some security steps that apps need to implement themselves.

Security Checklist – This list below should be considered the absolute minimum that all apps using Facebook Login should implement. Other features will be unique to your app and you will need to always think about how to make your app as secure as possible. Apps that are not secure will lose the trust of their audience and people will stop using them.

The App Secret – The App Secret is used in some of the Login flows to generate access tokens and the Secret itself is intended to secure usage of your App to only those that are trusted. The secret can be used to easily create an App Access Token which can make API requests on behalf of any user of the app, which makes it extremely important that an App Secret is not compromised.

Therefore the App Secret or an App Access token should never be included in any code that could be accessed by anyone other than a developer of the app. This applies to all methods of code that are not secured like client-side code (such as HTML or Javascript) or native apps (such as iOS, Android or Windows desktop apps) that could be decompiled.

We recommend that App Access Tokens should only be used directly from your app’s servers in order to provide the best security. For native apps, we suggest that the app communicates with your own server and the server then makes the API requests to Facebook using the App Access Token. For this reason, if your ‘App Type’ under Advanced Settings in the App Dashboard is set to Native/Desktop we assume that your native app contains the App Secret or an App Access Token in the binary, and we do not allow calls signed with an App Access Token to proceed. The API will behave as though no access token was provided.

If your App Secret is compromised, you should reset it immediately in the Basic Settings of your App Dashboard. When you start the reset process, you can specify a number of hours that the compromised secret will continue to work for when making requests, however anything sent from Facebook (such as signed requests) will use the new secret straight away, so you must adjust your code to expect it as soon as possible.

Secure Server-side Calls with appsecret_proof – You can reduce your exposure to malware and spammers by requiring server-to-server calls to Facebook’s API be signed with the appsecret_proof parameter.

Secure Client-side Calls with Short-term Tokens and Code Flow – In some configurations, apps will reuse a long-term token across multiple clients. Don’t do this. Instead use short-term tokens that are generated with the code flow.

Token Hijacking – To understand how this happens, imagine a native iOS app that wants to make API calls, but instead of doing it directly, communicates with a server owned by the same app and passes that server a token generated using the iOS SDK. The server would then use the token to make API calls.

The endpoint that the server uses to receive the token could be compromised and others could pass access tokens for completely different apps to it. This would be obviously insecure, but there is a way to protect against this – access tokens should never be assumed to be from the app that is using them, instead they should be checked using debugging endpoints.

State Parameter – If you’re using the Facebook login dialog on your website, the state parameter is a unique string that guards your application against Cross-site Request Forgery attacks.

Enable Strict Mode – Strict Mode keeps apps safe by preventing bad actors from hijacking your redirect. Enabling Strict Mode is required for all apps.

Before turning on Strict Mode in the App Dashboard, ensure your current redirect traffic still works by taking the following actions in Facebook Login settings:

After taking these actions, make sure to enable strict mode.

How Strict Mode Works – Strict Mode prevents hijacking of your redirect URIs by requiring an exact match from your Valid OAuth redirect URIs list. For example, if your list contains www.example.com, then Strict Mode won’t allow www.example.com/token as a valid redirect. It also won’t allow any extra query parameters not present in your Valid OAuth redirect URIs list.

Lock Down Your Facebook App Settings – Enable and/or disable any authentication flows that the app does not use to minimize attack surface area.

The App Dashboard contains a number of additional settings which allow developers to shut down areas of attack that might otherwise lead to security issues:

Use HTTPS – Use HTTPS, instead of HTTP, for redirects, the JavaScript SDK, and as an internet protocol, because it uses encryption. HTTPS keeps transmitted data private and guards against eavesdropping attacks. It also prevents data from being tampered with during transmission by, for example, introducing advertisements or malicious code.

An Enforce HTTPS setting for Facebook Login is available in your App Dashboard. When you turn it on, it requires all Facebook Login redirects to use HTTPS, and that Facebook JavaScript SDK calls that return or require an access token are only from HTTPS pages. If not all of your URLs are HTTPS or HSTS, you should update your pages to work over HTTPS and turn on the Enforce HTTPS setting in your Facebook Login settings.

To enforce HTTPS in redirects and for the JavaScript SDK:

In the near future, this setting will be required.

Testing a Login Flow

It’s important to test and verify that your Facebook Login flow works well under a variety of conditions. To test your Login flow, first create a separate Facebook user account:

Common Test Cases – Before you test each use case below, make sure you remove your app from your test user’s Facebook account using app settings.

  1. Someone new to your app logs in with Facebook
  1. Someone logs in with Facebook after previously logging in via a non-Facebook flow with the same email address
  1. Someone who has logged into your app with Facebook in the past logs back in
  1. Someone cancels log in with Facebook and tries to log in again
  1. Someone removes your app from Facebook via app settings and revisits your app. Your app should detect this and prompt the person to log back in.
  1. Someone changes the Facebook password after logging in with Facebook to your app

In this case, your token will be invalid and you should notify users that their Facebook session has expired and ask them to log in again.

  1. Someone disabled Facebook platform via app settings and logs in to your app

In this case, you should make sure your app detects the error so that it can notify users and redirect them to the non-iOS integrated version of Facebook Login.

  1. For games that want to sync their status across multiple devices, test your syncing state

Best Practices

  1. Prompt people to log in at the right time

If your app is well known and understood, you might be able to put your login button on the initial screen and still see decent conversion rates. If you do this, be sure the intro screen has a clear, succinct and compelling statement about what it has to offer:

A better option is to provide a glimpse of the content available to people prior to logging in, like the background photo in this example:

If your app requires additional education, you may want to offer a multi step demo above your login button. This gives people the option to either log in immediately or learn more first.

The best experience is to let people use your app and see its content before prompting them to log in. For example, many ecommerce sites such as Zulily don’t require people to log in until they’re ready to check out.

  1. Only ask for the permissions you need

Only ask for the permissions you need. The fewer permissions you ask for, the easier it is for people to feel comfortable granting them. We’ve seen that asking for fewer permissions typically results in greater conversion.

You can always ask for additional permissions later after people have had a chance to try out your app.

An additional benefit of asking for fewer permissions is that you might not need to submit your app for App Review. You need to submit for App Review if you request any permissions other than public_profile and email.

  1. Ask for permissions in context and explain why

You should trigger permission requests when people are trying to accomplish an action in your app which requires that specific permission.

For example, the Facebook app only asks for Location Services when people explicitly tap on the location button when updating their status.

In addition, people are most likely to accept permission requests when they clearly understand why your app needs that info to offer a better experience.

  1. Use the button that comes with SDKs

The Facebook Login button that comes with our SDKs is easy to integrate and includes built-in education that ensures a consistent design and experience:

However, if you do decide to build your own, for best results follow the Facebook Platform Policy and the recommendations in the User Experience Design topic.

  1. Avoid having people login from a WebView

Logging in from a WebView works only if people have the Facebook app installed on their mobile device. Because you cannot predict whether people will have the app installed, it’s better not to have them log in from a WebView.

  1. Provide a way to log out

Once people are logged in, you should also give them a way to log out, disconnect their account, or delete it all together. In addition to being a courtesy, this is also a requirement of our Facebook Platform Policy.

The dating app Tinder, for example, gives you the option to log out or to delete your account entirely.

  1. Test and measure

It’s incredibly important to test your Facebook Login flow under a variety of conditions, and we’ve built a robust testing plan for you to follow. It’s also a good idea to run qualitative usability tests to understand how people are reacting to what they see.

Once you’ve tested your Login flow and are ready to launch, we suggest using an analytics program to understand if people are completing the process and their overall conversion rates. Best practice apps can see conversion rates of over 80%. Facebook Analytics lets you monitor your conversion rates for free.

  1. Follow the Facebook Platform Policy

To avoid potential problems later on, do a quick check to make sure your Login integration adheres to the login section of our policies.

  1. Submit your app for App Review

You only need to submit your app for App Review if you’re requesting permissions beyond public profile and email. We recommend you submit your app for review as early as possible in your development lifecycle after you’ve integrated Facebook Login. You’ll receive transparent feedback during the App Review process, including feedback on changes you can make to get a denied permission approved, if appropriate. For existing apps, going through Login Review will not affect your current app.

Exit mobile version