First Download Facebook Android SDK from github then Create New Project and Write below Code into OnClicklistener of Button.
MainActivity.java
Facebook facebook = new Facebook(APP_ID);
restoreCredentials(facebook);
messageToPost = "Hello Everyone.";
if (!facebook.isSessionValid()) {
loginAndPostToWall();
} else {
postToWall(messageToPost);
}
Declare below variables into MainActivity.java
private Facebook facebook;
private String messageToPost;
private static final String APP_ID = "152424051507008";
private static final String[] PERMISSIONS = new String[] { "publish_stream" };
private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";
Add below methods into your MainActivity.java
public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
public void loginAndPostToWall() {
facebook.authorize(this, PERMISSIONS,Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}
public void postToWall(String message) {
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") || response.equals("false")) {
showToast("Blank response.");
} else {
showToast("Message posted to your facebook wall!");
}
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
}
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null) {
postToWall(messageToPost);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
}
}
private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
.show();
}
Now Run Your Project
Enjoy :-)
You can download complete source code from this link.
Android - Facebook Integration
Don’t forget to provide feedback or follow this blog, if you find this blog is useful.
MainActivity.java
Facebook facebook = new Facebook(APP_ID);
restoreCredentials(facebook);
messageToPost = "Hello Everyone.";
if (!facebook.isSessionValid()) {
loginAndPostToWall();
} else {
postToWall(messageToPost);
}
Declare below variables into MainActivity.java
private Facebook facebook;
private String messageToPost;
private static final String APP_ID = "152424051507008";
private static final String[] PERMISSIONS = new String[] { "publish_stream" };
private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";
Add below methods into your MainActivity.java
public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
public void loginAndPostToWall() {
facebook.authorize(this, PERMISSIONS,Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}
public void postToWall(String message) {
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") || response.equals("false")) {
showToast("Blank response.");
} else {
showToast("Message posted to your facebook wall!");
}
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
}
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null) {
postToWall(messageToPost);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
}
}
private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
.show();
}
Now Run Your Project
Enjoy :-)
You can download complete source code from this link.
Android - Facebook Integration
Don’t forget to provide feedback or follow this blog, if you find this blog is useful.
i'm getting this error:
ReplyDeleteFailed to find provider info for com.facebook.katana.provider.AttributionIdProvider
Please see my update and download complete source code.
DeleteHi....
ReplyDeletethis is my first attempt on android app with facebook integration .
i m also getting the same error , please tell me what should i do to solve this.
i am eagerly to solve this issue.please help me to get out of this.
Thanks in advance
my error as:-
"Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider"
Hello Yogesh,
DeletePlease Download code from above link and put your APP_ID in Main_Activity file and after that if you have any issue then give me your email address i will send you demo application.
darjidhruvit@gmail.com can you please send me a demo application i urgently require that cause i have to include this thing into project and i am having error in other code like ,,,solution for Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider
ReplyDeleteHello Dhruvit,
DeleteYou can download source code of this post from below link.
http://www.mediafire.com/?3n34v50c6p64r4a
Hello! How can I Extend the expiration of existing short-lived user access_tokens?
ReplyDeleteHello Luz,
DeleteSorry but i don't know about this.
Hello! Now I'm having this Error: "message": "An active access token must be used to query information about the current user". "type": "OAuthException"..
ReplyDeleteHello Luz,
DeleteThere is some problems in your Access_Token or APP_ID so please check it first.
Thanks it is very helpful for me.grate work.
ReplyDeleteHello Krishna,
DeleteWelcome........
error on LoginDialogListener
ReplyDeletenew LoginDialogListener());
say can't resolved
Hello,
DeleteYou can download source code of this post from below link.
http://www.mediafire.com/?3n34v50c6p64r4a
Thanks to Dipak Keshariya . It is good tutorial . I got a some doubt . how to integrate the code to share with text(message) and image .
ReplyDeletehello when i click on log in facebook...it start loading...and then finish application. no option for post
ReplyDeletesuccess,but This application in not possible log in of another user.
ReplyDeleteplease give me idea..........
Hi
ReplyDeleteiam not finding any source code on this link:
http://www.mediafire.com/?3n34v50c6p64r4a
Please provide me source code link.
Thanks & Regards
Pavan.