Skip to content

Commit

Permalink
Added a sample for Facebook App Login and Graph API (insights) call
Browse files Browse the repository at this point in the history
Change-Id: Ie719bb118a98d4794d5da6c9aad2a281c7caf413
  • Loading branch information
hidekoji committed May 26, 2011
1 parent 32d3e06 commit fb0a119
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Empty file modified bundle 100755 → 100644
Empty file.
71 changes: 71 additions & 0 deletions src/test/java/org/scribe/examples/FacebookAppLoginExample.java
@@ -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.