Skip to content

Commit 42ef871

Browse files
committed
Added ImgUr support
1 parent 74f53a4 commit 42ef871

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.model.Token;
4+
5+
/**
6+
* OAuth API for ImgUr
7+
*
8+
* @author David Wursteisen
9+
* @see <a href="http://api.imgur.com/#authapi">ImgUr API</a>
10+
*/
11+
public class ImgUrApi extends DefaultApi10a {
12+
13+
@Override
14+
public String getRequestTokenEndpoint()
15+
{
16+
return "https://api.imgur.com/oauth/request_token";
17+
}
18+
19+
@Override
20+
public String getAccessTokenEndpoint()
21+
{
22+
return "https://api.imgur.com/oauth/access_token";
23+
}
24+
25+
@Override
26+
public String getAuthorizationUrl(Token requestToken)
27+
{
28+
return String.format("https://api.imgur.com/oauth/authorize?oauth_token=%s", requestToken.getToken());
29+
}
30+
}
31+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.scribe.examples;
2+
3+
import org.scribe.builder.ServiceBuilder;
4+
import org.scribe.builder.api.ImgUrApi;
5+
import org.scribe.model.OAuthRequest;
6+
import org.scribe.model.Response;
7+
import org.scribe.model.Token;
8+
import org.scribe.model.Verb;
9+
import org.scribe.model.Verifier;
10+
import org.scribe.oauth.OAuthService;
11+
12+
import java.util.Scanner;
13+
14+
public class ImgUrExample
15+
{
16+
private static final String PROTECTED_RESOURCE_URL = "http://api.imgur.com/2/account.json";
17+
18+
public static void main(String[] args)
19+
{
20+
// Replace these with your own api key and secret (you'll need an read/write api key)
21+
String apiKey = "your_app_id";
22+
String apiSecret = "your_api_secret";
23+
OAuthService service = new ServiceBuilder().provider(ImgUrApi.class).apiKey(apiKey).apiSecret(apiSecret).build();
24+
Scanner in = new Scanner(System.in);
25+
26+
System.out.println("=== ImgUr's OAuth Workflow ===");
27+
System.out.println();
28+
29+
// Obtain the Request Token
30+
System.out.println("Fetching the Request Token...");
31+
Token requestToken = service.getRequestToken();
32+
System.out.println("Got the Request Token!");
33+
System.out.println();
34+
35+
System.out.println("Now go and authorize Scribe here:");
36+
String authorizationUrl = service.getAuthorizationUrl(requestToken);
37+
System.out.println(authorizationUrl);
38+
System.out.println("And paste the verifier 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.GET, PROTECTED_RESOURCE_URL);
53+
service.signRequest(accessToken, request);
54+
Response response = request.send();
55+
System.out.println("Got it! Lets see what we found...");
56+
System.out.println();
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+
}

0 commit comments

Comments
 (0)