Skip to content

Commit 69fff5b

Browse files
czzhifernandezpablo85
authored andcommitted
Added Kaixin2, Renren and SinaWeibo2 Apis and examples.
1 parent ceae6f8 commit 69fff5b

File tree

7 files changed

+362
-0
lines changed

7 files changed

+362
-0
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,4 @@
7979
[1.3.1]
8080
* FEATURE: Meetup.com Api
8181
* FEATURE: NetProspex Api
82+
* FEATURE: Kaixin2, SinaWeibo2 and Renren Apis
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.extractors.*;
4+
import org.scribe.model.*;
5+
import org.scribe.utils.*;
6+
7+
/**
8+
* Kaixin(http://www.kaixin001.com/) open platform api based on OAuth 2.0.
9+
*/
10+
public class KaixinApi20 extends DefaultApi20
11+
{
12+
13+
private static final String AUTHORIZE_URL = "http://api.kaixin001.com/oauth2/authorize?client_id=%s&redirect_uri=%s&response_type=code";
14+
private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL + "&scope=%s";
15+
16+
@Override
17+
public AccessTokenExtractor getAccessTokenExtractor()
18+
{
19+
return new JsonTokenExtractor();
20+
}
21+
22+
@Override
23+
public String getAccessTokenEndpoint()
24+
{
25+
return "https://api.kaixin001.com/oauth2/access_token?grant_type=authorization_code";
26+
}
27+
28+
@Override
29+
public String getAuthorizationUrl(OAuthConfig config)
30+
{
31+
// Append scope if present
32+
if (config.hasScope())
33+
{
34+
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope()));
35+
}
36+
else
37+
{
38+
return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
39+
}
40+
}
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.extractors.*;
4+
import org.scribe.model.*;
5+
import org.scribe.utils.*;
6+
7+
/**
8+
* Renren(http://www.renren.com/) OAuth 2.0 based api.
9+
*/
10+
public class RenrenApi extends DefaultApi20
11+
{
12+
private static final String AUTHORIZE_URL = "https://graph.renren.com/oauth/authorize?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://graph.renren.com/oauth/token?grant_type=authorization_code";
25+
}
26+
27+
@Override
28+
public String getAuthorizationUrl(OAuthConfig config)
29+
{
30+
// Append scope if present
31+
if (config.hasScope())
32+
{
33+
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope()));
34+
}
35+
else
36+
{
37+
return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
38+
}
39+
}
40+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.extractors.*;
4+
import org.scribe.model.*;
5+
import org.scribe.utils.*;
6+
7+
/**
8+
* SinaWeibo OAuth 2.0 api.
9+
*/
10+
public class SinaWeiboApi20 extends DefaultApi20
11+
{
12+
private static final String AUTHORIZE_URL = "https://api.weibo.com/oauth2/authorize?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 Verb getAccessTokenVerb()
17+
{
18+
return Verb.POST;
19+
}
20+
21+
@Override
22+
public AccessTokenExtractor getAccessTokenExtractor()
23+
{
24+
return new JsonTokenExtractor();
25+
}
26+
27+
@Override
28+
public String getAccessTokenEndpoint()
29+
{
30+
return "https://api.weibo.com/oauth2/access_token?grant_type=authorization_code";
31+
}
32+
33+
@Override
34+
public String getAuthorizationUrl(OAuthConfig config)
35+
{
36+
// Append scope if present
37+
if (config.hasScope())
38+
{
39+
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope()));
40+
}
41+
else
42+
{
43+
return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
44+
}
45+
}
46+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
import java.util.*;
8+
9+
public class Kaixin20Example
10+
{
11+
private static final String NETWORK_NAME = "Kaixin";
12+
private static final String PROTECTED_RESOURCE_URL = "https://api.kaixin001.com/users/me.json";
13+
private static final Token EMPTY_TOKEN = null;
14+
15+
public static void main(String[] args)
16+
{
17+
// Replace these with your own api key and secret
18+
String apiKey = "your api key";
19+
String apiSecret = "your api secret";
20+
OAuthService service = new ServiceBuilder()
21+
.provider(KaixinApi20.class)
22+
.apiKey(apiKey)
23+
.apiSecret(apiSecret)
24+
.callback("http://your.domain.com/handle")
25+
.build();
26+
Scanner in = new Scanner(System.in);
27+
28+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
29+
System.out.println();
30+
31+
// Obtain the Authorization URL
32+
System.out.println("Fetching the Authorization URL...");
33+
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
34+
System.out.println("Got the Authorization URL!");
35+
System.out.println("Now go and authorize Scribe here:");
36+
System.out.println(authorizationUrl);
37+
System.out.println("And paste the authorization code here");
38+
System.out.print(">>");
39+
Verifier verifier = new Verifier(in.nextLine());
40+
System.out.println();
41+
42+
// Trade the Request Token and Verifier for the Access Token
43+
System.out.println("Trading the Request Token for an Access Token...");
44+
Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
45+
System.out.println("Got the Access Token!");
46+
System.out.println("(if your curious it looks like this: " + accessToken + " )");
47+
System.out.println();
48+
49+
// Now let's go and ask for a protected resource!
50+
System.out.println("Now we're going to access a protected resource...");
51+
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
52+
service.signRequest(accessToken, request);
53+
Response response = request.send();
54+
System.out.println("Got it! Lets see what we found...");
55+
System.out.println();
56+
System.out.println(response.getCode());
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+
}
63+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
import java.nio.charset.*;
8+
import java.security.*;
9+
import java.util.*;
10+
11+
public class RenrenExample
12+
{
13+
private static final String NETWORK_NAME = "Renren";
14+
private static final String PROTECTED_RESOURCE_URL = "http://api.renren.com/restserver.do";
15+
private static final Token EMPTY_TOKEN = null;
16+
17+
public static void main(String[] args)
18+
{
19+
// Replace these with your own api key and secret
20+
String apiKey = "your api key";
21+
String apiSecret = "your api secret";
22+
OAuthService service = new ServiceBuilder()
23+
.provider(RenrenApi.class)
24+
.apiKey(apiKey)
25+
.apiSecret(apiSecret)
26+
.scope("status_update publish_feed")
27+
.callback("http://your.doman.com/oauth/renren")
28+
.build();
29+
Scanner in = new Scanner(System.in);
30+
31+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
32+
System.out.println();
33+
34+
// Obtain the Authorization URL
35+
System.out.println("Fetching the Authorization URL...");
36+
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
37+
System.out.println("Got the Authorization URL!");
38+
System.out.println("Now go and authorize Scribe here:");
39+
System.out.println(authorizationUrl);
40+
System.out.println("And paste the authorization code here");
41+
System.out.print(">>");
42+
Verifier verifier = new Verifier(in.nextLine());
43+
System.out.println();
44+
45+
// Trade the Request Token and Verfier for the Access Token
46+
System.out.println("Trading the Request Token for an Access Token...");
47+
Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
48+
System.out.println("Got the Access Token!");
49+
System.out.println("(if your curious it looks like this: " + accessToken + " )");
50+
System.out.println();
51+
52+
// Now let's go and ask for a protected resource!
53+
System.out.println("Now we're going to access a protected resource...");
54+
OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL);
55+
Map<String, String> parameters = new HashMap<String, String>();
56+
parameters.put("method", "users.getInfo");
57+
parameters.put("format", "json");
58+
parameters.put("v", "1.0");
59+
60+
List<String> sigString = new ArrayList<String>(parameters.size() + 1);
61+
for (Map.Entry<String, String> entry : parameters.entrySet())
62+
{
63+
request.addQuerystringParameter(entry.getKey(), entry.getValue());
64+
sigString.add(String.format("%s=%s", entry.getKey(), entry.getValue()));
65+
}
66+
sigString.add(String.format("%s=%s", OAuthConstants.ACCESS_TOKEN, accessToken.getToken()));
67+
Collections.sort(sigString);
68+
StringBuilder b = new StringBuilder();
69+
for (String param : sigString)
70+
{
71+
b.append(param);
72+
}
73+
b.append(apiSecret);
74+
System.out.println("Sig string: " + b.toString());
75+
request.addQuerystringParameter("sig", md5(b.toString()));
76+
service.signRequest(accessToken, request);
77+
Response response = request.send();
78+
System.out.println("Got it! Lets see what we found...");
79+
System.out.println();
80+
System.out.println(response.getCode());
81+
System.out.println(response.getBody());
82+
83+
System.out.println();
84+
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
85+
86+
}
87+
88+
public static String md5(String orgString)
89+
{
90+
try
91+
{
92+
java.security.MessageDigest md = MessageDigest.getInstance("MD5");
93+
byte[] array = md.digest(orgString.getBytes(Charset.forName("UTF-8")));
94+
StringBuffer sb = new StringBuffer();
95+
for (int i = 0; i < array.length; ++i)
96+
{
97+
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
98+
}
99+
return sb.toString();
100+
}
101+
catch (NoSuchAlgorithmException e)
102+
{
103+
e.printStackTrace();
104+
}
105+
return null;
106+
}
107+
108+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
import java.util.*;
8+
9+
public class SinaWeibo2Example
10+
{
11+
private static final String NETWORK_NAME = "SinaWeibo";
12+
private static final String PROTECTED_RESOURCE_URL = "https://api.weibo.com/2/account/get_uid.json";
13+
private static final Token EMPTY_TOKEN = null;
14+
15+
public static void main(String[] args)
16+
{
17+
// Replace these with your own api key and secret
18+
String apiKey = "342348223";
19+
String apiSecret = "cfdf672e166a4bc954c0e33f03cf0d1b";
20+
OAuthService service = new ServiceBuilder()
21+
.provider(SinaWeiboApi20.class)
22+
.apiKey(apiKey)
23+
.apiSecret(apiSecret)
24+
.callback("http://www.dajie.com/oauth/sina")
25+
.build();
26+
Scanner in = new Scanner(System.in);
27+
28+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
29+
System.out.println();
30+
31+
// Obtain the Authorization URL
32+
System.out.println("Fetching the Authorization URL...");
33+
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
34+
System.out.println("Got the Authorization URL!");
35+
System.out.println("Now go and authorize Scribe here:");
36+
System.out.println(authorizationUrl);
37+
System.out.println("And paste the authorization code here");
38+
System.out.print(">>");
39+
Verifier verifier = new Verifier(in.nextLine());
40+
System.out.println();
41+
42+
// Trade the Request Token and Verifier for the Access Token
43+
System.out.println("Trading the Request Token for an Access Token...");
44+
Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
45+
System.out.println("Got the Access Token!");
46+
System.out.println("(if your curious it looks like this: " + accessToken + " )");
47+
System.out.println();
48+
49+
// Now let's go and ask for a protected resource!
50+
System.out.println("Now we're going to access a protected resource...");
51+
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
52+
service.signRequest(accessToken, request);
53+
Response response = request.send();
54+
System.out.println("Got it! Lets see what we found...");
55+
System.out.println();
56+
System.out.println(response.getCode());
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+
}
63+
}

0 commit comments

Comments
 (0)