Skip to content

Commit 7b45840

Browse files
Merge pull request scribejava#227 from dwursteisen/master
Added ImgUr Api support
2 parents 69fff5b + 0967770 commit 7b45840

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.model.*;
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+
14+
@Override
15+
public String getRequestTokenEndpoint()
16+
{
17+
return "https://api.imgur.com/oauth/request_token";
18+
}
19+
20+
@Override
21+
public String getAccessTokenEndpoint()
22+
{
23+
return "https://api.imgur.com/oauth/access_token";
24+
}
25+
26+
@Override
27+
public String getAuthorizationUrl(Token requestToken)
28+
{
29+
return String.format("https://api.imgur.com/oauth/authorize?oauth_token=%s", requestToken.getToken());
30+
}
31+
}
32+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.scribe.examples;
2+
3+
import org.scribe.builder.*;
4+
import org.scribe.builder.api.*;
5+
import org.scribe.model.*;
6+
import org.scribe.oauth.*;
7+
8+
import java.util.*;
9+
10+
public class ImgUrExample
11+
{
12+
private static final String PROTECTED_RESOURCE_URL = "http://api.imgur.com/2/account.json";
13+
14+
public static void main(String[] args)
15+
{
16+
// Replace these with your own api key and secret (you'll need an read/write api key)
17+
String apiKey = "your_app_id";
18+
String apiSecret = "your_api_secret";
19+
OAuthService service = new ServiceBuilder().provider(ImgUrApi.class).apiKey(apiKey).apiSecret(apiSecret).build();
20+
Scanner in = new Scanner(System.in);
21+
22+
System.out.println("=== ImgUr's OAuth Workflow ===");
23+
System.out.println();
24+
25+
// Obtain the Request Token
26+
System.out.println("Fetching the Request Token...");
27+
Token requestToken = service.getRequestToken();
28+
System.out.println("Got the Request Token!");
29+
System.out.println();
30+
31+
System.out.println("Now go and authorize Scribe here:");
32+
String authorizationUrl = service.getAuthorizationUrl(requestToken);
33+
System.out.println(authorizationUrl);
34+
System.out.println("And paste the verifier here");
35+
System.out.print(">>");
36+
Verifier verifier = new Verifier(in.nextLine());
37+
System.out.println();
38+
39+
// Trade the Request Token and Verfier for the Access Token
40+
System.out.println("Trading the Request Token for an Access Token...");
41+
Token accessToken = service.getAccessToken(requestToken, verifier);
42+
System.out.println("Got the Access Token!");
43+
System.out.println("(if your curious it looks like this: " + accessToken + " )");
44+
System.out.println();
45+
46+
// Now let's go and ask for a protected resource!
47+
System.out.println("Now we're going to access a protected resource...");
48+
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
49+
service.signRequest(accessToken, request);
50+
Response response = request.send();
51+
System.out.println("Got it! Lets see what we found...");
52+
System.out.println();
53+
System.out.println(response.getBody());
54+
55+
System.out.println();
56+
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
57+
}
58+
}

0 commit comments

Comments
 (0)