我需要授予用户对身份验证的读取和发布权限。发布用于发布他们的分数,也保存在Facebook上。我使用以下代码段进行身份验证。
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
final AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
loginResult.getAccessToken().getPermissions();
nextActivity(profile);
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) { }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
backtogame = (Button) findViewById(R.id.backtogame);
backtogame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setLoginBehavior(LoginBehavior.WEB_ONLY);
// if (isLoggedIn()) {
// Profile profile = Profile.getCurrentProfile();
//nextActivity(profile);
// }
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
nextActivity(newProfile);
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
nextActivity(profile);
}
@Override
public void onCancel() {
Toast.makeText(LoginActivity.this, "Canceled!", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException e) {
Toast.makeText(LoginActivity.this, "Error, Check your internet connection!", Toast.LENGTH_SHORT).show();
}
};
loginButton.setReadPermissions("public_profile", "user_friends", "user_games_activity");
loginButton.registerCallback(callbackManager, callback);
}
@Override
protected void onResume() {
super.onResume();
//Facebook login
Profile profile = Profile.getCurrentProfile();
// loginButton.clearPermissions();
// loginButton.setPublishPermissions("publish_actions"); //write permissions should be set after read permission, and need to change category on facebook to game
nextActivity(profile);
}
@Override
protected void onPause() {
super.onPause();
}
protected void onStop() {
super.onStop();
//Facebook login
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
super.onActivityResult(requestCode, responseCode, intent);
//Facebook login
LoginManager.getInstance().logOut();
callbackManager.onActivityResult(requestCode, responseCode, intent);
}
如果我添加&#34; publish_actions&#34;对loginbutton的权限,它将不再要求读取权限。
我在网上发现的所有文章都与会话参数有关,而该会话参数已不再使用。