I'm trying to give users an option to set/revoke publishing permission via checkbox (Facebook SDK for Android). Code is provided below. Everything works fine except that after revoking the code responsible for checking publishing permissions fails miserably.
I understand that Session has no way of knowing if user has revoked any permissions after loggin in. What is the correct way to handle this kind of situation? Do I have to query available permissions manually, or is there a way to seamlessly recreate session with basic permissions?
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
publishCheckbox = (CheckBox) view.findViewById(R.id.publishCheckbox);
publishCheckbox.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
CheckBox chb = (CheckBox) v;
if (chb.isChecked() && checkForPublishPermission() == false){
requestPublishPermissions();
}
else{
removePublishPermissions();
}
}
});
...
}
private void requestPublishPermissions() {
Session session = Session.getActiveSession();
if ( session!= null && session.isOpened()){
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS).setRequestCode(REAUTH_ACTIVITY_CODE);
newPermissionsRequest.setCallback(callback);
session.requestNewPublishPermissions(newPermissionsRequest);
}
}
private void removePublishPermissions() {
Request r = new Request(Session.getActiveSession(), PUBLISH_ACTIONS_PERMISSION_PATH, null, HttpMethod.DELETE);
r.setCallback(new Request.Callback() {
@Override
public void onCompleted(Response response) {
publishCheckbox.setEnabled(true);
}
});
r.executeAsync();
publishCheckbox.setEnabled(false);
}
private boolean checkForPublishPermission(){
boolean result = false;
Session session = Session.getActiveSession();
if (session == null || !session.isOpened()) {
result = false;
}
else{
List<String> permissions = session.getPermissions();
if (permissions.containsAll(PERMISSIONS)) {
result = true;
}
}
return(result);
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
...
publishCheckbox.setChecked(checkForPublishPermission());
...
} else if (state.isClosed()) {
...
}
}
Well I ended up requesting user permissions each time I needed such a check, something along these lines:
private void checkForPublishPermission(){
mPublishCheckbox.setEnabled(false);
Request r = new Request(Session.getActiveSession(), PUBLISH_ACTIONS_PERMISSION_PATH, null, HttpMethod.GET);
r.setCallback(new Request.Callback() {
@Override
public void onCompleted(Response response) {
if (response != null) {
GraphObject o = response.getGraphObject();
if ( o != null){
JSONArray data = o.getInnerJSONObject().optJSONArray("data");
mCanPublish = (data.optJSONObject(0).has("publish_actions"))?true:false;
mPublishCheckbox.setChecked(mCanPublish);
}
}
mPublishCheckbox.setEnabled(true);
}
});
r.executeAsync();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With