From 7b587c3d1df5fd5637e2610c8a89f9ae85cc8e9f Mon Sep 17 00:00:00 2001 From: Arthur Wang Date: Thu, 7 Apr 2011 14:00:33 +0800 Subject: [PATCH] Added Netease Weibo(t.163.com) api and example. --- .../scribe/builder/api/NeteaseWeibooApi.java | 47 +++++++++++++ .../scribe/examples/NeteaseWeiboExample.java | 68 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/main/java/org/scribe/builder/api/NeteaseWeibooApi.java create mode 100644 src/test/java/org/scribe/examples/NeteaseWeiboExample.java diff --git a/src/main/java/org/scribe/builder/api/NeteaseWeibooApi.java b/src/main/java/org/scribe/builder/api/NeteaseWeibooApi.java new file mode 100644 index 0000000..fae38ac --- /dev/null +++ b/src/main/java/org/scribe/builder/api/NeteaseWeibooApi.java @@ -0,0 +1,47 @@ +package org.scribe.builder.api; + +import org.scribe.model.*; + +public class NeteaseWeibooApi extends DefaultApi10a +{ + private static final String REQUEST_TOKEN_URL = "http://api.t.163.com/oauth/request_token"; + private static final String ACCESS_TOKEN_URL = "http://api.t.163.com/oauth/access_token"; + private static final String AUTHORIZE_URL = "http://api.t.163.com/oauth/authorize?oauth_token=%s"; + private static final String AUTHENTICATE_URL = "http://api.t.163.com/oauth/authenticate?oauth_token=%s"; + + @Override + public String getRequestTokenEndpoint() + { + return REQUEST_TOKEN_URL; + } + + @Override + public String getAccessTokenEndpoint() + { + return ACCESS_TOKEN_URL; + } + + @Override + /** + * this method will ignore your callback + * if you're creating a desktop client please choose this url + * else your can call getAuthenticateUrl + * + * via http://open.t.163.com/wiki/index.php?title=%E8%AF%B7%E6%B1%82%E7%94%A8%E6%88%B7%E6%8E%88%E6%9D%83Token(oauth/authorize) + */ + public String getAuthorizationUrl(Token requestToken) + { + return String.format(AUTHORIZE_URL, requestToken.getToken()); + } + + /** + * this method is for web client with callback url + * if you're creating a desktop client please call getAuthorizationUrl + * + * via http://open.t.163.com/wiki/index.php?title=%E8%AF%B7%E6%B1%82%E7%94%A8%E6%88%B7%E6%8E%88%E6%9D%83Token(oauth/authenticate) + */ + public String getAuthenticateUrl(Token requestToken) + { + return String.format(AUTHENTICATE_URL, requestToken.getToken()); + } +} diff --git a/src/test/java/org/scribe/examples/NeteaseWeiboExample.java b/src/test/java/org/scribe/examples/NeteaseWeiboExample.java new file mode 100644 index 0000000..e65335e --- /dev/null +++ b/src/test/java/org/scribe/examples/NeteaseWeiboExample.java @@ -0,0 +1,68 @@ +package org.scribe.examples; + +import java.util.*; + +import org.scribe.builder.*; +import org.scribe.builder.api.*; +import org.scribe.model.*; +import org.scribe.oauth.*; + +public class NeteaseWeiboExample +{ + private static final String NETWORK_NAME = "NetEase(163.com) Weibo"; + private static final String PROTECTED_RESOURCE_URL = "http://api.t.163.com/account/verify_credentials.json"; + + public static void main(String[] args) + { + // Replace these with your own api key and secret + String apiKey = "your key"; + String apiSecret = "your secret"; + OAuthService service = new ServiceBuilder() + .provider(NeteaseWeibooApi.class) + .apiKey(apiKey) + .apiSecret(apiSecret) + .build(); + Scanner in = new Scanner(System.in); + + System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ==="); + System.out.println(); + + // Grab a request token. + System.out.println("Fetching request token."); + Token requestToken = service.getRequestToken(); + System.out.println("Got it ... "); + System.out.println(requestToken.getToken()); + + // Obtain the Authorization URL + System.out.println("Fetching the Authorization URL..."); + String authorizationUrl = service.getAuthorizationUrl(requestToken); + System.out.println("Got the Authorization URL!"); + System.out.println("Now go and authorize Scribe here:"); + System.out.println(authorizationUrl); + System.out.println("And paste the authorization code here"); + System.out.print(">>"); + Verifier verifier = new Verifier(in.nextLine()); + System.out.println(); + + // Trade the Request Token and Verfier for the Access Token + System.out.println("Trading the Request Token for an Access Token..."); + Token accessToken = service.getAccessToken(requestToken, verifier); + System.out.println("Got the Access Token!"); + System.out.println("(if your curious it looks like this: " + + accessToken + " )"); + System.out.println(); + + // Now let's go and ask for a protected resource! + System.out.println("Now we're going to access a protected resource..."); + OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); + service.signRequest(accessToken, request); + Response response = request.send(); + System.out.println("Got it! Lets see what we found..."); + System.out.println(); + System.out.println(response.getCode()); + System.out.println(response.getBody()); + + System.out.println(); + System.out.println("Thats it man! Go and build something awesome with Scribe! :)"); + } +}