11

I am trying to implement Facebook integration into my Android app and it goes off and logs in to Facebook fine, but when it tries to pass the access token back to the app it just returns:

The webpage at fbconnect://success#access_token=[ACCESS TOKEN] might be temporarily down or it may have moved permanently to a new web address.

Obviously where [ACCESS TOKEN] is a long sequence of chars.

I have got the App ID correct and added the key hash to facebook. But what process could I have missed?

Code:

public class FacebookActivity extends Activity {

private static final String APP_ID = "[MY APP ID]";
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";

private Facebook facebook;
private String messageToPost;

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();
    }

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    facebook = new Facebook();
    restoreCredentials(facebook);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_facebook);

    String facebookMessage = getIntent().getStringExtra("facebookMessage");
    if (facebookMessage == null){
        facebookMessage = "Test wall post";
    }
    messageToPost = facebookMessage;

            if (! facebook.isSessionValid()) {
        loginAndPostToWall();
    }
    else {
        postToWall(messageToPost);
    }
}

public void loginAndPostToWall(){
     facebook.authorize(this, APP_ID, PERMISSIONS, 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!");
        }
        finish();
    } catch (Exception e) {
        showToast("Failed to post to wall!");
        e.printStackTrace();
        finish();
    }
}

class LoginDialogListener implements DialogListener {
    @Override
    public void onComplete(Bundle values) {
        saveCredentials(facebook);
        if (messageToPost != null){
        postToWall(messageToPost);
    }
    }
    public void onFacebookError(FacebookError error) {
        showToast("Authentication with Facebook failed!");
        finish();
    }
    public void onError(DialogError error) {
        showToast("Authentication with Facebook failed!");
        finish();
    }
    public void onCancel() {
        showToast("Authentication with Facebook cancelled!");
        finish();
    }
}

private void showToast(String message){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

}

New to Android development so I'm sure it's something simple.

anothershrubery
  • 20,461
  • 14
  • 53
  • 98
  • what is your problem? you are unable to post messages – TNR Dec 14 '12 at 14:56
  • Well that is the ultimate goal, but the problem is that when Facebook tries to redirect back to my app, with the access token, it show the above message. (Webpage not available). Therefore my app can't get the access token and therefore can never be authorised. – anothershrubery Dec 14 '12 at 15:34
  • are your application going to facebook application and then it is not providing the token or it is nt giving you token when u login from ur application directly – Parth Dani Dec 17 '12 at 11:11
  • Also had you tested the app id by using it in the demo provided by them.Is it working properly over there – Parth Dani Dec 17 '12 at 11:12

2 Answers2

1

I updated the native facebook app on the device and everything works.

xiangxin
  • 409
  • 6
  • 18
1

Seems like the native Facebook App must be version 2 or higher. I am seeing the same issue for 1.9.6 version, and upgrading the Facebook app solves the issue.

s1m3n
  • 623
  • 6
  • 21