How to access news feed section of facebook in Android? - android

I can connect to my application in Facebook. I am trying to fetch news feed of application in Facebook.
After login it prompts me if it can 'access posts in my news feed', allow or don't allow. If I click on allow then nothing happens. It just goes back to my activity screen. I am new totally.
Why can't I access news feed section of that application in facebook?
The code is given below:
public class MyGreatActivity extends Activity
{
Facebook facebook = new Facebook("115793565149113");
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
facebook.authorize(this, new String[] {"read_stream" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}

What you've done so far is just setting up the connection.
Now you need to make a request to actually get the data you want.
Add something like this to your code.
AsyncFacebookRunner fbData = new AsyncFacebookRunner(facebook);
Bundle params = new Bundle();
// what data should be retrieved
params.putString("fields", "type, link, from, message, picture, name, description, created_time");
// from which feed would you like data
fbData.request("yourappname/feed", params, new FBRequestListener(this));
// class that will handle the data from facebook
public class FBRequestListener implements RequestListener {
.... // check the documentation on which methods you should include
}

Related

Can't get CallbackManager to be fired

I am working on FB Login integration on my app. I have done all the set-up neccessary and generated my hash-key. I use a custom UI view for the login so I implement the LoginManager in my activty as below
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this);
setContentView(R.layout.activity_account_front);
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// not called
Log.e("fb_login_sdk", "callback success");
}
#Override
public void onCancel() {
// not called
Log.e("fb_login_sdk", "callback cancel");
}
#Override
public void onError(FacebookException e) {
// not called
Log.e("fb_login_sdk", "callback onError");
}
});
final Activity activity = this;
face = (ImageView) findViewById(R.id.face);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("fb_login_sdk", "click");
List<String> perm = new ArrayList<String>();
perm.add("email");
LoginManager.getInstance().logInWithReadPermissions(activity, perm);
}
});
With this, the highest I have gotten is the onclick on the imageview log. I have implemented this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("Results", String.valueOf(resultCode));
callbackManager.onActivityResult(requestCode, resultCode, data);
}
but none of the logs are printed apart from the one at the beginning of the click event. When I press the button, a spinner shows briefly and then the app just end (not crash). How do I get this to work, its driving me crazy as I have spent about a day tring to get this to work. Thanks
Try to use application context instead of activity context when you initialize facebook sdk.
Replace this
FacebookSdk.sdkInitialize(this);
with
FacebookSdk.sdkInitialize(getApplicationContext());
Hope it will be useful for you.
For me, it was a simple and perhaps an oversight. As #Arkar pointed out, I forgot to remove this from my AndroidManifest file for that particular Activity and hence there was not callback triggered.
android:noHistory

Android Facebook SDK 4.0.0 Sharing callback doesn't work properly

I am using following code to share link on facebook. when user click on cancel on Share dialog interface,onSuccess() callback method is called sometimes instead of onCancel(). And getting post id null.Please help me what's going wrong?
ShareButton btn;
CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this);
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_share);
btn = (ShareButton) findViewById(R.id.btn_share);
btn.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.e("Tag","Successfully posted");
Log.e("Post id",result.getPostId());
}
#Override
public void onCancel() {
Log.e("Tag","Canceled by user");
}
#Override
public void onError(FacebookException error) {
Log.e("Tag",error.getLocalizedMessage());
}
});
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("My Custom URL"))
.setContentTitle("Test")
.build();
btn.setShareContent(content);
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
Well, maybe I'm late but I faced the same issue weeks ago.
What I've noticed is if you press Cancel in the web-base Share dialog, the onSuccess() method is called but the Share.Result object contains a null postId, so you can control whenever the user pressed cancel or share by checking the Share.Result response.
Another thing I've noticed is that if you share content with native app installed, postId field is always null... so you will have to check if the user has the app installed to check or not the postId field.

facebook login takes long time during logging in

I have used facebook sdk 3.5 in android to create custom login button after clicking will get facebook details then send to server and then go to next intent(screen).I have created the code below and using it but the progress dialog takes long time to get to onCompletemethod() in facebook also sometime its timedOut.I have posted the code below please let me know if there is a better way to login than the one below and if I can reduce the time it takes to login facebook .I really appreciate ay help .Thanks in advance.
public class MainActivity extends Activity {
private ProgressDialog progressDialog;
static String email;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Wait...");
Button bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog.show();
}
});
Session.openActiveSession(MainActivity.this, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
progressDialog.dismiss();
email= (String) response.getGraphObject().getProperty("email");
Log.d("email", email);
new FetchTask().execute();
}
}
}).executeAsync();
}
}
});
}});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(Session.getActiveSession() != null)
{
Session.getActiveSession().onActivityResult(MainActivity.this, requestCode, resultCode, data);
}
}
I have the same issue. Maybe you found a solution.
I have the problem on Nexus 4 , but not get it on Samsung S3
Sorry I have no as much reputation to leave a comment.

Android Facebook authorization - can not log in when official Facebook app is installed

I need to log in to Facebook and get same fields like email, etc. I use the Facebook SDK, and I set my Android key Hash in developers.facebook and set "Configured for Android SSO". In the simulator and some devices the application works fine.
But if the official Facebook application is installed on the device, my application does not work: I push the login button, but I not see a dialog with a web-view were my password and login are asked for. It looks like the problem in Stack Overflow question Using facebook.authorize with the Android SDK does not call onActivityResult or Stack Overflow question Android Facebook API single sign-on?, but I can not understand how to resolve it.
My code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
this.facebookConnector.getFacebook().authorizeCallback(requestCode, resultCode, data);
}
public void getAccessToken() {
SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {
#Override
public void onAuthSucceed() {
setupAccessToken(facebookConnector.getFacebook().getAccessToken());
}
#Override
public void onAuthFail(String error) {
Toast.makeText(getApplicationContext(), getString(R.string.error_login), Toast.LENGTH_SHORT).show();
}
};
SessionEvents.addAuthListener(listener);
facebookConnector.login();
}
facebookConnector code
public class FacebookConnector {
public void login() {
if (!facebook.isSessionValid()) {
facebook.authorize(this.activity, this.permissions, new LoginDialogListener());
}
}
private final class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
SessionEvents.onLoginSuccess();
}
public void onFacebookError(FacebookError error) {
SessionEvents.onLoginError(error.getMessage());
}
public void onError(DialogError error) {
SessionEvents.onLoginError(error.getMessage());
}
public void onCancel() {
SessionEvents.onLoginError("Action Canceled");
}
}
}
Please update the below code of your application. It will solve your problem.
public void loginAndPostToWall() {
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
new LoginDialogListener());
}
I had the same problem like you. Finally, I solved using this:
Open Facebook.java provided by the Facebook SDK and then change it like this:
public void authorize(Activity activity, String[] permissions,
int activityCode, final DialogListener listener) {
boolean singleSignOnStarted = false;
mAuthDialogListener = listener;
/*
// Prefer single sign-on, where available.
if (activityCode >= 0) {
singleSignOnStarted = startSingleSignOn(activity, mAppId,
permissions, activityCode);
}
// Otherwise fall back to the traditional dialog.
if (!singleSignOnStarted) {
*/
startDialogAuth(activity, permissions);
// }
}
This is just a wild guess, but instead of this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
this.facebookConnector.getFacebook().authorizeCallback(requestCode, resultCode, data);
}
Try:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
this.facebookConnector.getFacebook().authorizeCallback(requestCode, resultCode, data);
}
Since you're not calling the parent method some things might not work as expected...
private static Session openActiveSession(Activity activity, boolean allowLoginUI, StatusCallback callback, List<String> permissions) {
OpenRequest openRequest = new OpenRequest(activity).setPermissions(permissions).setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO).setCallback(callback);
Session session = new Session.Builder(activity).build();
if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
Edit your openactivesession function like this

Can't get Facebook log-in screen?

I'm following the Facebook Android SDK tutorial and I have all of the code leading up to this step in my app. I start the app (testing it on my phone, but does the same in Emulator) and this screen comes up:
Ok, great!
But, once it's done loading, instead of any sort of log-in screen coming up like the example given here:
I just come up with this:
Code:
package com.greatapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.facebook.android.*;
import com.facebook.android.Facebook.*;
public class MyGreatActivity extends Activity {
Facebook facebook = new Facebook("MY_APP_ID");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
facebook.authorize(this, new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
Ok, I deleted the project and started again. This screen from before is doing something, but keeps reloading this loading screen over and over and over again. Almost like it's contacting Facebook's servers for 1000 different things and each one has a loading screen. I don't know what to do. No errors in Logcat.
It works for me.
Is there any error message in logcat?
Do you use Internet permission?
Check these things.
or maybe you have already completed
or add some log in your code for example:
facebook.authorize(this, new DialogListener() {
#Override
public void onComplete(Bundle values) {
Log.d("onComplete",""+values);
}
#Override
public void onFacebookError(FacebookError error) {
Log.d("onFacebookError",""+error);
}
#Override
public void onError(DialogError e) {
Log.d("onError",""+e);
}
#Override
public void onCancel() {
Log.d("onCancel","cancel");
}
});
and see result in your logcat
Just modify your code like this:
import com.facebook.android.*;
import com.facebook.android.Facebook.*;
public class MyGreatActivity extends Activity {
public static final String[] PERMISSIONS = new String[] {"email", "publish_checkins", "publish_stream","offline_access"};
Facebook facebook = new Facebook("MY_APP_ID");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
facebook.authorize(this,PERMISSIONS,Facebook.FORCE_DIALOG_AUTH, new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
Facebook.FORCE_DIALOG_AUTH ---> will login facebook forcefully
also you need to call facebook.logout(context) method to logout current method ,before login with new user.
User this method:
public void Logout() throws MalformedURLException, IOException {
facebook.logout(mContext);
}

Categories

Resources