当我第一次通过Facebook登录应用时,我收到.hide-nav .owl-controls {
display: none;
}
的个人资料
当我退出应用并再次启动时,它已经登录。所以我可以直接调用Profile.getCurrentProfile();
返回null。
代码
Profile.getCurrentProfile();
答案 0 :(得分:1)
这有两种情况:
AccessToken
,然后获取个人资料。刷新令牌代码可以在this answer中找到,但下面的代码包含它(为了简化)。代码取自FB's dreadful documentation)。
您可以将此代码直接放入您的应用中,注释中显示"案例1",只需调用您正常的FB登录。
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();
}
答案 1 :(得分:-1)
而不是访问Profile.getCurrentProfile()
在onSuccess()
中使用访问令牌并生成图形请求(这对我有用)
这里的代码片段:
FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.v("profile track", (DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(loginResult.getAccessToken().getExpires())));
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
String name = object.getString("name");
String email = object.getString("email");
String id = object.getString("id");
Toast.makeText(Login.this, name + " " + " " + email + " " + id, Toast.LENGTH_SHORT).show();
/*write your code that is to be executed after successful login*/
} catch (JSONException ex) {
ex.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
};
答案 2 :(得分:-3)
获取个人资料详情后,请致电LoginManager.getInstance().logOut();
。