Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat to do if facebook session / token is invalid in my application?
    primarykey
    data
    text
    <p>I'm a beginner with the Facebook SDK (3.0.1) for Android and I'm wondering a lot of questions that I was not able to find on the official documentation.</p> <p>In the facebook example, to ask for new permissions, they are using the UILifeCyclerHelper in the same activity than the login activity. My case is a little bit different.</p> <p>In my case, I log in the user in a first activity, and then, more later, I'm asking for permissions to publish on its wall.</p> <p>1) I'm wondering about what to do when the session is null, closed, or in an other state that don't allow me to ask these permissions.</p> <p>2) Because the publishing on the wall is do later by my server, I need to send the user access token. Here again, what can I do if this token is null / invalid ?</p> <p>3) I'm currently asking permissions like bellow. What can I do to have a callback of the user's answer ? Is this happenned onActivityResult ? What kind of answers are returned ?</p> <p>Per advance, thanks for your help !</p> <pre><code>Session session = Session.getActiveSession(); if (session != null){ // Check for publish permissions List&lt;String&gt; permissions = session.getPermissions(); if (!isSubsetOf(PERMISSIONS, permissions)) { Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS); session.requestNewPublishPermissions(newPermissionsRequest); } else{ launchPublishWallTask(); } private static final List&lt;String&gt; PERMISSIONS = Arrays.asList("publish_stream"); private boolean isSubsetOf(Collection&lt;String&gt; subset, Collection&lt;String&gt; superset) { for (String string : subset) { if (!superset.contains(string)) { return false; } } return true; } </code></pre> <hr> <p>EDIT (to answer to Ming Li's answer) :</p> <p>Thanks for your answer... but there is some points that I don't understand again.</p> <p>1) If my session is null, do you say to simply do that ?</p> <pre><code>if (session == null){ session = new Session(this.getSherlockActivity()); Session.setActiveSession(session); } </code></pre> <p>By doing that, the user will have to login again, no ? Permissions will disappear from this new Session object, even if user has accepted it before, isn't it ?</p> <p>2) I have read that the user access token is only valid during 2 houres, and is invalidated in some case, like the user reset password for example. So, by recreating the session object (question 1 above), even if I have the session object AND permissions, is it sure that I will have a valid user access token too ?</p> <p>3) OK, thanks</p> <p>To resume my question, is it a good way to do "only" that in my case (I have not copy here the onActivityResult method, but it's ok for that):</p> <pre><code>public void onClick(View v){ // Retrieve Facebook session object Session session = Session.getActiveSession(); if (session == null){ session = new Session(this.getSherlockActivity()); Session.setActiveSession(session); } session.addCallback(this); if (session != null){ // Check for publish permissions List&lt;String&gt; permissions = session.getPermissions(); if (!isSubsetOf(PERMISSIONS, permissions)) { Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest( this, PERMISSIONS ); session.requestNewPublishPermissions(newPermissionsRequest); } else{ launchPublishWallTask(); } } } @Override public void call(Session session, SessionState state, Exception exception) { if (session != null &amp;&amp; isSubsetOf(PERMISSIONS, session.getPermissions())){ launchPublishWallTask(); } else{ Toast.makeText(getSherlockActivity(), "you have to accept !", Toast.LENGTH_LONG).show(); } } private void launchPublishWallTask(){ // Here, I'm sure that I will have a valid Session.getActiveSession().getAccessToken() .. or not ? } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload