Facebook user email android

Phone number & E-mail verification in Android using Facebook Account Kit

Now-a-days most of the apps we develop are payment related. That’s why it becomes mandatory to verify user by their phone number or e-mail. Normally we do this with Firebase authentication. But recently, Facebook released a new product named Account Kit to make this verification easier. And in this article, we are going to implement this Facebook Account Kit.

Why Facebook Account Kit ?

Though there are some others methods to authenticate users, we should use Facebook Account Kit because of some special reasons. The reasons include:

  • The process is very simple and easy to use.
  • The login scope is fully handled by Facebook’s secure infrastructure.
  • The good thing is that it doesn’t require a Facebook Account for a user to log in.
  • It supports up to 10,000 verification messages per month(Relatively cheaper than others). After that, applications that exceed that will be charged at SMS standard rates.

Now lets start and follow the below steps:

Step 1: Create a new project with android studio

Step 2: Create an application on Facebook and Set up Facebook account kit

  1. Go to https://developers.facebook.com/apps/, sign in with your credentials and click on the ‘ Add a new App’ button.

2. Type the ‘ Display Name’ of your application and a ‘ Contact Email’ on the input fields and click on ‘ Create App Id’. then enter the captcha and click submit.

3. From the left navigation drawer, click on Add a Product then press on the Set Up button on Account Kit.

4. After clicking Set Up skip the Quickstart and go back to the app’s Basic Settings section

5. Head to the bottom of the Settings page and click on the + Add Platform button and then choose Android. This screen will open.

6. Insert your Google Play Package Name, Class Name and Key Hashes on the Android Platform. Save changes and tick: “Use this package name”.

7. Now go to Account Kit’s Settings section and enable “ Require server validation”. Then click “Get Started”.

9. Turn off the option “Require App Secret” and make sure the option “Enable Client Access Token Flow” has turned on, set to “YES”. Here you will obtain “Account Kit Client token” which will come in handy in the next steps.

Step 3: Set up Strings.xml

  1. Get your “ Facebook AppID” from the toolbar on Facebook developers website or you can navigate to: Settings → Basic and copy to App ID.
  2. In Android studio open strings.xmlfile and define below strings.

Step 4: Import Account kit SDK

  • Open the app level build.gradle (Module: app) file and add the implementation dependency of account kit with the latest version.
  • After adding this, click on sync now and let android studio to import the SDK into your project.

Step 5: Edit AndroidManifest.xml

  • Add Internet Permission to Android’s Manifest.
  • Add the following meta-data and activity to the application tag of AndroidManifest.xml.

Step 6: Edit interface (activity_main.xml)

  • In this tutorial we are going to use a very simple layout. It contains only two buttons, and you can find its code below:

Step 7: Edit Java files

Initialize the SDK

Previously we had the initialize the Account Kit SDK in application class but According to the latest Facebook we now don’t need to initialize it manually, it gets initialize by itself.

Initiate the Login Flow

Handle response for login flow in Activity’s Result

In onActivityResult method we can notify the user if login failed with Toast message and if login is successful then you can start a new activity.

Accessing account information on the device

  • After successful login session, we can access the Account Kit ID, phone number, and Email of the current account by the following code

account.getEmail() will return null if LoginType.PHONE was used in your configuration. Vice versa if you use LoginType.EMAIL in your configuration.

How to check the user is logged in or not?

  • By calling AccountKit.getCurrentAccessToken( ) we can know the user logged in or not.
Читайте также:  Андроид студио не запускается avd

How to remove access token on Logout ?

  • By calling AccountKit.logout( ) method we can remove the stored access token.

How to Customize the Account Kit User Interface?

It’s not possible to change the whole Account Kit UI. Modification is very limited made by Facebook. You can change Color of texts, buttons, Change background, title, Predefine phone-number etc. You can’t even change the text below the number input.

Customize color of EditText, Button, Text and Background

  • To change the color of text, editText, button set a custom theme to the AccountKitActivity in AndroidManifest.xml

And the AppLoginTheme looks like below in style.xml . You can modify this as per your requirements.

Specify target country code

  • If you target your app for some specific country you can use setSMSWhitelist(String[] smsWhitelist) method of AccountKitConfigurationBuilder , this will show the list of country codes which we want to target.

Specify default country code

  • You can use setDefaultCountryCode(String defaultCountryCode) in AccountKitConfigurationBuilder to give default value.

Set initial phone number in AccountKitActivity

  • You can set the initial phone number by doing:

How to Customize the color of Email?

Account Kit also offers the customization of the color of emails to match the color scheme with the color of your app. To do this-

  • Go to the Account kit section of your app dashboard and there you can select the Background color, Button color and Link color of the Email like below.

So we learned about the facebook account kit integration in android app to verify the user of our app via phone OTP and Email. Be sure to give claps if you find this article helpful.

Источник

Login With Facebook Android Studio using Facebook SDK 4

Hello friends, welcome to our new tutorial. In this tutorial we will create a login with facebook android app using android studio and facebook sdk 4.0. So lets begin.

Creating Android Project

  • Open Android Studio and Click on File-> New ->New Project
  • Give your application a name (in my case it is AndroidLogin) and a package (mine is net.simplifiedcoding)
  • Select Phone or Tablet
  • Select Blank Activity with Fragment.
  • Leave the rest things as it is and finish.
  • Now once our project is ready we need to configure a lot of things so lets begin.

Prerequisites for creating Login with Facebook Android App

  • Facebook app configured and linked to our android app
  • Facebook SDK
  • Facebook App ID
  • Android Key Hash
  • Facebook Activity

Lets start with creating Facebook App

Configuring Facebook SDK to Android Studio

  • Go to your gradle scripts -> build.gradle(Module:app)
  • Add the following code
  • Now add a new dependency for Facebook SDK
  • Inside dependencies add this line
  • It will download facebook sdk and will take some time. It can take long time if you have a slow internet connection. So mean while lets create our facebook app.

Creating Facebook App

  • Go to https://developers.facebook.com/. If you have not registered yourself as a developer yet then facebook will ask you to register as a developer. Simply register as a developer. (Congrats you are now a developer :P)
  • From the top navigation menu hover over my apps.
  • Then click add a new app.
  • You will asked to select a platform, click on android.
  • On the new page you can select quick start or you can select skip and create app id from the upper right corner.
  • Click on skip and create app id.
  • Give a display name and a namespace for your app as shown in the image below.

Creating Facebook App ID

  • Click create app id.
  • You will be redirected to your apps dashboard.

Apps Dashboard

  • Here you can get your app id. Copy the app id it will be used further. (I have made my app id hidden because i cant let you see it for security reasons)
  • Now from the left click on settings.
  • Click on add a platform and select android.
  • Enter your package name and class name of your main activity and click on save changes
  • The last thing you need is your App Key Hashes.

Adding a Platform for your App

Generating Key Hashes for your Login With Facebook Android App

  • Go to android studio.
  • On your project open your strings.xml file (res->values->strings.xml)
  • Add the below code
  • Replace your actual app id with your app id.
  • Now go to your AndroidManifest.xml
  • First add internet permission using the following code
  • Add a this meta-data code inside your application
  • This is containing our app id which we defined inside strings.xml.
  • Now create a new class in your package and name it MyApplication (name doesn’t matter you can name it anything)
  • Copy the following code
  • Make sure in the above code your provide your application’s package name.
  • The above class will print the KeyHash in the logcat. But we still need to define this class inside your AndroidManifest.
  • Go to AndroidManifest.xml again and Inside application opening tag define your class as shown below
  • Now execute your app and you will get your keyhash in your logcat.
  • Copy the keyhash and enter that to your app settings in facebook and click save.
Читайте также:  Send text to android

Creating your Login With Facebook Android App

  • For facebook login Facebook SDK provides an activity that will open while login. You only need to add that activity to your manifest file.
  • Add the facebook activity using the following code.
  • So your final AndroidManifest.xml would be like
  • Now come to your fragment layout and create a TextView and a Facebook Login Button. The button is provided by facebook sdk. You can use the below code.
  • We will show the user name in the textview after successful login.
  • Go to your MainFragment.java and copy the following code.
  • Run your app now and Bravo it works 😉
  • If you are having troubles then you can download the source code of this project from here

Some more android tutorials you should check

So thats all for this login with facebook android app tutorial friends. Feel free to ask if you have any queries or troubles following the steps. Thank you 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Checkout these tutorials as well:

111 thoughts on “Login With Facebook Android Studio using Facebook SDK 4”

When i create a Blank Activity with Fragment it creates me an activity that extends from ActionBarActivity and then creates a class that extends from Fragment, how can i execute my MainFragment from an class that extends form ActionBarActivity.

I am not getting what you want to ask? Tell me your actual requirement.. or you can check details about fragments from here – > http://developer.android.com/training/basics/fragments/creating.html

hey bilal khan …can you make an tutorial on sharing photos videos or status from android app to facebook automatically…

from your activity you can go to mainfragment, after setcontentview paste this

if (savedInstanceState == null) <
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new MainFragment ()).commit();>

Your tutorial is great and helped me a lot but I want to create the login button on a popup window, how should I do that? and how can I hide the login button when the user has already loged in? and will you do some tutorials on facebook sharing and like button too?

Ok stay tuned I will try to help you with some more tutorials ASAP 🙂

Is there a way to use the FB SDK log in button without using a fragment? Can I just put it straight on the MainActivity? If so, how do I do this?

Yeah you can obviously do that

How can i integrate this code in my main activity without creating a fragment class??

Please tel me the way to do this without creating main fragment…..

I downloaded the code, but it is a game. Please, could you check the link?

Sorry for the inconvenience I corrected the link you can check it now… 🙂

Hai, i have a problem, when i connect in my account its work, but in the second part it say the my hash key is not stored, what does it mean, i use your applciation. Thanks for your help

Thanks man! I got your site after one week and you wont believe even after trying millions of options it dint work. Amazing work 😀

You welcome bro 🙂

I try this code butt my application not open just an error message shown “Your application is forcefully stoped”

In my case it is working absolutely fine as you can see on the video. Send me your code.. I will check it

mine also same problem 🙁

Check what exception your app is throwing from your logcat

hi bro, i have the same problem, can you help me.
can i send you my code to you see it?

if you say yes, where and how i can send you this??

I need to get the email id, phone number , password of the after login. please suggest how to get that. Thanks i advance.

List permissions = new ArrayList ();
permissions.add(“email”);

Add all the permissions you need and then
loginButton.setReadPermissions(permissions);

Thanks a lot! work 100%.. How I can start another activity after login ? And how can I pass the parameters of facebook to the new activity?

how can I start new activity after successful login?

You can use Intent to start another activity..

Hi, great tutorial but i seem to have an error through your code. when i run inspect code in android studio it comes up with an error such as…

Читайте также:  Как удалить гугл аккаунт с андроида после сброса настроек хонор

Location
class anonymous in FacebookCallback callback (mobliedevelopment.terrelsimeongordon.hwl_fb.MainFragment)

Problem synopsis
Variable ‘accessToken’ at line 38 is never used

i downloaded the source code yet the same error comes up

this is the code that android is complaining about?

private FacebookCallback callback = new FacebookCallback() <
@Override
public void onSuccess(LoginResult loginResult) <
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
displayMessage(profile);
>

do you have any clue why it is not working and why it can not find accessToken? grateful if you could help answer this 1 million dollar question 🙂

Hi sorry about that i just found the issue 🙂
just wondering if its possible to extend just an activity instead of an ActionBarActivity for the MainActivity? if so how would you i go about altering this code to that requirement?

How do i change the height and width of the Login button ( com.facebook.login.widget.LoginButton ) i m using the 4.1 sdk for facebook

you can also create your own button 🙂

thanks bro.
its really useful article.

I need to get email Id of the user, added the permission of it too but How do i print that in the TextView. Profile.getEmailID(); doesn’t work. Please help

did you find any solution ?

i m not getting the friend list

i need the friend list of my friends but i m not.
plz tell me the permissions to write for it how can we aceess all that ..
i am new in android

The problem is how to call another activity right after authorization succeed and remove logout button.

thanks in advance……

help me to print complete profile information of the user.
Thank you

Session and graph is not working in this sdk version. so please give the code to get the complete profile information.

your tutorial heiped me to get the username from profile using profile.getName();

similarly i need to get the all profile information. please give the code.

Thanks in advance

ok stay tuned and I will try to post a new tutorial for this

login page of facebook appeared only once during the first run. Subsequent running doesnt show it.Please help me.
also during debugging, variable profile gets null value. iam a beginner to android. please help

getting error in logcat please sir help me

Error:A problem occurred configuring project ‘:app’.
> Could not download facebook-android-sdk.aar (com.facebook.android:facebook-android-sdk:4.0.0)
> Could not get resource ‘https://jcenter.bintray.com/com/facebook/android/facebook-android-sdk/4.0.0/facebook-android-sdk-4.0.0.aar’.
> Could not GET ‘https://jcenter.bintray.com/com/facebook/android/facebook-android-sdk/4.0.0/facebook-android-sdk-4.0.0.aar’.
> d29vzk4ow07wi7.cloudfront.net

I am able to integrate facebook login into my app successfully by follwing your post. The actual req of my app is to trigger facebook status at the end of every month. When i try to login after two months login dialog does not open up. It throws a FacebookAuthorizationException. Can you please help me in figuring it out.

Note: I believe due to access token expiry facebook login dialog does not open. You can check it

After clicking the Facebook login button, I am getting an error saying you are not logged in. please log in and try again…how to login?

10-16 15:59:05.620 1304-1335/? E/InputDispatcher﹕ channel ‘c10b4fa com.example.dipali.androidlogin/com.example.dipali.androidlogin.MainActivity (server)’

Channel is unrecoverably broken and will be disposed!

I have this error.How can i solve this error?

Try downloading my source code and check you are still getting the error or not?

I referred your source code Still i am having this error.Process

Couldn’t load memtrack module (No such file or directory)
10-17 11:11:42.089 2347-2347/? E/android.os.Debug﹕ failed to load memtrack module: -2
10-17 11:11:49.677 1837-1837/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
10-17 11:11:49.677 1837-1837/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn’t include a PendingIntent in the extras?
10-17 11:11:50.245 2384-2384/? E/memtrack﹕ Couldn’t load memtrack module (No such file or directory)
10-17 11:11:50.246 2384-2384/? E/android.os.Debug﹕ failed to load memtrack module: -2
10-17 11:11:50.345 2384-2392/? E/art﹕ Thread attaching while runtime is shutting down: Binder_1
10-17 11:11:53.054 1837-1837/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
10-17 11:11:53.055 1837-1837/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn’t include a PendingIntent in the extras?
10-17 11:11:59.472 2393-2393/? E/SysUtils﹕ ApplicationContext is null in ApplicationStatus
10-17 11:11:59.956 2393-2393/? E/libEGL﹕ validate_display:255 error 3008 (EGL_BAD_DISPLAY)
10-17 11:11:59.956 2393-2393/? E/libEGL﹕ validate_display:255 error 3008 (EGL_BAD_DISPLAY)
10-17 11:11:59.956 2393-2393/? E/chromium﹕ [ERROR:gl_surface_egl.cc(327)] No suitable EGL configs found.
10-17 11:11:59.956 2393-2393/? E/chromium﹕ [ERROR:gl_surface_android.cc(23)] GLSurfaceEGL::InitializeOneOff failed.
10-17 11:11:59.956 2393-2393/? E/chromium﹕ [ERROR:browser_main_loop.cc(698)] GLSurface::InitializeOneOff failed
10-17 11:12:00.266 2393-2393/? E/DataReductionProxySettingListener﹕ No DRP key due to exception:java.lang.ClassNotFoundException: com.android.webview.chromium.Drp
10-17 11:12:00.913 953-953/? E/SELinux﹕ avc: denied < find >for service=batteryproperties scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:healthd_service:s0 tclass=service_manager
10-17 11:12:04.515 2393-2474/? A/chromium﹕ [FATAL:gl_surface_android.cc(58)] Check failed: kGLImplementationNone != GetGLImplementation() (0 vs. 0)
——— beginning of crash
10-17 11:12:04.515 2393-2474/? A/libc﹕ Fatal signal 6 (SIGABRT), code -6 in tid 2474 (GpuThread)
10-17 11:12:04.634 962-962/? A/DEBUG﹕ *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
10-17 11:12:04.634 962-962/? A/DEBUG﹕ Build fingerprint: ‘generic_x86/sdk_google_phone_x86/generic_x86:6.0/MRA44C/2166767:eng/test-keys’
10-17 11:12:04.634 962-962/? A/DEBUG﹕ Revision: ‘0’
10-17 11:12:04.634 962-962/? A/DEBUG﹕ ABI: ‘x86’
10-17 11:12:04.634 962-962/? A/DEBUG﹕ pid: 2393, tid: 2474, name: GpuThread >>> com.example.dipali.androidlogin > com.example.dipali.androidlogin Reply

Hi, that was an really awesome tutorial, I run the app, go the output. But I tried to reopen means its shows the HashKey error. Please guide me

Источник

Оцените статью