Setting up an Android FB API 3.0 Session with Snippets

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.

+4
source share
1 answer

So it looks like you have already worked on this, and your workaround is okay, but I would like to give you another option here if using this snippet is cleaner for you or others.

In cases where you cannot or do not want to receive information from FacebookActivity, you can use either your activity or your fragment to initialize the session. Both Activity and Fragment have onActivityResult that you can override, and the one that is used depends on what you pass to initialize Session.OpenRequest.

So, if you want the callback to come to the fragment, you can call:

 ...new Session.OpenRequest(myFragment)... 

Then override onActivityResult in your fragment and redirect the call to your session.

It's a little nicer that your fragment no longer needs special knowledge of what activity it works in.

+4
source

Source: https://habr.com/ru/post/1441771/


All Articles