Integrate the Facebook SDK
The Facebook Login SDK for Android is a component of the Facebook SDK for Android. To use the Facebook Login SDK in your project, make it a dependency in Maven, or download it. Choose the method you prefer with the following button.
- Go to Android Studio | New Project | Minimum SDK
- Select "API 15: Android 4.0.3" or higher and create your new project.
- In your project, open your_app | Gradle Scripts | build.gradle
- Add the Maven Central Repository to build.gradle before dependencies:repositories { mavenCentral() }
- Add compile 'com.facebook.android:facebook-android-sdk:[4,5)' to your build.gradle dependencies.
Using Maven
- In your project, open your_app > Gradle Scripts > build.gradle (Project) make sure the following repository is listed in the
buildscript { repositories {}}
: - In your project, open your_app > Gradle Scripts > build.gradle (Module: app) and add the following implementation statement to the
dependencies{}
section to depend on the latest version of the Facebook Login SDK: - Build your project.
string.xml
<string name="facebook_app_id">XXXXXXXXXXXXX</string>
<string name="fb_login_protocol_scheme">fb22XXXXXX60580447282</string>
menifest.xml
//permission
<uses-permission android:name="android.permission.INTERNET"/>
//applicaltion tag
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
Initialization of CallBackManager And Button.
CallbackManager callbackManager = CallbackManager.Factory.create();
Button loginButton = (Button) findViewById(R.id.facebook);
loginButton.setOnClickListener(this);
And use this facebookIntegration() method in your onClick method.
private void facebookIntegration() {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email"));
// LoginButton.setLoginBehavior(LoginBehavior.WEB_ONLY);
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
Bundle facebookBundle = getFacebookData(loginResult.getAccessToken());
}
@Override
public void onCancel() {
// Log.d("sam", "cancel");
// App code
}
@Override
public void onError(FacebookException exception) {
// Log.d("sam", new Gson().toJson(exception));
// App code
}
});
}
private Bundle getFacebookData(final AccessToken token) {
final Bundle facebookBundle = new Bundle();
facebookBundle.putString("fields", "id,name,email");
GraphRequest request = GraphRequest.newMeRequest(token,
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
String name = "", email = "", id = "";
try {
name = object.getString("name");
} catch (JSONException e) {
name = "";
e.printStackTrace();
}
try {
email = object.getString("email");
} catch (JSONException e) {
email = "";
e.printStackTrace();
}
try {
id = object.getString("id");
} catch (JSONException e) {
id = "";
e.printStackTrace();
}
Log.d("sam", name + " " + email + " "+ id);
Log.d("sam", "Object = " + object.toString());
}
});
request.setParameters(facebookBundle);
request.executeAsync();
return facebookBundle;
}
And use your callbackManager.onActivityResult() method of your activity or fragment onActivityResult method.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
Note: Please Config your manifest and Gradle before using this code.
That is all. If any help related to this post please comment.
Thank you, guys.
Enjoy coding.
No comments:
Post a Comment