Facebook integration for Android

I am developing an Android application where I want to integrate facebook. I went through the link http://developers.facebook.com/docs/guides/mobile/#android

I have little doubt:

  • I generated the key using keytool, but could not figure out how to register it in the Mobile and Devices section. Where is this Mobile Devices section located?

  • How can I implement facebook functionality as buttons in an application? Any sample code will be really helpful.

Rgds, sapan

www.darooadda.com

+6
source share
4 answers

1) Follow the following path:

  • Go to http://www.facebook.com/developers/
  • Click on the application link on the right.
  • click change settings (in the menu on the right)
  • Click "Mobile" and devices in the menu on the left.

2) Do you want to create your own application or access it through web browsing? If this is a web view, then create a web page, as in any other html code. To get started with your native application, have you looked at an example: https://github.com/facebook/facebook-android-sdk/tree/master/examples/simple ?

+2
source

I really do not answer the question, but I recommend using general intentions in Android instead of the popup API, which requires authentication. If an Android user has a Facebook account, he will likely have a facebook application.

UPDATED: Take a look at the documentation: http://developer.android.com/reference/android/content/Intent.html

It should be something like this:

Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT, "Extra message"); startActivity(Intent.createChooser(intent, "message")); 

Perhaps you can directly target only the facebook app.

+1
source
 package com.notifaction.fb_intgration; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.widget.Toast; import com.facebook.CallbackManager; import com.facebook.FacebookCallback; import com.facebook.FacebookException; import com.facebook.FacebookSdk; import com.facebook.GraphRequest; import com.facebook.GraphResponse; import com.facebook.Profile; import com.facebook.login.LoginManager; import com.facebook.login.LoginResult; import com.facebook.login.widget.LoginButton; import org.json.JSONException; import org.json.JSONObject; import java.util.Arrays; public class MainActivity extends AppCompatActivity { private CallbackManager callbackManager; TextView username,facebook; private String facebook_id,f_name, m_name, l_name, full_name, email_id; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(getApplicationContext()); callbackManager = CallbackManager.Factory.create(); setContentView(R.layout.activity_main); facebook = (TextView)findViewById(R.id.login_facebook); username=(TextView)findViewById(R.id.username); callbackManager = CallbackManager.Factory.create(); LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { Log.d("Success", "Login"); GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject object, GraphResponse response) { Log.v("LoginActivity", response.toString()); // Application code try { String email = object.getString("email"); String name = object.getString("name"); Toast.makeText(MainActivity.this, ""+email+" "+name, Toast.LENGTH_LONG).show(); } catch (JSONException e) { e.printStackTrace(); } } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id,name,email,gender,birthday"); request.setParameters(parameters); request.executeAsync(); } @Override public void onCancel() { Toast.makeText(MainActivity.this, "Login Cancel", Toast.LENGTH_LONG).show(); } @Override public void onError(FacebookException exception) { Toast.makeText(MainActivity.this, "Login unsuccessful!", Toast.LENGTH_LONG).show(); } }); facebook.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "email", "user_birthday", "user_friends")); } }); username.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LoginManager.getInstance().logOut(); // logout in facebook account } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); } } ////////////// xml /////// <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:facebook="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="logout" android:layout_margin="10dp" android:textSize="20sp" /> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginRight="40dp" android:layout_marginLeft="40dp" android:layout_marginTop="10dp"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="LOGIN WITH FACEBOOK" android:gravity="center" android:background="@drawable/facebook_buttion_background" android:textSize="14dp" android:paddingLeft="20dp" android:drawableLeft="@drawable/fb" android:textColor="@color/white" android:textStyle="bold" android:id="@+id/login_facebook" /> </LinearLayout> </LinearLayout> /////////////// android manifest///////////////////////////// <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.notifaction.fb_intgration"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id" /> <activity android:name="com.facebook.FacebookActivity" android:configChanges= "keyboard|keyboardHidden|screenLayout|screenSize|orientation" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:label="@string/app_name" /> <provider android:authorities="com.facebook.app.FacebookContentProvider1234" android:name="com.facebook.FacebookContentProvider" android:exported="true" /> </application> </manifest> ////////////////// gradel /////////////////// apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "23.0.1" defaultConfig { applicationId "com.notifaction.fb_intgration" minSdkVersion 16 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:22.2.0' compile 'com.android.support:design:22.2.0' compile 'com.facebook.android:facebook-android-sdk:4.6.0' } 
0
source

I used facebook sdk 4.10.0 to integrate login into my android app. Next tutorial:

Facebook integration of Android studio.

You can get first name, last name, email address, gender, facebook id and date of birth from facebbok.

The above guide also explains how to create an application in the facebook developer console via video.

Gradle.build

apply plugin: 'com.android.application'

android {compileSdkVersion 23 buildToolsVersion "23.0.3"

 defaultConfig { applicationId "com.demonuts.fblogin" minSdkVersion 16 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } repositories { mavenCentral() } 

}

dependencies {compile fileTree (dir: 'libs', include: [' * .jar ']) testCompile' junit: junit: 4.12 'compile' com.android.support:appcompat-v7:23.4.0 'compile' com.facebook .android: facebook-android-sdk: 4.10.0 'compile' com.github.androidquery: androidquery: 0.26.9 '}

Source code for activity_main.xml

 <?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" tools:context="com.demonuts.fblogin.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000" android:layout_marginLeft="10dp" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/text"/> <ImageView android:layout_width="300dp" android:layout_height="300dp" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:id="@+id/ivpic" android:src="@mipmap/ic_launcher"/> <com.facebook.login.widget.LoginButton android:id="@+id/btnfb" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 

Code for MainActivity.java

 import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import android.widget.TextView; import com.androidquery.AQuery; import com.facebook.AccessToken; import com.facebook.AccessTokenTracker; import com.facebook.CallbackManager; import com.facebook.FacebookCallback; import com.facebook.FacebookException; import com.facebook.FacebookSdk; import com.facebook.GraphRequest; import com.facebook.GraphResponse; import com.facebook.Profile; import com.facebook.ProfileTracker; import com.facebook.login.LoginResult; import com.facebook.login.widget.LoginButton; import org.json.JSONException; import org.json.JSONObject; import java.util.Arrays; public class MainActivity extends AppCompatActivity { private AQuery aQuery; private ImageView ivpic; private TextView tvdetails; private CallbackManager callbackManager; private AccessTokenTracker accessTokenTracker; private ProfileTracker profileTracker; private LoginButton loginButton; private FacebookCallback&lt;LoginResult&gt; callback = new FacebookCallback&lt;LoginResult&gt;() { @Override public void onSuccess(LoginResult loginResult) { GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject object, GraphResponse response) { Log.v("LoginActivity", response.toString()); // Application code try { Log.d("tttttt",object.getString("id")); String birthday=""; if(object.has("birthday")){ birthday = object.getString("birthday"); // 01/31/1980 format } String fnm = object.getString("first_name"); String lnm = object.getString("last_name"); String mail = object.getString("email"); String gender = object.getString("gender"); String fid = object.getString("id"); tvdetails.setText("Name: "+fnm+" "+lnm+" \n"+"Email: "+mail+" \n"+"Gender: "+gender+" \n"+"ID: "+fid+" \n"+"Birth Date: "+birthday); aQuery.id(ivpic).image("https://graph.facebook.com/" + fid + "/picture?type=large"); //https://graph.facebook.com/143990709444026/picture?type=large Log.d("aswwww","https://graph.facebook.com/"+fid+"/picture?type=large"); } catch (JSONException e) { e.printStackTrace(); } } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id, first_name, last_name, email, gender, birthday, location"); request.setParameters(parameters); request.executeAsync(); } @Override public void onCancel() { } @Override public void onError(FacebookException error) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(this); setContentView(R.layout.activity_main); tvdetails = (TextView) findViewById(R.id.text); ivpic = (ImageView) findViewById(R.id.ivpic); loginButton = (LoginButton) findViewById(R.id.btnfb); aQuery = new AQuery(this); callbackManager = CallbackManager.Factory.create(); accessTokenTracker= new AccessTokenTracker() { @Override protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) { } }; profileTracker = new ProfileTracker() { @Override protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) { } }; accessTokenTracker.startTracking(); profileTracker.startTracking(); loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_friends")); loginButton.registerCallback(callbackManager, callback); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); } @Override public void onStop() { super.onStop(); accessTokenTracker.stopTracking(); profileTracker.stopTracking(); } @Override public void onResume() { super.onResume(); Profile profile = Profile.getCurrentProfile(); } } 
0
source

Source: https://habr.com/ru/post/886543/


All Articles