I am using FB API 3.0 for Android and I am trying to establish a session correctly.
But it seems that I am doing something wrong or saying that I am missing something.
In my launch activity, I check if there is already a session, and if it does not exist, I create a new one (as in the FB example):
Settings.addLoggingBehavior(LoggingBehaviors.INCLUDE_ACCESS_TOKENS); Session session = Session.getActiveSession(); if (session != null){ String sesija = session.toString(); Log.w ("ss", sesija);} if (session == null) { if (savedInstanceState != null) { session = Session.restoreSession(this, null, MyGlobals.INSTANCE.statusCallback, savedInstanceState); Log.w("No Session found", "Loading saved session as it exists"); } if (session == null) { session = new Session(this); Log.w("No Session found", "CreatingNewSession"); String sesija2 = session.toString(); Log.w ("ss2", sesija2); } Session.setActiveSession(session); if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) { session.openForRead(new Session.OpenRequest(this).setCallback(MyGlobals.INSTANCE.statusCallback)); Log.w("There is an active session", "Active Session Exists"); } }
At this point, my journal says the following:
10-24 09:11:52.284: W/ss(1404): {Session state:CREATED, token:{AccessToken token: permissions:[}, appId:xxxxxx}
(AppId is true)
Then in my snippet, I try to login to facebook using this code:
Session session = Session.getActiveSession(); if (!session.isOpened() && !session.isClosed()) { Log.w("Session is not opend", "Session is not closed"); session.openForRead(new Session.OpenRequest(getActivity()).setCallback(MyGlobals.INSTANCE.statusCallback)); } else { Session.openActiveSession(getActivity(), true, MyGlobals.INSTANCE.statusCallback); Log.w("Open Active Session", "Status Callback"); }
At this moment, the FB web view appears, I set the username and pass it on, it is kind, it works, and then disappears.
Now Iโm not looking anywhere, I donโt see the answer and TOKEN, and if I press again to enter (on the same button), my application will fail.
In the log I have this:
10-24 09:12:31.944: W/ss(1404): {Session state:OPENING, token:{AccessToken token: permissions:[}, appId:xxxxxx}
If I compare it with the SessionLoginExample provided by FB, I will get there:
10-24 09:08:50.004: W/ss(1356): {Session state:OPENED, token:{AccessToken token:AAAFaKtb7Pg4BALYg4B5eosa0ZAE9ZAXB0ZBMFDJdNbsDsZAkGJUfKtGs71OEJikDxT2VBfo4QMXiNASz23ZAa6D76eUxW0mZAMa013HP2kxgZDZD permissions:[}, appId:xxxxxxx}
Meaning I am doing something wrong, because I will not catch the returned AccessToken, and it is not saved properly.
What I think is wrong, but I donโt understand how I should read the information about the session correctly or say where I can catch and save information about tokens inside the fragment.
I tried to insert a fragment:
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Boolean session = Session.getActiveSession().onActivityResult(getSherlockActivity(), requestCode, resultCode, data); }
Since it was "onActivityResult" in the example, but it seems like it is not doing anything.
Just to summarize: I create a session for launch activity. Then, if the FB application is not installed, in Tab4 (settings) the user should be able to log into the FB using webview. Then on Tab1 and Tab2 the user should be able to share the image with FB, as on Instagram. If the application is installed, I would disable tab 4 and allow the user to share the FUHHGG application using Tab1 and Tab2.
Tnx.
RESOLVED: A thing that is never written, but must be well known. When FB authentication is performed, the "On Activity Result" code must be executed to save the token with the session. However, if you are dealing with fragments and tabs, like me, the "onActivityResult" function is never executed in the code of the fragment, but must be executed in the Activity, which takes care of the fragments / tabs.