none
Facebook Login on Android

    Question

  • Hi,

    I have an android app that uses facebook login, therefore I have an access token(via Session.getCurrentSession().getAccessToken()).

    In the example that shows the authentication feature for Windows Azure Mobile Services, they use a new Login window to authenticate withe the mobile service. I do not need a new window therefore I use the method that takes an acces token as a string.

    I tried to use the method that takes an access token and used the access token from the current active session, via the facebook sdk, but it does not seem to work. It returns an error on onComplete and the cause is {"code":400,"error":"SyntaxError: Unexpected token B"}. My access toke starts with B, if that helps.

    What is the format that I should use in order to pass my access token to the service?

    Thanks,

    Alex

    Tuesday, April 02, 2013 8:29 PM

Answers

  • The format for the token which should be passed to the method is {"access_token", "the-actual-access-token"}. The "actual-access-token" is the value you receive from the Facebook SDK. Your code should look somehow like the following:

    if (currentSession != null && currentSession.isOpened())
        JsonObject token = new JsonObject();
        token.addProperty("access_token", currentSession.getAccessToken());
        serviceClient.login(MobileServiceAuthenticationProvider.Facebook, token,  new UserAuthenticationCallback() {
            // implementation here
        });
    }
    


    Carlos Figueira

    • Marked as answer by Alex Gyori Wednesday, April 03, 2013 8:17 PM
    Wednesday, April 03, 2013 8:10 PM

All replies

  • Hi Alex,

    Can you provide code or steps that shows the problem?

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, April 03, 2013 12:00 PM
    Owner
  • Hi Jeff,

    I am using the LoginButton widget to manage login with facebook:

        <com.facebook.widget.LoginButton
                android:id="@+id/facebook_login_button"
                android:layout_width="wrap_content"
            android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp" />


    I am using this code to sign in with the mobile service:

    public void login(){
    Session currentSession = Session.getActiveSession();

    if(currentSession!=null && currentSession.isOpened())
    serviceClient.login(MobileServiceAuthenticationProvider.Facebook,currentSession.getAccessToken(),  new UserAuthenticationCallback() {

    @Override
    public void onCompleted(MobileServiceUser user, Exception exception,
    ServiceFilterResponse response) {
    if(exception!=null)
    Log.d("authentication", "the authentication failed", exception); }
    });
    }

    Thanks,

    Alex

    Wednesday, April 03, 2013 12:10 PM
  • Hi Alex,

    The current API has no 'currentSession' attribute, so this call would be invalid:

    MobileServiceAuthenticationProvider.Facebook,currentSession.getAccessToken()

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, April 03, 2013 2:14 PM
    Owner
  • Hi Jeff,

    What do you mean is invalid?

    I am using the windows azure mobile services api for android. I just looked at the sources on GitHub, there is a method: 

    public void login(MobileServiceAuthenticationProvider provider,String oAuthToken,UserAuthenticationCallbackcallback).

    I assumed that session.getAccessToken() would provide exactly what is needed.

    When I said Session before, I meant com.facebook.Session (developers.facebook.com/docs/reference/android/3.0/Session/)

    Thanks,

    Alex

    Wednesday, April 03, 2013 2:27 PM
  • Ah,

    Sorry, from your previous code the comma looked like a period and that was really confusing me!

    There are some possibilities:

    • The return value for the token is some weird encoding so the oAuth token is not valid.
    • The token is not valid for some reason.
    • The token is being provided at the wrong time.

    I am a bit new to Android development but there must be a way to inspect the http/https traffic and see what auth tokens are being passed back and forth.  Are you aware of tools that allow this? (looks like you can use my fave tool fiddler: http://www.mostlydevelopers.com/blog/post/2012/05/04/Debugging-Web-Requests-with-Fiddler-for-Android.aspx  ).

    Can you provide a very simple project based on the Todo sample and uses the Facebook SDK so I can reproduce the issue?  I may be able to spot something here.

    -Jeff


    Jeff Sanders (MSFT)



    Wednesday, April 03, 2013 2:41 PM
    Owner
  • The format for the token which should be passed to the method is {"access_token", "the-actual-access-token"}. The "actual-access-token" is the value you receive from the Facebook SDK. Your code should look somehow like the following:

    if (currentSession != null && currentSession.isOpened())
        JsonObject token = new JsonObject();
        token.addProperty("access_token", currentSession.getAccessToken());
        serviceClient.login(MobileServiceAuthenticationProvider.Facebook, token,  new UserAuthenticationCallback() {
            // implementation here
        });
    }
    


    Carlos Figueira

    • Marked as answer by Alex Gyori Wednesday, April 03, 2013 8:17 PM
    Wednesday, April 03, 2013 8:10 PM