Question

May I ask you a question? How to get Facebook access token to create with Facebook Android SDK 3.8 ? I have tried various way to do it. But it seems like I don't do it the right way. The app is Android 4.0 SDK and API Level 14.

In my UI I have a classical Android Button.

<Button
android:id="@+id/btn_facebook"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="80dp"
android:background="@drawable/button_facebook_normal"
android:text="@string/btn_facebook"
android:textColor="@color/white" />

In my AndroidManifest.xml

and this

<!-- FACEBOOK SDK -->
<activity android:name="com.facebook.LoginActivity" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />

strings.xml

<string name="app_id">XXXXXXXX</string>

I just want to get the access token onClick of the button, then I can send it to the API application back end. In my configuration the namespace is missing but I don't know where to put it in the facebook sdk configuration.

Thanks a lot in advance.


I try to get something out of this https://developers.facebook.com/docs/facebook-login/access-tokens#usertokens


Ok my AppId was refering to an Application where I was not register as a developper so ... :) voila, the application was in a sandbox . right now I have the screen with "this application requiert your information"

The only thing missing right now is the Access Token which in a empty string at this time..

Here is the code

private String getAccessToken() {
    Session.OpenRequest request = new Session.OpenRequest(this);
    request.setPermissions(Arrays.asList("email", "user_birthday"));
    request.setCallback(mCallback);

    // get active session
    Session mFacebookSession = Session.getActiveSession();
    if (mFacebookSession == null || mFacebookSession.isClosed()) {
        mFacebookSession = new Session(this);
    }
    mFacebookSession.openForRead(request);
    return mFacebookSession.getAccessToken(); // the access token is empty here
}

StatusCallback mCallback = new Session.StatusCallback() {

    @Override
    public void call(Session session, SessionState state,
            Exception exception) {
        // TODO Auto-generated method stub
            System.out.println("test : "+session.getAccessToken());// the access token is empty here too.




    }

}; // the code you already have
Était-ce utile?

La solution

You need to pass the arguments from the onActivityResult method to the facebook session onActivityResult method, and the access token won't be empty, as follows:

    /**
    * Required for Facebook.
    */
    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        super.onActivityResult(arg0, arg1, arg2);
        Session.getActiveSession().onActivityResult(TheActivity.this, arg0, arg1, arg2);
    }

It's stated within the facebook android login document here, at the end of "Authentication change triggers" section.

Autres conseils

I get the access token like this :

private void onSessionStateChange(Session session, SessionState state, Exception exception) 
{
    if (state.isOpened()) 
    {
        Log.i(TAG, "Logged in...");
        pb.setVisibility(View.VISIBLE);
        authButton.setClickable(false);
        final Session curSession = Session.getActiveSession();

        Request r = Request.newMeRequest(curSession, new Request.GraphUserCallback() 
        {
            @Override
            public void onCompleted(GraphUser user, Response response) 
            {
                try 
                {
                    GlobalVars.fbId = user.getId();
                    GlobalVars.fbUName = user.getName();
                    GlobalVars.fbUFirstName = user.getFirstName();

                    String loginCheckURL = GlobalVars.server_url
                                            +"login.php?fb_user&fbID="+GlobalVars.fbId
                                            +"&fb_access_token=" +curSession.getAccessToken();

                    Log.e("Login Check", loginCheckURL);
                    new GetUserDetailsTask().execute(loginCheckURL);
                } 
                catch (Exception e) 
                {
                    justSomeMethThatHandlesTheVariousProblemsWeMightFaceWhileFacebookLogin(response.getError());
                }
            }
        });
        r.executeAsync();
    } 
    else if (state.isClosed()) 
    {
        Log.i(TAG, "Logged out...");
    }
}

Use this library if you don't want to hassle the Facebook SDK https://github.com/sromku/android-simple-facebook

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top