forked from scribejava/scribejava
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a sample for Facebook App Login and Graph API (insights) call
Change-Id: Ie719bb118a98d4794d5da6c9aad2a281c7caf413
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/test/java/org/scribe/examples/FacebookAppLoginExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! :)"); | ||
|
||
} | ||
} |