This was the code that I implemented to get the access Token.  Obviously it
isn't finished yet because I need to implement some error handling, actual
saving and of the user, etc., but it's a framework that hopefully can help
someone else that wants to use OAuth in the Scribe package.

Note: I'm also using RestFB instead of the wicket-stuff package.  It made a
lot more sense to me and seemed to have better user support.  I'm sure
wicket-facebook would work just as well here.


public class LoggedOutPanel extends Panel {

private static final String apiKey = "my_api_key";
 private static final String apiSecret = "my_api_secret";
private static final Token EMPTY_TOKEN = null;
 private static final OAuthService service = new ServiceBuilder()
.provider(FacebookApi.class)
 .apiKey(apiKey)
.apiSecret(apiSecret)
 .callback("http://localhost:8080/project-1.0-SNAPSHOT/signin";)
 .build();
 public LoggedOutPanel (String id, PageParameters parameters) {
super(id);
 final ExternalLink fbLogin = new ExternalLink("fb-login",
service.getAuthorizationUrl(EMPTY_TOKEN));
 fbLogin.add(new Image("fb-login-img", new
ContextRelativeResource("/images/facebook_login.png")));
 add(fbLogin);
 }
}

public final class SignIn extends BasePage {
 private static final String apiKey = "my_api_key";
 private static final String apiSecret = "my_api_secret";
private static final Token EMPTY_TOKEN = null;
 private static final OAuthService service = new ServiceBuilder()
.provider(FacebookApi.class)
 .apiKey(apiKey)
.apiSecret(apiSecret)
 .callback("http://localhost:8080/project-1.0-SNAPSHOT/signin";)
 .build();
private String ACCESS_TOKEN;
 public SignIn(final PageParameters parameters) {
 if(parameters.isEmpty()) {
// TODO Try logging in again
 } else {
Verifier verifier = new Verifier(parameters.get("code").toString());
 Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
ACCESS_TOKEN = accessToken.getToken().toString();
 FacebookClient fb = new DefaultFacebookClient(ACCESS_TOKEN);
Person user = fb.fetchObject("me", Person.class, Parameter.with("fields",
"username"));
 System.out.println("User name: " + user.getUsername());
}
 }
}

_______________________________________
Stephen Walsh | http://connectwithawalsh.com


On Thu, Feb 21, 2013 at 12:27 AM, Stephen Walsh <
[email protected]> wrote:

> I got this figure out. I'll post my solution tomorrow when I have a few
> minutes.
>
> Basically, I wasn't understanding that the code was coming back in a page
> parameter. Once I understood that it was fairly easy to implement.
>
>
> On Monday, February 18, 2013, Stephen Walsh wrote:
>
>> That's where I'm headed right now.  I had a signin page with
>> PageParameters that picked it up by accident...  I think I'm headed in the
>> right direction now.
>>
>> I'll post my solution when I get it finished.  I'd still be interested to
>> see your solution also.
>>
>> Thanks again.
>>
>> _______________________________________
>> Stephen Walsh | http://connectwithawalsh.com
>>
>>
>> On Mon, Feb 18, 2013 at 6:29 PM, Michael Chandler <
>> [email protected]> wrote:
>>
>>> > The browser gets a token back that makes perfect sense and the example
>>> is completed.
>>> > How do I "consume" the token?  I'll play around with it a bit and let
>>> you know what
>>> > I come up with.  Thanks for the help.
>>>
>>> Based on the path I was taking, the redirect URI is the key.  Facebook
>>> redirects as such:
>>>
>>> YOUR_REDIRECT_URI?
>>>     access_token=USER_ACCESS_TOKEN
>>>    &expires_in=NUMBER_OF_SECONDS_UNTIL_TOKEN_EXPIRES
>>>    &state=YOUR_STATE_VALUE
>>>
>>> Of course, if the request fails authentication, they redirect as follows:
>>>
>>> YOUR_REDIRECT_URI?
>>>     error_reason=user_denied
>>>    &error=access_denied
>>>    &error_description=The+user+denied+your+request.
>>>    &state=YOUR_STATE_VALUE
>>>
>>> So your redirect page could start out like this:
>>>
>>> public class FacebookResponseListener extends WebPage {
>>>
>>>         private static final long serialVersionUID = 1L;
>>>
>>>         public FacebookResponseListener(PageParameters params) {
>>>                 // if there is an error, handle it
>>>                 if (params.get("error_reason") != null) {
>>>                         // handle the error here!
>>>                 } else {
>>>                         String accessToken =
>>> params.get("access_token").toString();
>>>                         int expiresIn = params.get("expires_in").toInt();
>>>
>>>                         // etc... etc...
>>>
>>>                 }
>>>
>>>         }
>>> }
>>>
>>> Mike
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [email protected]
>>> For additional commands, e-mail: [email protected]
>>>
>>>
>>
>
> --
> _______________________________________
> Stephen Walsh | http://connectwithawalsh.com
>
>

Reply via email to