Skip to content

Commit 965a1ec

Browse files
Merge pull request scribejava#248 from adamnengland/master
Digg API added
2 parents ebe4b0e + 13d1eae commit 965a1ec

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.model.*;
4+
5+
public class DiggApi extends DefaultApi10a
6+
{
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+
{
14+
return BASE_URL + "request_token";
15+
}
16+
17+
@Override
18+
public String getAccessTokenEndpoint()
19+
{
20+
return BASE_URL + "access_token";
21+
}
22+
23+
@Override
24+
public String getAuthorizationUrl(Token requestToken)
25+
{
26+
return String.format(AUTHORIZATION_URL, requestToken.getToken());
27+
}
28+
29+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ public Token extract(String response)
2525
}
2626
}
2727

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

0 commit comments

Comments
 (0)