Skip to content

Commit 488fc14

Browse files
committed
Added Digg Support
Refactoring
1 parent 1208fd8 commit 488fc14

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
package org.scribe.builder.api;
3+
4+
import org.scribe.model.Token;
5+
6+
public class DiggApi extends DefaultApi10a {
7+
8+
private static final String AUTHORIZATION_URL = "http://digg.com/oauth/authorize?oauth_token=%s";
9+
private static final String BASE_URL = "http://services.digg.com/oauth/";
10+
11+
@Override
12+
public String getRequestTokenEndpoint() {
13+
return BASE_URL + "request_token";
14+
}
15+
16+
@Override
17+
public String getAccessTokenEndpoint() {
18+
return BASE_URL + "access_token";
19+
}
20+
21+
@Override
22+
public String getAuthorizationUrl(Token requestToken) {
23+
return String.format(AUTHORIZATION_URL, requestToken.getToken());
24+
}
25+
26+
}

src/main/java/org/scribe/extractors/JsonTokenExtractor.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import java.util.regex.*;
44

5-
import org.scribe.exceptions.*;
6-
import org.scribe.model.*;
7-
import org.scribe.utils.*;
5+
import org.scribe.exceptions.OAuthException;
6+
import org.scribe.model.Token;
7+
import org.scribe.utils.Preconditions;
88

99
public class JsonTokenExtractor implements AccessTokenExtractor
1010
{
1111
private Pattern accessTokenPattern = Pattern.compile("\"access_token\":\\s*\"(\\S*?)\"");
1212

13-
@Override
1413
public Token extract(String response)
1514
{
1615
Preconditions.checkEmptyString(response, "Cannot extract a token from a null or empty String");
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.scribe.examples;
2+
3+
import java.util.Scanner;
4+
5+
import org.scribe.builder.ServiceBuilder;
6+
import org.scribe.builder.api.DiggApi;
7+
import org.scribe.model.*;
8+
import org.scribe.oauth.OAuthService;
9+
10+
public class DiggExample {
11+
private static final String NETWORK_NAME = "Digg";
12+
private static final String PROTECTED_RESOURCE_URL = "http://services.digg.com/2.0/comment.digg";
13+
14+
public static void main(String[] args) {
15+
// Replace these with your own api key and secret
16+
String apiKey = "myKey";
17+
String apiSecret = "mySecret";
18+
OAuthService service = new ServiceBuilder().provider(DiggApi.class).apiKey(apiKey).apiSecret(apiSecret).build();
19+
Scanner in = new Scanner(System.in);
20+
21+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
22+
System.out.println();
23+
24+
// Obtain the Request Token
25+
System.out.println("Fetching the Request Token...");
26+
Token requestToken = service.getRequestToken();
27+
System.out.println("Got the Request Token!");
28+
System.out.println();
29+
30+
// Obtain the Authorization URL
31+
System.out.println("Fetching the Authorization URL...");
32+
String authorizationUrl = service.getAuthorizationUrl(requestToken);
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 authorization code here");
37+
System.out.print(">>");
38+
Verifier verifier = new Verifier(in.nextLine());
39+
System.out.println();
40+
41+
// Trade the Request Token and Verfier for the Access Token
42+
System.out.println("Trading the Request Token for an Access Token...");
43+
Token accessToken = service.getAccessToken(requestToken, verifier);
44+
System.out.println("Got the Access Token!");
45+
System.out.println("(if your curious it looks like this: " + accessToken + " )");
46+
System.out.println();
47+
48+
// Now let's go and ask for a protected resource!
49+
System.out.println("Now we're going to access a protected resource...");
50+
OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL);
51+
request.addBodyParameter("comment_id", "20100729223726:4fef610331ee46a3b5cbd740bf71313e");
52+
service.signRequest(accessToken, request);
53+
Response response = request.send();
54+
System.out.println("Got it! Lets see what we found...");
55+
System.out.println();
56+
System.out.println(response.getCode());
57+
System.out.println(response.getBody());
58+
59+
System.out.println();
60+
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
61+
62+
}
63+
}

0 commit comments

Comments
 (0)