From 1f36fd0f32ca44e37d85cb226ee0da5a789f633b Mon Sep 17 00:00:00 2001 From: Pablo Fernandez Date: Sun, 9 Jan 2011 15:52:10 -0300 Subject: [PATCH] Added Facebook Api and example --- changelog.txt | 6 ++ .../org/scribe/builder/api/FacebookApi.java | 16 ++++++ .../org/scribe/examples/FacebookExample.java | 55 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/main/java/org/scribe/builder/api/FacebookApi.java create mode 100644 src/test/java/org/scribe/examples/FacebookExample.java diff --git a/changelog.txt b/changelog.txt index 11cf828..2ddc512 100644 --- a/changelog.txt +++ b/changelog.txt @@ -18,3 +18,9 @@ * FEATURE: Token made Serializable * FIX: Google Api encoding issues (thanks @roger and @klakegg) + +[1.1.0] + + * FEATURE: OAuth 2.0 Support and Facebook support out of the box (thanks Diego Silveira) + * REFACTOR: Api creation and OAuthService refactors + * REFACTOR: Connections are created lazily (just before sending the request) \ No newline at end of file diff --git a/src/main/java/org/scribe/builder/api/FacebookApi.java b/src/main/java/org/scribe/builder/api/FacebookApi.java new file mode 100644 index 0000000..896c71f --- /dev/null +++ b/src/main/java/org/scribe/builder/api/FacebookApi.java @@ -0,0 +1,16 @@ +package org.scribe.builder.api; + +import org.scribe.exceptions.*; +import org.scribe.model.*; + +public class FacebookApi extends DefaultApi20 +{ + private static final String AUTHORIZE_URL = "https://graph.facebook.com/oauth/authorize?response_type=token&client_id=%s&redirect_uri=%s"; + @Override + public String getAuthorizationUrl(OAuthConfig config) + { + if(OAuthConstants.OUT_OF_BAND.equals(config.getCallback())) + throw new OAuthException("Facebook does not support oob authentication."); + return String.format(AUTHORIZE_URL, config.getApiKey(), config.getCallback()); + } +} diff --git a/src/test/java/org/scribe/examples/FacebookExample.java b/src/test/java/org/scribe/examples/FacebookExample.java new file mode 100644 index 0000000..c83a0de --- /dev/null +++ b/src/test/java/org/scribe/examples/FacebookExample.java @@ -0,0 +1,55 @@ +package org.scribe.examples; + +import java.util.*; + +import org.scribe.builder.*; +import org.scribe.builder.api.*; +import org.scribe.model.*; +import org.scribe.oauth.*; + +public class FacebookExample +{ + private static final String NETWORK_NAME = "Facebook"; + private static final String PROTECTED_RESOURCE_URL = "https://graph.facebook.com/me"; + private static final String NO_SECRET_NEEDED = ""; + private static final Token EMPTY_TOKEN = null; + + public static void main(String[] args) + { + OAuthService service = new ServiceBuilder() + .provider(FacebookApi.class) + .apiKey("anonymous") + .apiSecret("anonymous") + .callback("http://www.example.com/oauth_callback/") + .build(); + Scanner in = new Scanner(System.in); + + System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ==="); + System.out.println(); + + // Obtain the Authorization URL + System.out.println("Fetching the Authorization URL..."); + String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN); + System.out.println("Got the Authorization URL!"); + System.out.println("Now go and authorize Scribe here:"); + System.out.println(authorizationUrl); + System.out.println("And paste the access token here"); + System.out.print(">>"); + Token accessToken = new Token(in.nextLine(), NO_SECRET_NEEDED); + System.out.println(); + + // Now let's go and ask for a protected resource! + System.out.println("Now we're going to access a protected resource..."); + OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); + service.signRequest(accessToken, request); + Response response = request.send(); + System.out.println("Got it! Lets see what we found..."); + System.out.println(); + System.out.println(response.getCode()); + System.out.println(response.getBody()); + + System.out.println(); + System.out.println("Thats it man! Go and build something awesome with Scribe! :)"); + + } +} \ No newline at end of file