首次登录后,Facebook Profile.getCurrentProfile()始终返回null [英] Facebook Profile.getCurrentProfile() is always returns null after the first login
本文介绍了首次登录后,Facebook Profile.getCurrentProfile()始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我第一次通过Facebook登录该应用程序时,是从Profile.getCurrentProfile();
For the first time when I login into the app through Facebook, I'm getting profile from Profile.getCurrentProfile();
就像我退出应用程序并再次启动时一样,它已经登录.因此我可以直接调用Profile.getCurrentProfile();
返回null.
Where as when I exit the app and launch again, It was already logged in. So I can call directly Profile.getCurrentProfile();
is returning null.
代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_page);
Profile profile = Profile.getCurrentProfile();
// For the first launch the profile will be null
displayProfileName(profile);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile");
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(
Profile oldProfile, Profile currentProfile) {
profileTracker.stopTracking();
Profile.setCurrentProfile(currentProfile);
Profile profile = Profile.getCurrentProfile();
displayProfileName(profile);
}
};
profileTracker.startTracking();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (profileTracker != null) {
profileTracker.stopTracking();
}
}
@Override
protected void onPause() {
super.onPause();
// Logs 'app deactivate' App Event.
AppEventsLogger.deactivateApp(this);
}
@Override
protected void onRestart() {
super.onRestart();
AppEventsLogger.activateApp(this);
}
/**
*
* Method to display the Profile Name
*/
private void displayProfileName(Profile profile) {
if (profile != null) {
Toast.makeText(MainActivity.this, profile.getName(),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "No Profile", Toast.LENGTH_LONG)
.show();
}
}
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);
callbackManager.onActivityResult(arg0, arg1, arg2);
}
推荐答案
有两种情况:
- For first login, follow my other answer.
- For second login, you will need to refresh your
AccessToken
and then fetch the profile. Refreshing token code can be found in this answer but the code below has it (for simplification).
该代码取自 FB的可怕文档).
您可以将此代码直接放入您的应用中,其中注释为案例1",只需调用您的常规FB登录即可.
You can put this code straight into your app, where the comment says "case 1", just invoke your normal FB login.
private AccessTokenTracker mAccessTokenTracker;
private void loginToMyFbApp() {
FacebookSdk.sdkInitialize(this);
if (AccessToken.getCurrentAccessToken() != null) {
mAccessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
mAccessTokenTracker.stopTracking();
if(currentAccessToken == null) {
//(the user has revoked your permissions -
//by going to his settings and deleted your app)
//do the simple login to FaceBook
//case 1
}
else {
//you've got the new access token now.
//AccessToken.getToken() could be same for both
//parameters but you should only use "currentAccessToken"
//case 2
fetchProfile();
}
}
};
mAccessTokenTracker.startTracking();
AccessToken.refreshCurrentAccessTokenAsync();
}
else {
//do the simple login to FaceBook
//case 1
}
}
private void fetchProfile() {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
// this is where you should have the profile
Log.v("fetched info", object.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link"); //write the fields you need
request.setParameters(parameters);
request.executeAsync();
}
这篇关于首次登录后,Facebook Profile.getCurrentProfile()始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文