Android integrates Facebook for third-party landing

Source: Internet
Author: User
Tags openssl sha1

  One, the necessary preparation:

    

  Here is the official English document, which explains in detail below:

Facebook app: You need to create your own app on the Facebook developer platform,

Facebook SDK: Add the Facebook SDK to your project,

Facebook App ID: Need to get the ID of the app created on the Facebook developer platform.

Android Key hash: Get Android hash key.

Facebook activity: Include Facebook activity in your project's Androidmanifest.xml file.

Integrated Facebook third-party landing, requires the above five prerequisites, detailed below, how to get five conditions.

One: Facebook App

Go to Facebook developer Official website: https://developers.facebook.com/(scientific Internet required)

    

In the top right corner, click My Apps--add a new App,

Select Android,

  

Enter the name of the Create app

  

Categories at random, click on the lower right corner, "Create app number"

After the successful creation, will jump to a page, the top of the page, there are six steps to show, according to the page prompts, slowly down, step by step follow the steps to do it.

In the "Key hashes" step, in the command line of your computer, input the response command, you will get a string of characters, this is the key Hashe.

For example, on the Linux system command line, enter: keytool -exportcert - aliasandroiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64,接着输入密码:android。

This is where the Facebook app is created!

Two: Facebook SDK (using Android Studio)

1. Add the following code to Build.grale (Module:app)

    

repositories {    mavencentral ()}

2. Add the Facebook SDK to your project:

Still a Build.grale (module:app) file, in dependencies, add the following line of code:

" compile ' com.facebook.android:facebook-android-sdk:4.+ '

  

Three: Facebook App ID

ID on the Facebook Developer website, click on "My apps, then select the Responsive app and you'll see the app ID."

Add the ID to the project String.xml file as follows:

  

<string name="facebook_app_id">1142428895392515</  String>

Then configure it in the Androidmainifest.xml file.

<meta-data android:name="com.facebook.sdk.ApplicationId " android:value= " @string/facebook_app_id "/>

Four: Android Key Hash

When you create an app above, you've already got it.

V: Facebook Activity

In the project configuration file, include the Facebook activity. (I forgot to add, results: I can't find the Facebook activity error)

 

Six: Sample Code

Mainactivity.java

Package Mera.login;import Android.app.activity;import android.content.intent;import android.os.bundle;import Android.util.log;import Android.view.view;import Com.facebook.accesstoken;import Com.facebook.CallbackManager; Import Com.facebook.facebookcallback;import Com.facebook.facebookexception;import Com.facebook.facebooksdk;import Com.facebook.appevents.appeventslogger;import Com.facebook.login.loginresult;import Com.facebook.login.widget.LoginButton; Public classMainactivity extends Activity {//@ViewInject (id = r.id.facebook)    PrivateLoginbutton Bt_facebook; PrivateCallbackmanager Mcallbackmanager; PrivateAccesstoken Maccesstoken; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//first line, initialize the FACEBOOKSDK,facebooksdk.sdkinitialize (Getapplicationcontext ());        Super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mcallbackmanager=CallbackManager.Factory.create (); //Locate Login, buttonBt_facebook =(Loginbutton) Findviewbyid (R.id.facebook); Bt_facebook.registercallback (Mcallbackmanager,NewFacebookcallback<loginresult>() {@Override Public voidonsuccess (Loginresult loginresult) {LOG.E ("ABC","onsuccess"); } @Override Public voidOnCancel () {LOG.E ("ABC","OnCancel"); } @Override Public voidonError (facebookexception error) {LOG.E ("ABC","OnError");    }        }); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data)        {Super.onactivityresult (Requestcode, ResultCode, data);    Mcallbackmanager.onactivityresult (Requestcode, ResultCode, data); }     Public voidGetfacebooktoken (view view) {Accesstoken Maccesstoken=Accesstoken.getcurrentaccesstoken (); LOG.E ("token","token:"+ Maccesstoken.gettoken () +","+"user_id"+Maccesstoken.getuserid ()); } @Overrideprotected voidOnresume () {super.onresume (); Appeventslogger.activateapp ( This); } @Overrideprotected voidOnPause () {super.onpause (); Appeventslogger.deactivateapp ( This); }}

Activity_main.layout

<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"android:padding="10DP"Android:background="#c99e9e"Tools:context="mera.login.MainActivity"> <com.facebook.login.widget.loginbutton android:id="@+id/facebook"Android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>
</LinearLayout>

Please leave a comment message to me below.

  

Android integrates Facebook for third-party landing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.