Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f47bebced | ||
|
|
cc5a0a0471 | ||
|
|
0c3fc722b1 | ||
|
|
203cf83dc0 | ||
|
|
7b587c3d1d | ||
|
|
f89279c682 | ||
|
|
7b6b9056d3 | ||
|
|
ca7950004b | ||
|
|
543b53475f | ||
|
|
de1ac1f467 | ||
|
|
97f45d75e7 | ||
|
|
624fdfb714 | ||
|
|
bd51ae9f25 | ||
|
|
40ab98d506 | ||
|
|
0abe7f80d4 | ||
|
|
592b1b5fdd | ||
|
|
2112d3d8a9 | ||
|
|
109c93b4e4 | ||
|
|
41b6da1a44 | ||
|
|
91cb9b8407 |
No files matched your search
+1
-1
@@ -61,7 +61,7 @@ You can pull scribe from a maven repository, just add this to your __pom.xml__ f
|
||||
@<dependency>@
|
||||
@<groupId>org.scribe</groupId>@
|
||||
@<artifactId>scribe</artifactId>@
|
||||
@<version>1.1.2</version>@
|
||||
@<version>1.2.1</version>@
|
||||
@</dependency>@
|
||||
|
||||
h1. Getting started in less than 2 minutes
|
||||
|
||||
@@ -44,3 +44,15 @@
|
||||
* FEATURE: Added Foursquare OAuth 2.0 Api (thanks Tom Tasche)
|
||||
* FEATURE: Added support for OAuth1.0a signatures via querystring parameters
|
||||
* FEATURE: Added ConstantContact Api (thanks Matthew Laudato)
|
||||
|
||||
[1.2]
|
||||
* REFACTOR: Moved scope inside OAuthConfig (breaks backwards compatibility)
|
||||
* FEATURE: Added scopes to Facebook 2.0 Api
|
||||
* FEATURE: Added Plaintext signature for Yammer
|
||||
* 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)
|
||||
@@ -4,7 +4,7 @@
|
||||
<groupId>org.scribe</groupId>
|
||||
<artifactId>scribe</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.1.2</version>
|
||||
<version>1.2.1</version>
|
||||
<name>Scribe OAuth Library</name>
|
||||
<description>The best OAuth library out there</description>
|
||||
<url>http://github.com/fernandezpablo85/scribe-java</url>
|
||||
|
||||
@@ -147,6 +147,6 @@ public class ServiceBuilder
|
||||
Preconditions.checkNotNull(api, "You must specify a valid api through the provider() method");
|
||||
Preconditions.checkEmptyString(apiKey, "You must provide an api key");
|
||||
Preconditions.checkEmptyString(apiSecret, "You must provide an api secret");
|
||||
return api.createService(new OAuthConfig(apiKey, apiSecret, callback, signatureType), scope);
|
||||
return api.createService(new OAuthConfig(apiKey, apiSecret, callback, signatureType, scope));
|
||||
}
|
||||
}
|
||||
@@ -21,5 +21,5 @@ public interface Api
|
||||
*
|
||||
* @return fully configured {@link OAuthService}
|
||||
*/
|
||||
OAuthService createService(OAuthConfig config, String scope);
|
||||
OAuthService createService(OAuthConfig config);
|
||||
}
|
||||
@@ -134,14 +134,7 @@ public abstract class DefaultApi10a implements Api
|
||||
* @param callback OAuth callback (either URL or 'oob')
|
||||
* @param scope OAuth scope (optional)
|
||||
*/
|
||||
public OAuthService createService(OAuthConfig config, String scope)
|
||||
{
|
||||
OAuthService service = doCreateService(config);
|
||||
service.addScope(scope);
|
||||
return service;
|
||||
}
|
||||
|
||||
private OAuthService doCreateService(OAuthConfig config)
|
||||
public OAuthService createService(OAuthConfig config)
|
||||
{
|
||||
return new OAuth10aServiceImpl(this, config);
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ public abstract class DefaultApi20 implements Api
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public OAuthService createService(OAuthConfig config, String scope)
|
||||
public OAuthService createService(OAuthConfig config)
|
||||
{
|
||||
return new OAuth20ServiceImpl(this, config);
|
||||
}
|
||||
|
||||
@@ -1,22 +1,34 @@
|
||||
package org.scribe.builder.api;
|
||||
|
||||
import org.scribe.model.*;
|
||||
|
||||
import org.scribe.utils.*;
|
||||
import static org.scribe.utils.URLUtils.*;
|
||||
|
||||
public class FacebookApi extends DefaultApi20
|
||||
{
|
||||
private static final String AUTHORIZE_URL = "https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s";
|
||||
private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL + "&scope=%s";
|
||||
|
||||
@Override
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
return "https://graph.facebook.com/oauth/access_token";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getAuthorizationUrl(OAuthConfig config)
|
||||
{
|
||||
Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. Facebook does not support OOB");
|
||||
return String.format(AUTHORIZE_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback()));
|
||||
|
||||
// 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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.*;
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -5,22 +5,39 @@ import org.scribe.model.Token;
|
||||
public class TwitterApi extends DefaultApi10a
|
||||
{
|
||||
private static final String AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize?oauth_token=%s";
|
||||
private static final String REQUEST_TOKEN_RESOURCE = "api.twitter.com/oauth/request_token";
|
||||
private static final String ACCESS_TOKEN_RESOURCE = "api.twitter.com/oauth/access_token";
|
||||
|
||||
@Override
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
return "http://api.twitter.com/oauth/access_token";
|
||||
return "http://" + ACCESS_TOKEN_RESOURCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
return "http://api.twitter.com/oauth/request_token";
|
||||
return "http://" + REQUEST_TOKEN_RESOURCE;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getAuthorizationUrl(Token requestToken)
|
||||
{
|
||||
return String.format(AUTHORIZE_URL, requestToken.getToken());
|
||||
}
|
||||
|
||||
public static class SSL extends TwitterApi
|
||||
{
|
||||
@Override
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
return "https://" + ACCESS_TOKEN_RESOURCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
return "https://" + REQUEST_TOKEN_RESOURCE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,33 @@
|
||||
package org.scribe.builder.api;
|
||||
|
||||
import org.scribe.model.Token;
|
||||
import org.scribe.model.*;
|
||||
import org.scribe.services.*;
|
||||
|
||||
public class YammerApi extends DefaultApi10a
|
||||
{
|
||||
private static final String AUTHORIZATION_URL = "'https://www.yammer.com/oauth/authorize?oauth_token=%s'";
|
||||
|
||||
@Override
|
||||
public String getRequestTokenEndpoint()
|
||||
@Override
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
return "https://www.yammer.com/oauth/request_token";
|
||||
}
|
||||
return "https://www.yammer.com/oauth/request_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAccessTokenEndpoint()
|
||||
@Override
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
return "https://www.yammer.com/oauth/access_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthorizationUrl(Token requestToken)
|
||||
{
|
||||
return String.format(AUTHORIZATION_URL, requestToken.getToken());
|
||||
}
|
||||
return "https://www.yammer.com/oauth/access_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthorizationUrl(Token requestToken)
|
||||
{
|
||||
return String.format(AUTHORIZATION_URL, requestToken.getToken());
|
||||
}
|
||||
|
||||
@Override
|
||||
public SignatureService getSignatureService()
|
||||
{
|
||||
return new PlaintextSignatureService();
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import org.scribe.utils.*;
|
||||
|
||||
public class JsonTokenExtractor implements AccessTokenExtractor
|
||||
{
|
||||
private Pattern accessTokenPattern = Pattern.compile("\"access_token\":\"(\\S*)\"");
|
||||
private Pattern accessTokenPattern = Pattern.compile("\"access_token\":\"(\\S*?)\"");
|
||||
|
||||
@Override
|
||||
public Token extract(String response)
|
||||
@@ -17,11 +17,11 @@ public class JsonTokenExtractor implements AccessTokenExtractor
|
||||
Matcher matcher = accessTokenPattern.matcher(response);
|
||||
if(matcher.find())
|
||||
{
|
||||
return new Token(matcher.group(1), "");
|
||||
return new Token(matcher.group(1), "", response);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new OAuthException("Cannot extract an acces token. Response was: "+response);
|
||||
throw new OAuthException("Cannot extract an acces token. Response was: " + response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,28 +7,24 @@ package org.scribe.model;
|
||||
*/
|
||||
public class OAuthConfig
|
||||
{
|
||||
private String apiKey;
|
||||
private String apiSecret;
|
||||
private String callback;
|
||||
private SignatureType signatureType;
|
||||
private final String apiKey;
|
||||
private final String apiSecret;
|
||||
private final String callback;
|
||||
private final SignatureType signatureType;
|
||||
private final String scope;
|
||||
|
||||
public OAuthConfig(String key, String secret)
|
||||
{
|
||||
this(key,secret,null);
|
||||
this(key, secret, null, null, null);
|
||||
}
|
||||
|
||||
//Kept for backwards compatibility
|
||||
public OAuthConfig(String key, String secret, String callback)
|
||||
{
|
||||
this(key,secret,callback,null);
|
||||
}
|
||||
|
||||
public OAuthConfig(String key, String secret, String callback, SignatureType type)
|
||||
public OAuthConfig(String key, String secret, String callback, SignatureType type, String scope)
|
||||
{
|
||||
this.apiKey = key;
|
||||
this.apiSecret = secret;
|
||||
this.callback = callback;
|
||||
this.signatureType = type;
|
||||
this.callback = callback != null ? callback : OAuthConstants.OUT_OF_BAND;
|
||||
this.signatureType = (type != null) ? type : SignatureType.Header;
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
public String getApiKey()
|
||||
@@ -36,38 +32,28 @@ public class OAuthConfig
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
public void setApiKey(String apiKey)
|
||||
{
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
|
||||
public String getApiSecret()
|
||||
{
|
||||
return apiSecret;
|
||||
}
|
||||
|
||||
public void setApiSecret(String apiSecret)
|
||||
{
|
||||
this.apiSecret = apiSecret;
|
||||
}
|
||||
|
||||
public String getCallback()
|
||||
{
|
||||
return callback != null ? callback : OAuthConstants.OUT_OF_BAND;
|
||||
}
|
||||
|
||||
public void setCallback(String callback)
|
||||
{
|
||||
this.callback = callback;
|
||||
return callback;
|
||||
}
|
||||
|
||||
public SignatureType getSignatureType()
|
||||
{
|
||||
return signatureType != null ? signatureType : SignatureType.Header;
|
||||
return signatureType;
|
||||
}
|
||||
|
||||
public void setSignatureType(SignatureType type)
|
||||
public String getScope()
|
||||
{
|
||||
this.signatureType = type;
|
||||
return scope;
|
||||
}
|
||||
|
||||
public boolean hasScope()
|
||||
{
|
||||
return scope != null;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package org.scribe.model;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.charset.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -24,6 +25,9 @@ class Request
|
||||
private Map<String, String> headers;
|
||||
private String payload = null;
|
||||
private HttpURLConnection connection;
|
||||
private String charset;
|
||||
private byte[] bytePayload = null;
|
||||
private boolean connectionKeepAlive = false;
|
||||
|
||||
/**
|
||||
* Creates a new Http Request
|
||||
@@ -64,7 +68,7 @@ class Request
|
||||
String effectiveUrl = URLUtils.appendParametersToQueryString(url, querystringParams);
|
||||
if (connection == null)
|
||||
{
|
||||
System.setProperty("http.keepAlive", "false");
|
||||
System.setProperty("http.keepAlive", connectionKeepAlive ? "true" : "false");
|
||||
connection = (HttpURLConnection) new URL(effectiveUrl).openConnection();
|
||||
}
|
||||
}
|
||||
@@ -75,7 +79,7 @@ class Request
|
||||
addHeaders(connection);
|
||||
if (verb.equals(Verb.PUT) || verb.equals(Verb.POST))
|
||||
{
|
||||
addBody(connection, getBodyContents());
|
||||
addBody(connection, getByteBodyContents());
|
||||
}
|
||||
return new Response(connection);
|
||||
}
|
||||
@@ -86,11 +90,11 @@ class Request
|
||||
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.getOutputStream().write(content.getBytes());
|
||||
conn.getOutputStream().write(content);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,6 +145,16 @@ class Request
|
||||
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.
|
||||
*
|
||||
@@ -197,10 +211,32 @@ class Request
|
||||
* Returns the body of the request
|
||||
*
|
||||
* @return form encoded string
|
||||
* @throws OAuthException if the charset chosen is not supported
|
||||
*/
|
||||
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 +259,16 @@ class Request
|
||||
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}
|
||||
*
|
||||
@@ -246,7 +292,28 @@ class Request
|
||||
{
|
||||
this.connection.setReadTimeout((int) 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
|
||||
*/
|
||||
|
||||
@@ -12,12 +12,10 @@ import org.scribe.model.*;
|
||||
*/
|
||||
public class OAuth10aServiceImpl implements OAuthService
|
||||
{
|
||||
private static final String NO_SCOPE = null;
|
||||
private static final String VERSION = "1.0";
|
||||
|
||||
private OAuthConfig config;
|
||||
private DefaultApi10a api;
|
||||
private String scope;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
@@ -29,7 +27,6 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
{
|
||||
this.api = api;
|
||||
this.config = config;
|
||||
this.scope = NO_SCOPE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,7 +49,7 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
request.addOAuthParameter(OAuthConstants.CONSUMER_KEY, config.getApiKey());
|
||||
request.addOAuthParameter(OAuthConstants.SIGN_METHOD, api.getSignatureService().getSignatureMethod());
|
||||
request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
|
||||
if(scope != NO_SCOPE) request.addOAuthParameter(OAuthConstants.SCOPE, scope);
|
||||
if(config.hasScope()) request.addOAuthParameter(OAuthConstants.SCOPE, config.getScope());
|
||||
request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));
|
||||
}
|
||||
|
||||
@@ -88,14 +85,6 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
return VERSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void addScope(String scope)
|
||||
{
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
||||
@@ -22,14 +22,6 @@ public class OAuth20ServiceImpl implements OAuthService
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void addScope(String scope)
|
||||
{
|
||||
throw new UnsupportedOperationException("OAuth 2 does not use scopes");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@@ -40,6 +32,7 @@ public class OAuth20ServiceImpl implements OAuthService
|
||||
request.addQuerystringParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
|
||||
request.addQuerystringParameter(OAuthConstants.CODE, verifier.getValue());
|
||||
request.addQuerystringParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
|
||||
if(config.hasScope()) request.addQuerystringParameter(OAuthConstants.SCOPE, config.getScope());
|
||||
Response response = request.send();
|
||||
return api.getAccessTokenExtractor().extract(response.getBody());
|
||||
}
|
||||
|
||||
@@ -41,13 +41,6 @@ public interface OAuthService
|
||||
* @return oauth version as string
|
||||
*/
|
||||
public String getVersion();
|
||||
|
||||
/**
|
||||
* Adds the 'scope' parameter. This is **not** a default OAuth parameter and the setting is optional.
|
||||
*
|
||||
* @param scope OAuth Api scope (optional)
|
||||
*/
|
||||
public void addScope(String scope);
|
||||
|
||||
/**
|
||||
* Returns the URL where you should redirect your users to authenticate
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.scribe.services;
|
||||
|
||||
import org.scribe.exceptions.*;
|
||||
import org.scribe.utils.*;
|
||||
|
||||
/**
|
||||
* plaintext implementation of {@SignatureService}
|
||||
*
|
||||
* @author Pablo Fernandez
|
||||
*
|
||||
*/
|
||||
public class PlaintextSignatureService implements SignatureService
|
||||
{
|
||||
private static final String METHOD = "plaintext";
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public String getSignature(String baseString, String apiSecret, String tokenSecret)
|
||||
{
|
||||
try
|
||||
{
|
||||
Preconditions.checkEmptyString(apiSecret, "Api secret cant be null or empty string");
|
||||
return URLUtils.percentEncode(apiSecret) + '&' + URLUtils.percentEncode(tokenSecret);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new OAuthSignatureException(baseString, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public String getSignatureMethod()
|
||||
{
|
||||
return METHOD;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,19 +57,17 @@ public class ServiceBuilderTest
|
||||
builder.provider(ApiMock.class).apiKey("key").apiSecret("secret").scope("rss-api").build();
|
||||
assertEquals(ApiMock.config.getApiKey(), "key");
|
||||
assertEquals(ApiMock.config.getApiSecret(), "secret");
|
||||
assertEquals(ApiMock.scope, "rss-api");
|
||||
assertEquals(ApiMock.config.getScope(), "rss-api");
|
||||
}
|
||||
|
||||
public static class ApiMock implements Api
|
||||
{
|
||||
public static OAuthConfig config;
|
||||
public static String scope;
|
||||
|
||||
@Override
|
||||
public OAuthService createService(OAuthConfig config, String scope)
|
||||
public OAuthService createService(OAuthConfig config)
|
||||
{
|
||||
ApiMock.config = config;
|
||||
ApiMock.scope = scope;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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! :)");
|
||||
}
|
||||
}
|
||||
@@ -12,12 +12,14 @@ public class OAuthConfigTest
|
||||
{
|
||||
OAuthConfig config = new OAuthConfig("key", "secret");
|
||||
assertEquals(OAuthConstants.OUT_OF_BAND, config.getCallback());
|
||||
assertEquals(SignatureType.Header, config.getSignatureType());
|
||||
assertFalse(config.hasScope());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldOverrideDefaultsIfSet()
|
||||
{
|
||||
OAuthConfig config = new OAuthConfig("key", "secret", "http://callback");
|
||||
OAuthConfig config = new OAuthConfig("key", "secret", "http://callback", SignatureType.Header, "scope");
|
||||
assertEquals("http://callback", config.getCallback());
|
||||
assertEquals("key", config.getApiKey());
|
||||
assertEquals("secret", config.getApiSecret());
|
||||
|
||||
Reference in new issue
Block a user