Skip to content

Commit b35ea8d

Browse files
Merge pull request scribejava#264 from niQo/master
Support for Skyrock.com API
2 parents 294c311 + 5ad8059 commit b35ea8d

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.model.*;
4+
5+
/**
6+
* OAuth API for Skyrock.
7+
*
8+
* @author Nicolas Quiénot
9+
* @see <a href="http://www.skyrock.com/developer/">Skyrock.com API</a>
10+
*/
11+
public class SkyrockApi extends DefaultApi10a
12+
{
13+
private static final String API_ENDPOINT = "https://api.skyrock.com/v2";
14+
private static final String REQUEST_TOKEN_RESOURCE = "/oauth/initiate";
15+
private static final String AUTHORIZE_URL = "/oauth/authorize?oauth_token=%s";
16+
private static final String ACCESS_TOKEN_RESOURCE = "/oauth/token";
17+
18+
@Override
19+
public String getAccessTokenEndpoint()
20+
{
21+
return API_ENDPOINT + ACCESS_TOKEN_RESOURCE;
22+
}
23+
24+
@Override
25+
public String getRequestTokenEndpoint()
26+
{
27+
return API_ENDPOINT + REQUEST_TOKEN_RESOURCE;
28+
}
29+
30+
@Override
31+
public String getAuthorizationUrl(Token requestToken)
32+
{
33+
return String.format(API_ENDPOINT + AUTHORIZE_URL, requestToken.getToken());
34+
}
35+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.scribe.examples;
2+
3+
import java.util.Scanner;
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 SkyrockExample
11+
{
12+
private static final String PROTECTED_RESOURCE_URL = "https://api.skyrock.com/v2/user/get.json";
13+
14+
public static void main(String[] args)
15+
{
16+
OAuthService service = new ServiceBuilder()
17+
.provider(SkyrockApi.class)
18+
.apiKey("your-api-key")
19+
.apiSecret("your-api-secret")
20+
.build();
21+
Scanner in = new Scanner(System.in);
22+
23+
System.out.println("=== Skyrock'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+
System.out.println("Now go and authorize Scribe here:");
33+
System.out.println(service.getAuthorizationUrl(requestToken));
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.getCode());
54+
System.out.println(response.getBody());
55+
56+
System.out.println();
57+
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
58+
59+
}
60+
61+
}

0 commit comments

Comments
 (0)