Skip to content

Commit 203cf83

Browse files
Arthraimfernandezpablo85
authored andcommitted
Added Sohu Weibo api and example.
1 parent 7b587c3 commit 203cf83

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@
5454
[1.2.1]
5555
* FEATURE: Added custom charset support to Request (thanks Eric Genet)
5656
* FEATURE: Added support for Vkontakte (thanks dotbg)
57+
* FEATURE: Added Sohu Weibo, Netease Weibo & Sina Weibo Apis (thanks Arthur Wang)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.model.*;
4+
5+
public class SohuWeiboApi extends DefaultApi10a
6+
{
7+
private static final String REQUEST_TOKEN_URL = "http://api.t.sohu.com/oauth/request_token";
8+
private static final String ACCESS_TOKEN_URL = "http://api.t.sohu.com/oauth/access_token";
9+
private static final String AUTHORIZE_URL = "http://api.t.sohu.com/oauth/authorize?oauth_token=%s";
10+
11+
@Override
12+
public String getRequestTokenEndpoint()
13+
{
14+
return REQUEST_TOKEN_URL;
15+
}
16+
17+
@Override
18+
public String getAccessTokenEndpoint()
19+
{
20+
return ACCESS_TOKEN_URL;
21+
}
22+
23+
@Override
24+
public String getAuthorizationUrl(Token requestToken)
25+
{
26+
return String.format(AUTHORIZE_URL, requestToken.getToken());
27+
}
28+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 SohuWeiboExample
11+
{
12+
private static final String NETWORK_NAME = "SohuWeibo";
13+
private static final String PROTECTED_RESOURCE_URL = "http://api.t.sohu.com/account/verify_credentials.json";
14+
15+
public static void main(String[] args)
16+
{
17+
// Replace these with your own api key and secret
18+
String apiKey = "your_key";
19+
String apiSecret = "your_secret";
20+
OAuthService service = new ServiceBuilder()
21+
.provider(SohuWeiboApi.class)
22+
.apiKey(apiKey)
23+
.apiSecret(apiSecret)
24+
.build();
25+
Scanner in = new Scanner(System.in);
26+
27+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
28+
System.out.println();
29+
30+
// Grab a request token.
31+
System.out.println("Fetching request token.");
32+
Token requestToken = service.getRequestToken();
33+
System.out.println("Got it ... ");
34+
System.out.println(requestToken.getToken());
35+
36+
// Obtain the Authorization URL
37+
System.out.println("Fetching the Authorization URL...");
38+
String authorizationUrl = service.getAuthorizationUrl(requestToken);
39+
System.out.println("Got the Authorization URL!");
40+
System.out.println("Now go and authorize Scribe here:");
41+
System.out.println(authorizationUrl);
42+
System.out.println("And paste the authorization code here");
43+
System.out.print(">>");
44+
Verifier verifier = new Verifier(in.nextLine());
45+
System.out.println();
46+
47+
// Trade the Request Token and Verfier for the Access Token
48+
System.out.println("Trading the Request Token for an Access Token...");
49+
Token accessToken = service.getAccessToken(requestToken, verifier);
50+
System.out.println("Got the Access Token!");
51+
System.out.println("(if your curious it looks like this: "
52+
+ accessToken + " )");
53+
System.out.println();
54+
55+
// Now let's go and ask for a protected resource!
56+
System.out.println("Now we're going to access a protected resource...");
57+
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
58+
service.signRequest(accessToken, request);
59+
Response response = request.send();
60+
System.out.println("Got it! Lets see what we found...");
61+
System.out.println();
62+
System.out.println(response.getCode());
63+
System.out.println(response.getBody());
64+
65+
System.out.println();
66+
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
67+
}
68+
}

0 commit comments

Comments
 (0)