I can not get the facebook token

0

Hi, I am authenticating Facebook using the callback of the AccessTokenTracker class, onCurrentAccessTokenChanged, the problem is that it never runs and I can not access the AccessToken to see the data. I followed the documentation and still after running the method FacebookCallbackManager.onActivityResult (requestCode, resultCode, data); nothing else is executed. The code that I have is within a Fragment and is as follows:

@Override
public void onCreate(Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(this.getContext());
CallbackManager mFacebookCallbackManager = CallbackManager.Factory.create();
AccessTokenTracker mFacebookAccessTokenTracker = new AccessTokenTracker() {
    @Override
    protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken  currentAccessToken) {
           // AQUI NO SE EJECUTA NUNCA
      }  
}   };
if (AccessToken.getCurrentAccessToken()!=null)
    // NO ENTRA NUNCA
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState) {
    View view;
    setHasOptionsMenu(true);
    view = inflater.inflate(R.layout.login_layout, container, false);
    btnFacebook = (LoginButton)view.findViewById(R.id.sign_conectar_f) ;
    btnFacebook.setReadPermissions("public_profile email");
    btnFacebook.setFragment(this);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);                               
        mFacebookCallbackManager.onActivityResult(requestCode, resultCode, data);
    }
    
asked by JaviDonisio2 20.04.2017 в 05:55
source

1 answer

0

You have to add the registerCallback to your button, that will return a loginResult, with the loginResult you get the accesstoken.

btnFacebook.registerCallback(mFacebookCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
    ...
    String token = loginResult.getAccessToken().getToken()
    ...
}

@Override
public void onCancel() {
    //usuario no dio permiso
}

@Override
public void onError(FacebookException e) {
    e.printStackTrace();
}

});

    
answered by 20.04.2017 в 17:28