Skip to content

Commit 0359c85

Browse files
author
Darko Zelic
committed
Added ViadeoApi and example
1 parent 479bfe6 commit 0359c85

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.extractors.AccessTokenExtractor;
4+
import org.scribe.model.OAuthConfig;
5+
import org.scribe.utils.OAuthEncoder;
6+
import org.scribe.utils.Preconditions;
7+
8+
import org.scribe.extractors.JsonTokenExtractor;
9+
10+
public class ViadeoApi extends DefaultApi20
11+
{
12+
private static final String AUTHORIZE_URL = "https://secure.viadeo.com/oauth-provider/authorize2?client_id=%s&redirect_uri=%s&response_type=code";
13+
private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL + "&scope=%s";
14+
15+
@Override
16+
public AccessTokenExtractor getAccessTokenExtractor()
17+
{
18+
return new JsonTokenExtractor();
19+
}
20+
21+
@Override
22+
public String getAccessTokenEndpoint()
23+
{
24+
return "https://secure.viadeo.com/oauth-provider/access_token2?grant_type=authorization_code";
25+
}
26+
27+
@Override
28+
public String getAuthorizationUrl(OAuthConfig config)
29+
{
30+
Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. Viadeo does not support OOB");
31+
32+
// Append scope if present
33+
if(config.hasScope())
34+
{
35+
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope()));
36+
}
37+
else
38+
{
39+
return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
40+
}
41+
}
42+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 ViadeoExample
11+
{
12+
private static final String NETWORK_NAME = "Viadeo";
13+
private static final String PROTECTED_RESOURCE_URL = "https://api.viadeo.com/me?user_detail=full";
14+
private static final Token EMPTY_TOKEN = null;
15+
16+
public static void main(String[] args)
17+
{
18+
// Replace these with your own api key and secret
19+
String apiKey = "your_app_id";
20+
String apiSecret = "your_api_secret";
21+
OAuthService service = new ServiceBuilder()
22+
.provider(ViadeoApi.class)
23+
.apiKey(apiKey)
24+
.apiSecret(apiSecret)
25+
.callback("http://www.example.com/oauth_callback/")
26+
.build();
27+
Scanner in = new Scanner(System.in);
28+
29+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
30+
System.out.println();
31+
32+
// Obtain the Authorization URL
33+
System.out.println("Fetching the Authorization URL...");
34+
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
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(EMPTY_TOKEN, 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.getCode());
58+
System.out.println(response.getBody());
59+
60+
System.out.println();
61+
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
62+
63+
}
64+
}

0 commit comments

Comments
 (0)