Permalink
Browse files

Added a sample for Facebook App Login and Graph API (insights) call

Change-Id: Ie719bb118a98d4794d5da6c9aad2a281c7caf413
  • Loading branch information...
1 parent 32d3e06 commit fb0a1196fa75e5e1519b886bbde81f9dbf723c07 @hidekoji hidekoji committed May 26, 2011
Showing with 71 additions and 0 deletions.
  1. 0 bundle
  2. +71 −0 src/test/java/org/scribe/examples/FacebookAppLoginExample.java
View
0 bundle 100755 → 100644
No changes.
@@ -0,0 +1,71 @@
+package org.scribe.examples;
+
+import java.util.Scanner;
+
+import org.scribe.builder.ServiceBuilder;
+import org.scribe.builder.api.FacebookApi;
+import org.scribe.model.OAuthRequest;
+import org.scribe.model.Response;
+import org.scribe.model.Token;
+import org.scribe.model.Verb;
+import org.scribe.model.Verifier;
+import org.scribe.oauth.OAuthService;
+
+public class FacebookAppLoginExample
+{
+ private static final String NETWORK_NAME = "Facebook";
+ private static final Token EMPTY_TOKEN = null;
+ private static final String PROTECTED_RESOURCE_URL ="https://graph.facebook.com/%s/insights";
+
+
+ public static void main(String[] args)
+ {
+ // Replace these with your own api key and secret
+ String apiKey = "your_app_id";
+ String apiSecret = "your_api_secret";
+ String callbackURL = "your_call_back";
+ OAuthService service = new ServiceBuilder()
+ .provider(FacebookApi.class)
+ .apiKey(apiKey)
+ .apiSecret(apiSecret)
+ .callback(callbackURL)
+ .grantType(FacebookApi.GRANT_TYPE_CLIENT_CREDENTIALS)
+ .build();
+ Scanner in = new Scanner(System.in);
+
+ System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
+ System.out.println();
+
+ // Obtain the Authorization URL for App Login
+ 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 authorization code (see url and find &code=...) here");
+ System.out.print(">>");
+ Verifier verifier = new Verifier(in.nextLine());
+ System.out.println();
+
+ // Trade the Request Token and Verfier for the Access Token
+ System.out.println("Trading the Request Token for an Access Token...");
+ Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
+ System.out.println("Got the Access Token!");
+ System.out.println("(if your curious it looks like this: " + accessToken + " )");
+ 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, String.format(PROTECTED_RESOURCE_URL,apiKey));
+ 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! :)");
+
+ }
+}

0 comments on commit fb0a119

Please sign in to comment.