Reputation: 2170
when I use HelloFacebookSample the code works fine and I was able to post on the user wall.
but when I try to post video and embeded it on the wall , to do so I used publish to feed tutorial
when i convert my code i replace postStatusUpdate()
methood with publishStory()
public void postStatusUpdate() {
if (user != null && hasPublishPermission()) {
final String message ;
message = ( user.getFirstName()+" "+ getString( R.string.status_update_link)+ " " +video_id + " " + getString(R.string.google_play_link));
Request request = Request
.newStatusUpdateRequest(Session.getActiveSession(), message, new Request.Callback() {
@Override
public void onCompleted(Response response) {
showPublishResult(message, response.getGraphObject(), response.getError());
}
});
request.executeAsync();
} else {
pendingAction = PendingAction.POST_STATUS_UPDATE;
}
}
replace with this :
private void publishStory() {
Session session = Session.getActiveSession();
if (session != null){
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Bundle postParams = new Bundle();
postParams.putString("message", getString( R.string.google_play_link));
postParams.putString("link", "http://www.youtube.com/watch?v="+ video_id);
postParams.putString("source", "http://www.youtube.com/v/" + video_id);
postParams.putString("picture","http://img.youtube.com/vi/" +video_id + "/0.jpg");
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(TAG,
"JSON error "+ e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(MyApp.intense,
error.getErrorMessage(),
Toast.LENGTH_LONG).show();
} else {
/**
Toast.makeText(MyApp.intense,
// postId
"Sent"
,
Toast.LENGTH_LONG).show();
*/
}
}
};
Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
so when i try to post, the app is cresh and I recieve " Session: an attempt was made to request new permissions for a session that has a pending request"
I dont find when the first permission...
in other Q that posted in this form the developer recieve the same error MSG like I receive but it look like this error appear when the session is not OPENED and the user call it, I belive that not the problem in this case.
I attached more code from this FragmentActivity because I believe it necessary.
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (pendingAction != PendingAction.NONE &&
(exception instanceof FacebookOperationCanceledException ||
exception instanceof FacebookAuthorizationException)) {
new AlertDialog.Builder(HelloFacebookSampleActivity.this)
.setTitle(R.string.cancelled)
.setMessage(R.string.permission_not_granted)
.setPositiveButton(R.string.ok, null)
.show();
postStatusUpdateButton.setEnabled(false);
pendingAction = PendingAction.NONE;
} else if (state == SessionState.OPENED_TOKEN_UPDATED) {
handlePendingAction();
}
updateUI();
}
private enum PendingAction {
NONE,
POST_STATUS_UPDATE
}
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
Upvotes: 1
Views: 4211
Reputation: 343
Your call to session.requestNewPublishPermissions(newPermissionsRequest);
will generate a Facebook dialog asking the user to give the permission. You need to @Override onActivityResult()
to capture and apply what comes back form the Facebook dialog. In my app I first ask the user to authenticate my app, and then ask for permission to access their Groups...
public class FacebookManage extends Activity {
// Facebook
private Session fbSession = null;
private int authCount = 0;
// There is a UI button that calls addAccount();
public void addAccount(View v) {
// Open a new Session
OpenRequest or = new Session.OpenRequest(this);
or.setCallback(null);
// Login
Session.Builder sb = new Session.Builder(this);
Session session = sb.build();
Session.setActiveSession(session);
// This will bring up a Facebook dialog and return a call to onActivityResult
session.openForRead(or);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE) {
if (authCount == 0) {
// This is the first authentication where we got basic access to the user account
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
fbSession = Session.getActiveSession();
if (fbSession != null && !fbSession.isClosed()) {
// Ask for Group permission
authCount++;
List<String> fbPermissions = new ArrayList<String>();
fbPermissions.add("user_groups");
NewPermissionsRequest snpr = new NewPermissionsRequest(this, fbPermissions);
fbSession.requestNewReadPermissions(snpr);
}
} else if (authCount == 1) {
// Returning from asking for access to "Groups"
if (fbSession != null && !fbSession.isClosed()) {
// This will add the group permission to the current session
fbSession.onActivityResult(this, requestCode, resultCode, data);
}
}
}
}
I actually do three rounds of asking for permissions (Initial, Groups, Pages and Publish) and just use the authCount
to keep track of where I am. The user sees 4 Facebook dialogs, but the nice thing they have but single "permission" requests each. You get the idea...
I just call fbSession.close();
when I'm all done. Then the access token is cached and managed by the Facebook SDK. By using one Activity to do the Authentication and letting the SDK keep track of that, in other activities (pages) I just need to call fbSession = Session.openActiveSessionFromCache(this);
and I'm good to go.
(Note - if the user goes into their Facebook settings and specifically nukes some of the permissions you originally asked for, you won't know it until you try and publish. Ah, error trapping...)
Upvotes: 0
Reputation: 1015
You can just add pendingPublishReauthorization = true;
//so
if (session != null){
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
It's working for me.
Hope this help
Upvotes: 1