25 Commits
Author SHA1 Message Date
Pablo Fernandez 1932ad28c1 moved map functions to MapUtils 2011-08-15 23:40:15 -03:00
Pablo Fernandez 7f89dc3546 documented changes so far 2011-08-15 23:35:15 -03:00
Pablo Fernandez f3c0a179f3 Merge branch 'master' of github.com:fernandezpablo85/scribe-java 2011-08-15 23:32:55 -03:00
Pablo Fernandez d8e384f9c7 Merge pull request #137 from wscoble/live-example-eclipse-fix
fixed "invalid character constant" message from eclipse
2011-08-08 12:44:45 -07:00
Scott Scoble 35ca23f442 fixed "invalid character constant" message from eclipse 2011-08-08 00:28:23 -07:00
Pablo Fernandez 93602c4120 Merge pull request #135 from tomaslin/master
Windows Live oAuth Support
2011-08-02 09:04:26 -07:00
Tomas Lin 62b43855c2 wrapped line format 2011-08-02 15:56:43 +01:00
Tomas Lin a62fa36da0 added windows live support and example 2011-08-02 13:05:54 +01:00
Pablo Fernandez 1d59fbd014 Merge pull request #130 from dreamcwli/master
Add Plurk API
2011-07-18 05:56:13 -07:00
Chia-Wei Li 79fa710e47 Fix code style 2011-07-17 14:01:31 +08:00
Chia-Wei Li 079ce6a2c2 Add Plurk API 2011-07-17 11:48:16 +08:00
Pablo Fernandez d800ece6a6 added intellij ignores 2011-07-14 17:59:24 -03:00
Pablo Fernandez c86d7174af Merge pull request #124 from thepizzle/master
fixed NPE when setting connection timeouts.
2011-06-29 19:19:31 -07:00
Pablo Fernandez 5f47bebced bounced version number for release 2011-06-27 12:18:58 -03:00
ThePizzle 10c56bda27 Fix indentation and remove commented code 2011-06-09 09:23:33 -04:00
ThePizzle 1be46776e5 Set timeout parameters in doSend() to avoid NPE 2011-06-08 17:15:53 -04:00
Pablo Fernandez cc5a0a0471 missing entry on changelog 2011-06-03 16:49:30 -03:00
Pablo Fernandez 0c3fc722b1 enable persistent http connections 2011-06-03 16:46:16 -03:00
Arthur Wang 203cf83dc0 Added Sohu Weibo api and example. 2011-06-03 10:56:37 -03:00
Arthur Wang 7b587c3d1d Added Netease Weibo(t.163.com) api and example. 2011-06-03 10:50:17 -03:00
Arthur Wang f89279c682 Added Sina Weibo Api and example. 2011-06-03 10:46:29 -03:00
Pablo Fernandez 7b6b9056d3 multipart request support 2011-06-02 09:31:39 -03:00
Boris ca7950004b Implemented Vkontakte.ru support 2011-05-25 18:40:57 -03:00
Pablo Fernandez 543b53475f changelog charset update 2011-05-07 13:18:52 -03:00
Pablo Fernandez de1ac1f467 request charset handling 2011-05-07 13:15:35 -03:00
20 changed files with 704 additions and 57 deletions

No files matched your search

+5
View File
@@ -5,6 +5,11 @@
.project .project
.settings .settings
# IntelliJ Idea settings
.idea
scribe.iml
# Binaries # Binaries
target target
+3 -1
View File
@@ -38,6 +38,8 @@ h3. Supports all major 1.0a and 2.0 OAuth APIs out-of-the-box
* Yammer * Yammer
* Windows Live
* and many more! check the "examples folder":http://github.com/fernandezpablo85/scribe-java/tree/master/src/test/java/org/scribe/examples * and many more! check the "examples folder":http://github.com/fernandezpablo85/scribe-java/tree/master/src/test/java/org/scribe/examples
h3. Small and modular h3. Small and modular
@@ -61,7 +63,7 @@ You can pull scribe from a maven repository, just add this to your __pom.xml__ f
@<dependency>@ @<dependency>@
@<groupId>org.scribe</groupId>@ @<groupId>org.scribe</groupId>@
@<artifactId>scribe</artifactId>@ @<artifactId>scribe</artifactId>@
@<version>1.2.0</version>@ @<version>1.2.1</version>@
@</dependency>@ @</dependency>@
h1. Getting started in less than 2 minutes h1. Getting started in less than 2 minutes
+10
View File
@@ -50,3 +50,13 @@
* FEATURE: Added scopes to Facebook 2.0 Api * FEATURE: Added scopes to Facebook 2.0 Api
* FEATURE: Added Plaintext signature for Yammer * FEATURE: Added Plaintext signature for Yammer
* FEATURE: Added Twitter SSL endpoints (use TwitterApi.SSL.class) * FEATURE: Added Twitter SSL endpoints (use TwitterApi.SSL.class)
[1.2.1]
* FEATURE: Added custom charset support to Request (thanks Eric Genet)
* FEATURE: Added support for Vkontakte (thanks dotbg)
* FEATURE: Added Sohu Weibo, Netease Weibo & Sina Weibo Apis (thanks Arthur Wang)
* FEATURE: Added support for persistent Http Connections (thanks Craig Minton)
[1.2.2]
* FIX: Fixed error while setting connection timeouts
* FEATURE: Plurk Api
* FEATURE: Windows Live Api
+1 -1
View File
@@ -4,7 +4,7 @@
<groupId>org.scribe</groupId> <groupId>org.scribe</groupId>
<artifactId>scribe</artifactId> <artifactId>scribe</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>1.2.0</version> <version>1.2.1</version>
<name>Scribe OAuth Library</name> <name>Scribe OAuth Library</name>
<description>The best OAuth library out there</description> <description>The best OAuth library out there</description>
<url>http://github.com/fernandezpablo85/scribe-java</url> <url>http://github.com/fernandezpablo85/scribe-java</url>
@@ -0,0 +1,43 @@
package org.scribe.builder.api;
import org.scribe.extractors.*;
import org.scribe.model.*;
import org.scribe.utils.*;
import static org.scribe.utils.URLUtils.*;
public class LiveApi extends DefaultApi20
{
private static final String AUTHORIZE_URL = "https://oauth.live.com/authorize?client_id=%s&redirect_uri=%s&response_type=code";
private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL + "&scope=%s";
@Override
public String getAccessTokenEndpoint()
{
return "https://oauth.live.com/token?grant_type=authorization_code";
}
@Override
public String getAuthorizationUrl(OAuthConfig config)
{
Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. Live does not support OOB");
// Append scope if present
if (config.hasScope())
{
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), formURLEncode(config.getCallback()), formURLEncode(config.getScope()));
} else
{
return String.format(AUTHORIZE_URL, config.getApiKey(), formURLEncode(config.getCallback()));
}
}
@Override
public AccessTokenExtractor getAccessTokenExtractor()
{
return new JsonTokenExtractor();
}
}
@@ -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());
}
}
@@ -0,0 +1,28 @@
package org.scribe.builder.api;
import org.scribe.model.Token;
public class PlurkApi extends DefaultApi10a
{
private static final String REQUEST_TOKEN_URL = "http://www.plurk.com/OAuth/request_token";
private static final String AUTHORIZE_URL = "http://www.plurk.com/OAuth/authorize?oauth_token=%s";
private static final String ACCESS_TOKEN_URL = "http://www.plurk.com/OAuth/access_token";
@Override
public String getRequestTokenEndpoint()
{
return REQUEST_TOKEN_URL;
}
@Override
public String getAuthorizationUrl(Token requestToken)
{
return String.format(AUTHORIZE_URL, requestToken.getToken());
}
@Override
public String getAccessTokenEndpoint()
{
return ACCESS_TOKEN_URL;
}
}
@@ -0,0 +1,28 @@
package org.scribe.builder.api;
import org.scribe.model.*;
public class SinaWeiboApi extends DefaultApi10a
{
private static final String REQUEST_TOKEN_URL = "http://api.t.sina.com.cn/oauth/request_token";
private static final String ACCESS_TOKEN_URL = "http://api.t.sina.com.cn/oauth/access_token";
private static final String AUTHORIZE_URL = "http://api.t.sina.com.cn/oauth/authorize?oauth_token=%s";
@Override
public String getRequestTokenEndpoint()
{
return REQUEST_TOKEN_URL;
}
@Override
public String getAccessTokenEndpoint()
{
return ACCESS_TOKEN_URL;
}
@Override
public String getAuthorizationUrl(Token requestToken)
{
return String.format(AUTHORIZE_URL, requestToken.getToken());
}
}
@@ -0,0 +1,28 @@
package org.scribe.builder.api;
import org.scribe.model.*;
public class SohuWeiboApi extends DefaultApi10a
{
private static final String REQUEST_TOKEN_URL = "http://api.t.sohu.com/oauth/request_token";
private static final String ACCESS_TOKEN_URL = "http://api.t.sohu.com/oauth/access_token";
private static final String AUTHORIZE_URL = "http://api.t.sohu.com/oauth/authorize?oauth_token=%s";
@Override
public String getRequestTokenEndpoint()
{
return REQUEST_TOKEN_URL;
}
@Override
public String getAccessTokenEndpoint()
{
return ACCESS_TOKEN_URL;
}
@Override
public String getAuthorizationUrl(Token requestToken)
{
return String.format(AUTHORIZE_URL, requestToken.getToken());
}
}
@@ -0,0 +1,41 @@
package org.scribe.builder.api;
import org.scribe.extractors.*;
import org.scribe.utils.*;
import org.scribe.model.*;
/**
* @author Boris G. Tsirkin <mail@dotbg.name>
* @since 20.4.2011
*/
public class VkontakteApi extends DefaultApi20
{
private static final String AUTHORIZE_URL = "https://api.vkontakte.ru/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code";
private static final String SCOPED_AUTHORIZE_URL = String.format("%s&scope=%%s", AUTHORIZE_URL);
@Override
public String getAccessTokenEndpoint()
{
return "https://api.vkontakte.ru/oauth/access_token";
}
@Override
public String getAuthorizationUrl(OAuthConfig config)
{
Preconditions.checkValidUrl(config.getCallback(), "Valid url is required for a callback. Vkontakte does not support OOB");
if(config.hasScope())// Appending scope if present
{
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback()),URLUtils.formURLEncode(config.getScope()));
}
else
{
return String.format(AUTHORIZE_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback()));
}
}
@Override
public AccessTokenExtractor getAccessTokenExtractor()
{
return new JsonTokenExtractor();
}
}
@@ -36,7 +36,7 @@ public class BaseStringExtractorImpl implements BaseStringExtractor
MapUtils.decodeAndAppendEntries(request.getBodyParams(), params); MapUtils.decodeAndAppendEntries(request.getBodyParams(), params);
MapUtils.decodeAndAppendEntries(request.getOauthParameters(), params); MapUtils.decodeAndAppendEntries(request.getOauthParameters(), params);
params = MapUtils.sort(params); params = MapUtils.sort(params);
return URLUtils.percentEncode(URLUtils.concatSortedPercentEncodedParams(params)); return URLUtils.percentEncode(MapUtils.concatSortedPercentEncodedParams(params));
} }
private void checkPreconditions(OAuthRequest request) private void checkPreconditions(OAuthRequest request)
@@ -8,7 +8,7 @@ import org.scribe.utils.*;
public class JsonTokenExtractor implements AccessTokenExtractor public class JsonTokenExtractor implements AccessTokenExtractor
{ {
private Pattern accessTokenPattern = Pattern.compile("\"access_token\":\"(\\S*)\""); private Pattern accessTokenPattern = Pattern.compile("\"access_token\":\"(\\S*?)\"");
@Override @Override
public Token extract(String response) public Token extract(String response)
@@ -17,11 +17,11 @@ public class JsonTokenExtractor implements AccessTokenExtractor
Matcher matcher = accessTokenPattern.matcher(response); Matcher matcher = accessTokenPattern.matcher(response);
if(matcher.find()) if(matcher.find())
{ {
return new Token(matcher.group(1), ""); return new Token(matcher.group(1), "", response);
} }
else else
{ {
throw new OAuthException("Cannot extract an acces token. Response was: "+response); throw new OAuthException("Cannot extract an acces token. Response was: " + response);
} }
} }
+87 -10
View File
@@ -2,6 +2,7 @@ package org.scribe.model;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.nio.charset.*;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -24,6 +25,11 @@ class Request
private Map<String, String> headers; private Map<String, String> headers;
private String payload = null; private String payload = null;
private HttpURLConnection connection; private HttpURLConnection connection;
private String charset;
private byte[] bytePayload = null;
private boolean connectionKeepAlive = false;
private Long connectTimeout = null;
private Long readTimeout = null;
/** /**
* Creates a new Http Request * Creates a new Http Request
@@ -64,7 +70,7 @@ class Request
String effectiveUrl = URLUtils.appendParametersToQueryString(url, querystringParams); String effectiveUrl = URLUtils.appendParametersToQueryString(url, querystringParams);
if (connection == null) if (connection == null)
{ {
System.setProperty("http.keepAlive", "false"); System.setProperty("http.keepAlive", connectionKeepAlive ? "true" : "false");
connection = (HttpURLConnection) new URL(effectiveUrl).openConnection(); connection = (HttpURLConnection) new URL(effectiveUrl).openConnection();
} }
} }
@@ -72,10 +78,18 @@ class Request
Response doSend() throws IOException Response doSend() throws IOException
{ {
connection.setRequestMethod(this.verb.name()); connection.setRequestMethod(this.verb.name());
if (connectTimeout != null)
{
connection.setConnectTimeout(connectTimeout.intValue());
}
if (readTimeout != null)
{
connection.setReadTimeout(readTimeout.intValue());
}
addHeaders(connection); addHeaders(connection);
if (verb.equals(Verb.PUT) || verb.equals(Verb.POST)) if (verb.equals(Verb.PUT) || verb.equals(Verb.POST))
{ {
addBody(connection, getBodyContents()); addBody(connection, getByteBodyContents());
} }
return new Response(connection); return new Response(connection);
} }
@@ -86,11 +100,11 @@ class Request
conn.setRequestProperty(key, headers.get(key)); conn.setRequestProperty(key, headers.get(key));
} }
void addBody(HttpURLConnection conn, String content) throws IOException void addBody(HttpURLConnection conn, byte[] content) throws IOException
{ {
conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.getBytes().length)); conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.length));
conn.setDoOutput(true); conn.setDoOutput(true);
conn.getOutputStream().write(content.getBytes()); conn.getOutputStream().write(content);
} }
/** /**
@@ -141,6 +155,16 @@ class Request
this.payload = payload; this.payload = payload;
} }
/**
* Overloaded version for byte arrays
*
* @param payload
*/
public void addPayload(byte[] payload)
{
this.bytePayload = payload;
}
/** /**
* Get a {@link Map} of the query string parameters. * Get a {@link Map} of the query string parameters.
* *
@@ -153,7 +177,7 @@ class Request
{ {
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
String queryString = new URL(url).getQuery(); String queryString = new URL(url).getQuery();
params.putAll(URLUtils.queryStringToMap(queryString)); params.putAll(MapUtils.queryStringToMap(queryString));
params.putAll(this.querystringParams); params.putAll(this.querystringParams);
return params; return params;
} }
@@ -197,10 +221,32 @@ class Request
* Returns the body of the request * Returns the body of the request
* *
* @return form encoded string * @return form encoded string
* @throws OAuthException if the charset chosen is not supported
*/ */
public String getBodyContents() public String getBodyContents()
{ {
return (payload != null) ? payload : URLUtils.formURLEncodeMap(bodyParams); try
{
return new String(getByteBodyContents(),getCharset());
}
catch(UnsupportedEncodingException uee)
{
throw new OAuthException("Unsupported Charset: "+charset, uee);
}
}
byte[] getByteBodyContents()
{
if (bytePayload != null) return bytePayload;
String body = (payload != null) ? payload : URLUtils.formURLEncodeMap(bodyParams);
try
{
return body.getBytes(getCharset());
}
catch(UnsupportedEncodingException uee)
{
throw new OAuthException("Unsupported Charset: "+getCharset(), uee);
}
} }
/** /**
@@ -223,6 +269,16 @@ class Request
return headers; return headers;
} }
/**
* Returns the connection charset. Defaults to {@link Charset} defaultCharset if not set
*
* @return charset
*/
public String getCharset()
{
return charset == null ? Charset.defaultCharset().name() : charset;
}
/** /**
* Sets the connect timeout for the underlying {@link HttpURLConnection} * Sets the connect timeout for the underlying {@link HttpURLConnection}
* *
@@ -232,7 +288,7 @@ class Request
*/ */
public void setConnectTimeout(int duration, TimeUnit unit) public void setConnectTimeout(int duration, TimeUnit unit)
{ {
this.connection.setConnectTimeout((int) unit.toMillis(duration)); this.connectTimeout = unit.toMillis(duration);
} }
/** /**
@@ -244,9 +300,30 @@ class Request
*/ */
public void setReadTimeout(int duration, TimeUnit unit) public void setReadTimeout(int duration, TimeUnit unit)
{ {
this.connection.setReadTimeout((int) unit.toMillis(duration)); this.readTimeout = unit.toMillis(duration);
} }
/**
* Set the charset of the body of the request
*
* @param charsetName name of the charset of the request
*/
public void setCharset(String charsetName)
{
this.charset = charsetName;
}
/**
* Sets wether the underlying Http Connection is persistent or not.
*
* @see http://download.oracle.com/javase/1.5.0/docs/guide/net/http-keepalive.html
* @param connectionKeepAlive
*/
public void setConnectionKeepAlive(boolean connectionKeepAlive)
{
this.connectionKeepAlive = connectionKeepAlive;
}
/* /*
* We need this in order to stub the connection object for test cases * We need this in order to stub the connection object for test cases
*/ */
@@ -9,6 +9,10 @@ import java.util.*;
*/ */
public class MapUtils public class MapUtils
{ {
private static final String EMPTY_STRING = "";
private static final String PAIR_SEPARATOR = "=";
private static final String PARAM_SEPARATOR = "&";
/** /**
* Sorts a Map * Sorts a Map
* *
@@ -48,4 +52,43 @@ public class MapUtils
target.put(URLUtils.percentEncode(key), URLUtils.percentEncode(source.get(key))); target.put(URLUtils.percentEncode(key), URLUtils.percentEncode(source.get(key)));
} }
} }
/**
* Concats a key-value map into a querystring-like String
*
* @param params key-value map
* @return querystring-like String
*/
public static String concatSortedPercentEncodedParams(Map<String, String> params)
{
StringBuilder result = new StringBuilder();
for (String key : params.keySet())
{
result.append(key).append(PAIR_SEPARATOR);
result.append(params.get(key)).append(PARAM_SEPARATOR);
}
return result.toString().substring(0, result.length() - 1);
}
/**
* Parses and form-urldecodes a querystring-like string into a map
*
* @param queryString querystring-like String
* @return a map with the form-urldecoded parameters
*/
public static Map<String, String> queryStringToMap(String queryString)
{
Map<String, String> result = new HashMap<String, String>();
if (queryString != null && queryString.length() > 0)
{
for (String param : queryString.split(PARAM_SEPARATOR))
{
String pair[] = param.split(PAIR_SEPARATOR);
String key = URLUtils.formURLDecode(pair[0]);
String value = pair.length > 1 ? URLUtils.formURLDecode(pair[1]) : EMPTY_STRING;
result.put(key, value);
}
}
return result;
}
} }
@@ -133,47 +133,6 @@ public class URLUtils
} }
} }
/**
* Concats a key-value map into a querystring-like String
*
* @param params key-value map
* @return querystring-like String
*/
// TODO Move to MapUtils
public static String concatSortedPercentEncodedParams(Map<String, String> params)
{
StringBuilder result = new StringBuilder();
for (String key : params.keySet())
{
result.append(key).append(PAIR_SEPARATOR);
result.append(params.get(key)).append(PARAM_SEPARATOR);
}
return result.toString().substring(0, result.length() - 1);
}
/**
* Parses and form-urldecodes a querystring-like string into a map
*
* @param queryString querystring-like String
* @return a map with the form-urldecoded parameters
*/
// TODO Move to MapUtils
public static Map<String, String> queryStringToMap(String queryString)
{
Map<String, String> result = new HashMap<String, String>();
if (queryString != null && queryString.length() > 0)
{
for (String param : queryString.split(PARAM_SEPARATOR))
{
String pair[] = param.split(PAIR_SEPARATOR);
String key = formURLDecode(pair[0]);
String value = pair.length > 1 ? formURLDecode(pair[1]) : EMPTY_STRING;
result.put(key, value);
}
}
return result;
}
private static final class EncodingRule private static final class EncodingRule
{ {
private final String ch; private final String ch;
@@ -0,0 +1,64 @@
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 LiveExample
{
private static final String PROTECTED_RESOURCE_URL = "https://api.foursquare.com/v2/users/self/friends?oauth_token=";
private static final Token EMPTY_TOKEN = null;
public static void main(String[] args)
{
// Replace these with your own api key and secret
String apiKey = "";
String apiSecret = "";
OAuthService service = new ServiceBuilder()
.provider(LiveApi.class)
.apiKey(apiKey)
.apiSecret(apiSecret)
.scope("wl.basic")
.callback("http://localhost:9000/")
.build();
Scanner in = new Scanner(System.in);
System.out.println("=== Windows Live's OAuth Workflow ===");
System.out.println();
// Obtain the Authorization URL
System.out.println("Fetching the Authorization URL...");
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
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(EMPTY_TOKEN, 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 + accessToken.getToken());
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! :)");
}
}
@@ -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! :)");
}
}
@@ -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 SinaWeiboExample
{
private static final String NETWORK_NAME = "SinaWeibo";
private static final String PROTECTED_RESOURCE_URL = "http://api.t.sina.com.cn/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(SinaWeiboApi.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! :)");
}
}
@@ -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 SohuWeiboExample
{
private static final String NETWORK_NAME = "SohuWeibo";
private static final String PROTECTED_RESOURCE_URL = "http://api.t.sohu.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(SohuWeiboApi.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! :)");
}
}
@@ -0,0 +1,68 @@
package org.scribe.examples;
import org.scribe.builder.*;
import org.scribe.builder.api.*;
import org.scribe.model.*;
import org.scribe.oauth.*;
import java.util.*;
/**
* @author Boris G. Tsirkin <mail@dotbg.name>
* @since 20.4.2011
*/
public class VkontakteExample
{
private static final String NETWORK_NAME = "Vkontakte.ru";
private static final String PROTECTED_RESOURCE_URL = "https://api.vkontakte.ru/method/friends.get";
private static final Token EMPTY_TOKEN = null;
public static void main(String[] args)
{
// Replace these with your own api key and secret
final String clientId = "your app id";
final String apiSecret = "your api secret";
OAuthService service = new ServiceBuilder()
.provider(VkontakteApi.class)
.apiKey(clientId)
.apiSecret(apiSecret)
.scope("friends,wall,offline") // replace with desired scope
.callback("http://your.site.com/callback")
.build();
Scanner in = new Scanner(System.in);
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
System.out.println();
// Obtain the Authorization URL
System.out.println("Fetching the Authorization URL...");
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
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(EMPTY_TOKEN, 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! :)");
}
}