Skip to content

Commit 1f36fd0

Browse files
Added Facebook Api and example
1 parent 9ea686a commit 1f36fd0

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

changelog.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@
1818

1919
* FEATURE: Token made Serializable
2020
* FIX: Google Api encoding issues (thanks @roger and @klakegg)
21+
22+
[1.1.0]
23+
24+
* FEATURE: OAuth 2.0 Support and Facebook support out of the box (thanks Diego Silveira)
25+
* REFACTOR: Api creation and OAuthService refactors
26+
* REFACTOR: Connections are created lazily (just before sending the request)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.exceptions.*;
4+
import org.scribe.model.*;
5+
6+
public class FacebookApi extends DefaultApi20
7+
{
8+
private static final String AUTHORIZE_URL = "https://graph.facebook.com/oauth/authorize?response_type=token&client_id=%s&redirect_uri=%s";
9+
@Override
10+
public String getAuthorizationUrl(OAuthConfig config)
11+
{
12+
if(OAuthConstants.OUT_OF_BAND.equals(config.getCallback()))
13+
throw new OAuthException("Facebook does not support oob authentication.");
14+
return String.format(AUTHORIZE_URL, config.getApiKey(), config.getCallback());
15+
}
16+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.scribe.examples;
2+
3+
import java.util.*;
4+
5+
import org.scribe.builder.*;
6+
import org.scribe.builder.api.*;
7+
import org.scribe.model.*;
8+
import org.scribe.oauth.*;
9+
10+
public class FacebookExample
11+
{
12+
private static final String NETWORK_NAME = "Facebook";
13+
private static final String PROTECTED_RESOURCE_URL = "https://graph.facebook.com/me";
14+
private static final String NO_SECRET_NEEDED = "";
15+
private static final Token EMPTY_TOKEN = null;
16+
17+
public static void main(String[] args)
18+
{
19+
OAuthService service = new ServiceBuilder()
20+
.provider(FacebookApi.class)
21+
.apiKey("anonymous")
22+
.apiSecret("anonymous")
23+
.callback("http://www.example.com/oauth_callback/")
24+
.build();
25+
Scanner in = new Scanner(System.in);
26+
27+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
28+
System.out.println();
29+
30+
// Obtain the Authorization URL
31+
System.out.println("Fetching the Authorization URL...");
32+
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
33+
System.out.println("Got the Authorization URL!");
34+
System.out.println("Now go and authorize Scribe here:");
35+
System.out.println(authorizationUrl);
36+
System.out.println("And paste the access token here");
37+
System.out.print(">>");
38+
Token accessToken = new Token(in.nextLine(), NO_SECRET_NEEDED);
39+
System.out.println();
40+
41+
// Now let's go and ask for a protected resource!
42+
System.out.println("Now we're going to access a protected resource...");
43+
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
44+
service.signRequest(accessToken, request);
45+
Response response = request.send();
46+
System.out.println("Got it! Lets see what we found...");
47+
System.out.println();
48+
System.out.println(response.getCode());
49+
System.out.println(response.getBody());
50+
51+
System.out.println();
52+
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
53+
54+
}
55+
}

0 commit comments

Comments
 (0)