Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Member
davidvanvo
Posts: 3
Registered: ‎01-13-2011
My Device: Not Specified

Re: Facebook SDK problems

Mine has actually been working all day today (crossing fingers). I am using the suggest URL.

Contributor
icaro90302
Posts: 39
Registered: ‎09-28-2010
My Device: Not Specified

Re: Facebook SDK problems

Currently will work it with changing the URL because we all have the permissions granted to applications within facebook, but a new user to our application which do not have permission to access his/her data, will receive the white screen with the word "Success"

 

If you want to test, enter your facebook account and remove the permissions of your application to your profile and you will see as the white screen reappears.

Visitor
jimmd
Posts: 1
Registered: ‎01-13-2011
My Device: Not Specified

Re: Facebook SDK problems

Just got this to work with extended permissions with users who have already granted your app permissions and users who haven't. It's a bit of a hack, but it's working (on the Torch at least). This is for people who had the sdk working up until the 11th...

 

Login

As already stated, use the following url in the LoginScreen class:

 

 

http://m.facebook.com/login.php

 

 

The response you'll get back is going to depend on if the user has already granted your app permissions or not. If they haven't, you'll get the session back as an encoded JSON object in the query string. If they have, you'll simply get the access token in the query string.

 

If the response query string has "access_token=" then you should already be good to go. Grab it and call

 

fireAction(ACTION_LOGGED_IN, authToken);

 

 

If it has "session=" you have some work to do. Grab the value of "session" from the query string and do the following (you'll have to create another listener flag for the fireAction() method)

 

String jsonStr = URLDecoder.decode(session, "UTF-8");
JSONObject jsonObj = new JSONObject(jsonStr);
String session_key = jsonObj.getString("session_key");
fireAction(ACTION_LOGGED_IN_SESSION, session_key);

 

In your opening class's onAction method add

 

if (event.getAction().equals(LoginScreen.ACTION_LOGGED_IN_SESSION)) {
    fbc.getApplicationSettings().sessionKey = (String)event.getData();
    fbc.upgradeSession();
    // now you have the access token...
    String accessToken = fbc.getApplicationSettings().accessToken;
    // close web view and optionally launch permissions screen here ...
}

 

 

 

Extended Permissions

 

To get these, use the url that is in the PermissionsScreen class BUT change the redirect_uri to basically anything that actually resolves. Leaving it as "http://www.facebook.com/connect/login_success.html" will cause it to fail. Be sure to change the url on the onAction() method to match.

Developer
pwerry
Posts: 177
Registered: ‎01-21-2009
My Device: 9900

Re: Facebook SDK problems

[ Edited ]

Hey all,

 

Might I suggest we try adapting the Android sdk's method of obtaining the access token? The android SDK is authored and supported by facebook so it's unlikely the way that SDK does authentication will break. The login also looks and feels cleaner... when it actually works. However, I seem to be having an issue with the BrowserField where the redirect events aren't occurring, or at least not being handled correctly.

 

In the Android sdk they point to this site for login:

 

 

https://m.facebook.com/dialog/oauth?display=touch&type=user_agent&redirect_uri=fbconnect://success&c...

 

 

 

Where client_id is your app id and fbconnect://success is simply a callback url that your browser "should" be directed to with the eventual response of fbconnect://success#auth_token=[token]
The code modifcations you have to do in the Strawberry lib are relatively simple to start with.
1. change your NEXT_URL to :
 fbconnect://success

 

2. In LoginScreen, change the url to: 
What you will notice is that you'll make an initial first hop to: 
And then an immediate hop to:
At this point you're at the login screen. Attempt to login, and you will see a hop to:

It's at this point things go south. Firstly, there is a small difference between the last url on the bb than android. On android, refsrc=http://touch.facebook.com/login.php whereas on bb it is refsrc=http://m.facebook.com/dialog/oauth. I'm not really sure how relevant that is.

 

What's EXPECTED to happen is that you browser is bounced to uiserver.php once again and then the browser is redirected to fbconnect://success with the access token and expiry appended. I've yet to determine what needs to go on under the hood to get this to work but thought I'd post the progress here. If we can get this to work I think it will be a bit more reliable than using the permissions page.

Developer
Matoz
Posts: 15
Registered: ‎12-21-2008
My Device: Not Specified

Re: Facebook SDK problems

@jimmd: Your extended solution works for me too. Will test it on some devices now.

Developer
raquibulbari
Posts: 137
Registered: ‎09-30-2009
My Device: Torch 9800

Re: Facebook SDK problems

@jimmd Great, this is what I was looking for. Thank you very much

----------------------
Press like if you find a message helpful
---------
Contributor
dpolonsky
Posts: 27
Registered: ‎10-02-2010
My Device: Not Specified

Re: Facebook SDK problems

[ Edited ]

@jimmd I have tried to follow your sollution, but I have a problem with

 

fbc.upgradeSession();

 the response post call is [null]

 

StringBuffer response = httpClient.doPost(settings.graphUrl + "/oauth/exchange_sessions", data);

what seems to do the trick for me was changing the getSession method to retrieve and store only the session key, without the secret.


 

settings.sessionKey = response.getString("session_key");
restClient.setSessionKey(settings.sessionKey);

cheers

 

 

Developer
lyt0101
Posts: 28
Registered: ‎10-30-2010
My Device: Torch 9800

Re: Facebook SDK problems

Hello jimmd, thanks for posting the solution. But I still have some questions:

 

1. where do I fit the code where you grad the value of "session" in, say in the Strawberry example?

2. for extended permissions, what should I change the url to in order for it to jump back to my app screen?

 

I'm a newbie to BB dev, any help would be really appreciated.

 

Thanks!

 

Developer
pwerry
Posts: 177
Registered: ‎01-21-2009
My Device: 9900

Re: Facebook SDK problems

[ Edited ]

Hello everyone :smileyhappy:

 

Just wanted to share that I was able to get the android/iphone sdk based oauth scheme working on BB, which is probably the safest option since those two sdks are authored by Facebook, whereas the BB version is not. Facebook is *unlikely* to break their own sdk.

 

The basic idea is as follows but I'll leave the actual implementation open because there's probably many ways to do this and I'm sure mine is not the prettiest:

 

Global:

Set your NEXT_URL to "fbconnect://success"

 

LoginScreen:

1. Change your login url to https://m.facebook.com/dialog/oauth?display=touch&type=user_agent&redirect_uri=fbconnect://success&c... id]

 

You may want to remove the display=touch component on pre-6.0 devices because the resulting page doesn't render well.

 

2. You'll actually add the following method to BrowserScreen, but will override it in LoginScreen for a little polymorphism action (you can do this however you like, I just found it to be the quickest):

 

 

public boolean scrapeAuthTokenFromUrl( String url )
{
    if ( url == null || url.indexOf( "fbconnect://" ) == -1 ) return false;
        
    int startIndex = url.indexOf("access_token");

    if (startIndex > -1) 
    {
        int stopIndex = url.length();

        if (url.indexOf('&', startIndex) > -1) 
        {
            stopIndex = url.indexOf('&', startIndex);
        } 
        else if (url.indexOf(';', startIndex) > -1) 
        {
            stopIndex = url.indexOf(';', startIndex);
        }

        String authToken = url.substring(url.indexOf('=', startIndex) + 1, stopIndex);

        // Set your graph token here for api calls and make action success callback
                
        return true;
    }
            
    return false;
}

 

 

BrowserScreen:

Just want to mention quickly that I have been unable to get this to work on the simulator as the browser doesn't seem to be handling redirects correctly. Also note that the browser will behave slightly differently on different OSs so you have to be aware of those cases.

 

1. In eventOccurred(int event) make the following changes:

 

case Event.EVENT_URL_REQUESTED: {
    UrlRequestedEvent urlRequestedEvent = (UrlRequestedEvent) event;
    String absoluteURL = urlRequestedEvent.getURL();
                        
    if ( scrapeAuthTokenFromUrl( absoluteURL ) ) return null; // new
    
    urlRequestedEvent.getHeaders().removeProperties("referer");
    PrimaryResourceFetchThread thread = new PrimaryResourceFetchThread(absoluteURL, urlRequestedEvent.getHeaders(), urlRequestedEvent.getPostData(), event);
    thread.start();
 
    break;
}

...

case Event.EVENT_REDIRECT: {
    RedirectEvent redirectEvent = (RedirectEvent) event;
    String referrer = redirectEvent.getSourceURL();
                        
    if ( scrapeAuthTokenFromUrl( redirectEvent.getLocation() ) ) return null; // new

    // continue as before
}

...

 

 

MyApplication:

1. Implement the ActionListener interface and register with the LoginScreen

2. in onAction():

 

public void onAction( Action event )
{
    // This custom action will be fired as a result of finding the auth token.
    if ( event.getAction().equals( ACTION_AUTH_TOKEN_RECEIVED ) )
    {
        // Wrap this in an invokeLater to make sure it runs on the event thread
        UiApplication.getUiApplication().invokeLater( new Runnable() {
            public void run()
            {
                UiApplication.getUiApplication().popScreen( _loginScreen );

                // do more stuff
            }
        });
    }
}

 

All this will get you the auth token, and the login page looks slightly better than before. 

 

Developer
pwerry
Posts: 177
Registered: ‎01-21-2009
My Device: 9900

Re: Facebook SDK problems

Just wanted to mention, in the scrape method you should also check for the expiry time, appended in the same url callback as the access token. It's shorter than you think (about an hour and a half)