Visitar URL original
Could you please consider pulling my changes for OAuth 2.0 Client Credential support ? by hidekoji · Pull Request #116 · scribejava/scribejava · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added a sample for Facebook App Login and Graph API (insights) call
Change-Id: Ie719bb118a98d4794d5da6c9aad2a281c7caf413
  • Loading branch information
hidekoji committed May 26, 2011
commit fb0a1196fa75e5e1519b886bbde81f9dbf723c07
Empty file modified bundle
100755 → 100644
Empty file.
71 changes: 71 additions & 0 deletions src/test/java/org/scribe/examples/FacebookAppLoginExample.java
Original file line number Diff line number Diff line change
@@ -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! :)");

}
}