- Getting Started with the Facebook SDK for Android
- Quick Start
- Android Studio Setup
- Add Your Facebook App ID and Client Token
- Sending Images or Videos
- Running Sample Apps
- Create a Development Key Hash
- Create a Release Key Hash
- Use the Facebook SDK for Android with Maven
- Troubleshooting Sample Apps
- Android Facebook Login Integration Step By Step With Logout
- Download Source Code for android facebook integration login
- Step 1: Create a new project
- Step 2: Create an app in the Facebook developer console.
- Step 3: Updating build.gradle(Project: project_name)
- Step 4: Updating build.gradle(Module:app)
- Step 5: Changing strings resource file
- Step 6: Adding activity
- Add internet permission between …. tag.
- Step 7: Updating activity_main
- Step 8: Changing MainActivity class
- Description of MainActivity.java
- Logout from Facebook from custom button click
- Quick Tip: Add Facebook Login to Your Android App
- Premium Option
- Prerequisites
- 1. Register Your App
- 2. Add Facebook SDK to Your Project
- 3. Create an Activity
- Step 1: Define the Layout
- Step 2: Create the Class
- 4. Add the Facebook Application ID
- 5. Edit the Manifest
- 6. Build and Run
- Conclusion
Getting Started with the Facebook SDK for Android
This documentation explains how to get started integrating your Android app with Facebook by using the Facebook SDK for Android. The current version of the Facebook SDK for Android is version 12.0.0 and requires the Android API 15. For more information about versions and features, see Facebook SDK for Android.
Beginning with SDK v13.0, set to release in early 2022, a Client Token will be required for all calls to the Graph API.
You can set up your app to use the Facebook SDK for Android in the following ways:
- By using the Quick Start.
- By setting up your project with the Facebook SDK for Android.
Quick Start
To get a Facebook App ID, configure your app’s settings, and import the Facebook SDK for Android, click on the button below and follow the instructions.
Android Studio Setup
To use the Facebook SDK in an Android Studio project, add the SDK as a build dependency and import the SDK.
- Go to Android Studio | New Project | Minimum SDK.
- Select API 15: Android 4.0.3 (IceCreamSandwich) or higher and create your new project.
- After you create a new project, open Gradle Scripts | build.gradle (Project: and do the following:
- Add the following to the buildscript < repositories <>> section of the build.gradle (Project) file:
- Save and close build.gradle (Project: ) .
- Open Gradle Scripts | build.gradle (Module: app) and do the following:
- Add the following to the dependencies <> section of your build.gradle (module: app) file to compile the latest version of the Facebook SDK for Android:
When you use the Facebook SDK, some events in your app are automatically logged and collected unless you disable automatic event logging. For details about what information is collected and how to disable automatic event logging, see Automatic App Event Logging.
Add Your Facebook App ID and Client Token
Add your Facebook App ID and Client Token to your project’s strings file and update your Android manifest:
1. Open your /app/res/values/strings.xml file.
2. Add a string element with the name attribute facebook_app_id and value as your Facebook App ID to the file. For example
3. Open /app/manifests/AndroidManifest.xml
4. Add a uses-permission element to the manifest:
5. Add a meta-data element to the application element:
Sending Images or Videos
If you’re sharing links, images or video via the Facebook for Android app, you also need to declare the FacebookContentProvider in the manifest.
Append your app id to the end of the authorities value. For example if your Facebook app id is 1234 , the declaration looks like:
Running Sample Apps
The following samples come with the Facebook SDK for Android:
- HelloFacebookSample — Demonstrates profile access, status updates and photo upload.
- RPSSample — Use Native Share Dialog, Open Graph publishing, pickers, invites, and deep linking.
- Scrumptious — Login, requests, pickers, picture uploads, and Open Graph publishing.
You can experiment with samples by importing the Facebook SDK into an Android Studio project. The samples have a project dependency rather than a central repository dependency via maven central or jcenter. This is so that when a local copy of the SDK gets updates, the samples reflect the changes.
To run samples apps quickly, you can generate key hashes for your development environments. Add these to your Facebook developer profile for the sample apps. Keytool, for generating the key hashes, is included with the Java SE Development Kit (JDK) that you installed as part of setting up your development environment. OpenSSL is available for download from OpenSSL.
On Windows, you need the following:
Run the following command in a command prompt in the Java SDK folder. This generates a 28 character string.
Go to the Facebook Developer site. Log into Facebook and, using the dropdown menu in the top-right, go to Developer Settings:
In your developer settings, select Sample App from the menu, and add and save your key hash into your profile:
You can add multiple key hashes if you develop with multiple machines.
You can now compile and run all of the samples — including those that use Facebook Login.
Create a Development Key Hash
Facebook uses the key hash to authenticate interactions between your app and the Facebook app. If you run apps that use Facebook Login, you need to add your Android development key hash to your Facebook developer profile.
For the version of your app that you release to you also need to generate and set a Release Key Hash.
On either OS X or Windows you can get a key hash by generating it or by using the value returned by Settings.getApplicationSignature(Context) . For instructions, see Running Sample Apps .
Create a Release Key Hash
To authenticate the exchange of information between your app and the Facebook, you need to generate a release key hash and add this to the Android settings within your Facebook App ID. Without this, your Facebook integration may not work properly when you release your app to the store.
In a previous step, you should have updated your Facebook Developer Settings with the key hashes for your development environments.
When publishing your app, it is typically signed with a different signature to your development environment. Therefore, you want to make sure you create a Release Key Hash and add this to the Android settings for Facebook App ID.
To generate a hash of your release key, run the following command on Mac or Windows substituting your release key alias and the path to your keystore.
On Windows, you need the following:
Run the following command in a command prompt in the Java SDK folder:
Make sure to use the password that you set when you first created the release key.
This command should generate a 28 characher string. Copy and paste this Release Key Hash into your Facebook App ID’s Android settings.
You should also check that your Facebook App ID’s Android setting also contain the correct package name and main activity class for your Android package.
Use the Facebook SDK for Android with Maven
You can declare the Maven dependency with the latest available version of the Facebook SDK for Android.
Troubleshooting Sample Apps
If you have a problem running a sample app, it may be related to the key hash. You may see one of the following scenarios:
- A native Login Dialog appears but after accepting the permissions you are still in a logged out state. The logcat also contains an exception:
- A non-native Login Dialog appears with an error message: »..App is Misconfigured for facebook login. ».
Check your key hash and you can make sure you use the correct key hash. I
You can also manually modify the sample code to use the right key hash. For example in HelloFacebookSampleActivity class make a temporary change to the onCreate() :
Save your changes and re-run the sample. Check your logcat output for a message similar to this:
Save the key hash in your developer profile. Re-run the samples and verify that you can log in successfully.
Источник
Android Facebook Login Integration Step By Step With Logout
Today’s tutorial is about Android Facebook Integration with Login and Logout.
Social media integration is one of the most popular topics that we can see in applications programming. Like another social networks, Facebook provides powerful API for a lot of platform, including mobiles OS in general and Android in particular.
As we can see in many applications which needing user dentifiable information (account), they allow us to create a new account from Facebook ID (Login with Facebook). By this way, apps will use oAuth to get our public information through connecting and communicating with Facebook SDK.
Android facebook integration login example implement the social login in android app, so that user can login or sign in to the app without creating username and password.
We will fetch information like first name, last name, email, gender , facebook id and birth date from facebbok.
We will integrate it by using Facebook’s latest SDK. Checkout output of this example tutorial which is developed by demonuts.com
Download Source Code for android facebook integration login
Step 1: Create a new project
As always Prepare new project in the Android Studio. Select first activity as empty activity.
Step 2: Create an app in the Facebook developer console.
Watch following video, which describes the whole process to develop an app in facebook developer console.
Now here is a quick explanation of above video.
- Login to your facebook account and open Facebook developer console.
- Click on create app button which is at right upper corner and fill required information.
- After creating an app, click on setting from left menu side bar. Save your App ID, which is required in android studio project later.
- Click on Add platform and select Android. Fill package name and class in which you are going to add code for Facebook login integration Android. Now for getting Key Hash add following code in your project and run it, so you will get Key Hash in logcat as you shown in above video.
- Code for getting Key Hash:
- Put this Key Hash in Facebook developer console and click on save changes button.
- Now from the left side menu, click on App Review option. You will see that currently, your app is not public.
- To make your app public, just click on the upper switch as shown in the image. You will see that by default there three permissions are given email, public_profile, and user_image . To have more permissions click on start submission button and you have to give proper reasons to Facebook team why you need more permission. Right now default three permissions are enough for us.
Step 3: Updating build.gradle(Project: project_name)
add the following in gradle file
structure. So final code for build.gradle(Project: project_name) will look like this:
Step 4: Updating build.gradle(Module:app)
Go to android studio project and add following in the build.gradle(Module:app) file under android<> section.
Now in the dependency <> section, add
So final code for build.gradle(Module:app) will look like this:
check in your downloaded source code for exact results.
Step 5: Changing strings resource file
Now in the directory: res-> values -> strings.xml create one string resource and put your App ID(we saved it before, and if you have not, go to developer console get from there.)
Step 6: Adding activity
Go to AndroidManifest.xml file and add following inside application>….. tags.
Add internet permission between …. tag.
Note: If you are targeting SDK version above 22 (Above Lollipop) then you need to ask a user for granting runtime permissions. Check marshmallow runtime permission for more information.
Complete AndroidManifest.xml:
Step 7: Updating activity_main
Put following code in activity_main.xml file:
Step 8: Changing MainActivity class
Copy following code in MainActivity.java
Description of MainActivity.java
When a user clicks the login button , a dialog interface will open, and the user will enter his details. After Facebook successfully detects the user, the code flow will come to
from here as you can see, callback manager will come in to the picture and code flow will go to:
Finally, you will get Facebook details of the user in the string like fnm, lnm, mail, etc. and use it as per your requirement.
Logout from Facebook from custom button click
If you want to logout user by clicking on any custom button in any activity or fragment, put following in that button’s onclicklistener(),
You should also remove First name, last name, email-id etc. details from SharedPreferences if you stored it.
After Facebook you will need to integrate Google login in shortly.
You can learn Google Plus Login integration easily and smoothly : Google Plus Login sign in intagration
So that’s all for android facebook integration login tutorial, keep visiting for other examples.
Источник
Quick Tip: Add Facebook Login to Your Android App
Facebook Login provides a convenient and secure way for people to log in to an app without having to go through a sign-up process first. Using the latest version of Facebook’s SDK for Android, it takes only a few minutes to add this feature to your app.
In this quick tip, you will learn how to add a Facebook login button to an Android app and handle the events to log a user in using Facebook Login.
Premium Option
If you want a quick solution for setting up user authentication in your Android apps, try Android Authentication System on Envato Market. It’s a source code project that lets developers create signups and logins and authorize users for their app.
Android Authentication System on Envato Market You can also find Android developers on Envato Studio to help you with your projects.
Prerequisites
Before you begin, make sure you have access to the following:
1. Register Your App
All apps that use the Facebook SDK must be registered with Facebook. Log in to the Facebook Developers website and click Create a New App in the top right.
You are presented with a form that asks for the app’s Display Name, Namespace, and Category. Enter the required fields and click Create App ID.
In the next screen, you are able to see your Application ID. Make a note of it, because you will be needing it later in this tutorial.
Open Settings from the left and click the Add Platform button. From the pop-up, select Android.
In the next form, enter the package name of your app and the name of your Activity . If you haven’t created your app or Activity yet, make sure you remember the values you entered.
To fill in the Key Hashes field, open a terminal window and run the keytool command to generate a key hash using the debug keystore located at
/.android/debug.keystore. This is what the command should look like.
The default password for the debug keystore is android. Enter that password when prompted. The output of the command should be a string of 28 characters. Copy it, go back to your browser, and paste the string into the Key Hashes field as shown below.
Make sure Single Sign On is set to Yes and click the Save Changes button. Your app is now registered.
2. Add Facebook SDK to Your Project
The Facebook SDK is available on Maven Central. To use this repository, edit the build.gradle file in your project’s app directory and add the following code to it before the list of dependencies:
You can now add the Facebook SDK to your project as a compile dependency. Add the following code to the list of dependencies:
3. Create an Activity
Step 1: Define the Layout
Create a new layout named main_activity.xml in res/layout. This is going to be a very simple layout with only two widgets:
- a LoginButton to allow the user to log in to Facebook
- a TextView to display the result of the latest login attempt
You can place them inside a RelativeLayout . After including attributes for padding and positioning the widgets, the layout’s XML will look something like this:
Step 2: Create the Class
Create a new Java class that extends Activity and name it MainActivity.java. Remember that the name of this class and the package that it belongs to should match the values you entered while registering your app with Facebook.
Declare the widgets you defined in the activity’s layout as fields of this class.
Declare a CallbackManager as another field. The CallbackManager , as its name suggests, is used to manage the callbacks used in the app.
The SDK needs to be initialized before using any of its methods. You can do so by calling sdkInitialize and passing the application’s context to it. Add the following code to the onCreate method of your Activity :
Next, initialize your instance of CallbackManager using the CallbackManager.Factory.create method.
Call setContentView to set the layout defined in the previous step as the layout of this Activity and then use findViewById to initialize the widgets.
It’s time to create a callback to handle the results of the login attempts and register it with the CallbackManager . Custom callbacks should implement FacebookCallback . The interface has methods to handle each possible outcome of a login attempt:
- If the login attempt is successful, onSuccess is called.
- If the user cancels the login attempt, onCancel is called.
- If an error occurs, onError is called.
To register the custom callback, use the registerCallback method. The code to create and register the callback should look like this:
You can now add code to these methods to display appropriate messages using the setText method of the TextView .
When the onSuccess method is called, a LoginResult is passed as a parameter. Retrieve the access token it contains using getAccessToken and use its getUserId method to get the user’s ID. To get the token in the form of a String , use getToken . Display these values in the TextView by adding the following code to the onSuccess method:
If the user cancel’s the login attempt, we display a message saying «Login attempt canceled». Add the following code to the onCancel method:
Similarly, add the following code to the onError method:
Tapping the login button starts off a new Activity , which returns a result. To receive and handle the result, override the onActivityResult method of your Activity and pass its parameters to the onActivityResult method of CallbackManager .
4. Add the Facebook Application ID
The application ID you received when you registered your app should be added as a string in your project’s res/values/strings.xml. For this tutorial, call the string facebook_app_id.
5. Edit the Manifest
Define your Activity in the AndroidManifest.xml. If it is the first Activity of your app, you should also add an intent-filter that responds to the action android.intent.action.MAIN .
Add the application ID as meta-data .
Define FacebookActivity as another Activity that belongs to your app. It handles most of the configuration changes itself. You need to mention that using the configChanges attribute.
Finally, you have to request android.permission.INTERNET to be able to connect to Facebook’s servers.
6. Build and Run
Your app is now complete. When you build it and deploy it on your Android device, you will see the Facebook login button.
Tapping the login button takes you to a Facebook page that asks you to log in and authorize the app.
After successfully logging in, the TextView will display the user ID and auth token.
Conclusion
In this quick tip, you learned how to use the Facebook SDK to add Facebook Login to your Android app. You also learned how to handle the possible outcomes of a login attempt. To learn more about Facebook Login, you can go through the reference for the Facebook SDK for Android.
Источник
- Add the following to the dependencies <> section of your build.gradle (module: app) file to compile the latest version of the Facebook SDK for Android: