Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use AccessTokenTracker in Android Facebook SDK

I am using Facebook SDK 4.0

When I go to app setting and delete the permissions that I have given previously AccessTokenTracker is not called. What should I do to AccessTokenTracker be called?

I am testing the use cases of login, the following one is not tracked by AccessTokenTracker:

Someone removes your app from Facebook via app settings and revisits your app. Your app should detect this and prompt the person to log back in.

1)Go to your app and tap on the "Log in with Facebook” button
2)Tap OK to accept the read permissions (and OK again to accept write permissions where applicable)
3)Go to app settings on Facebook and remove your app
4)Repeat steps 1-2 and verify that Facebook Login works

Here is the code of my MainActivity, it is just a simple app to learn how to use the SDK.

package com.ognid.acesstokentracker;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;

import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookSdk;


public class MainActivity extends ActionBarActivity {
    CallbackManager callbackManager;
    AccessTokenTracker accessTokenTracker;
    Fragment selection=new SelectionFragment();
    Fragment login=new LoginFragment();
    String TAG=MainActivity.class.getSimpleName();


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

        FacebookSdk.sdkInitialize(this.getApplicationContext());
        callbackManager = CallbackManager.Factory.create();

        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(
                    AccessToken oldAccessToken,
                    AccessToken currentAccessToken) {
                Log.d(TAG,"onCurrentAccessTokenChanged");
                updateWithToken(currentAccessToken);

            }
        };
        setContentView(R.layout.activity_main);
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG,"is tracking:"+accessTokenTracker.isTracking());

    }

    private void updateWithToken(AccessToken token) {
        if(token!=null)showFragment(selection);
        else showFragment(login);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        accessTokenTracker.stopTracking();
    }

    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        updateWithToken(AccessToken.getCurrentAccessToken());
    }

    void showFragment(Fragment f){
        getSupportFragmentManager().beginTransaction().replace(R.id.container,f).commit();
    }



}
like image 705
diego.goncalves Avatar asked Sep 29 '22 05:09

diego.goncalves


1 Answers

In general, if you revoke a permission from facebook, it locally (in your app) does not change. In this situation the facebook api will inform you with a permission error (200) if your app does not have sufficient privileges to perform a certain request. Let's say you want to publish and locally your token has the publish permission then, if you try to post, the API will return with an error code 200. At this point you know that you have to re-request publish permissions and if the user grants your app this permission the AccessTokenTracker will be invoked.

Note that, as per Facebook Android SDK the "local" access token can be outdated. Take a look a the javadoc of the getPermission() method of the AccessToken class.

like image 50
Francesco Mameli Avatar answered Oct 05 '22 06:10

Francesco Mameli