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 | ||
|
|
c265e730a3 | ||
|
|
5719d7a18a | ||
|
|
8abdde928a | ||
|
|
193dcd2e25 | ||
|
|
931236561a | ||
|
|
5f40e0dca0 | ||
|
|
8afb142b7f | ||
|
|
bfcf0655da | ||
|
|
db88d53807 | ||
|
|
cbfd88d749 | ||
|
|
1eb0b1003c | ||
|
|
75bcb0013d | ||
|
|
145c009fc3 | ||
|
|
72901165f8 | ||
|
|
4f23cfaf20 | ||
|
|
5c0d150547 | ||
|
|
6bdae4999e | ||
|
|
c441b7d8a4 | ||
|
|
d8ab4ba404 | ||
|
|
6923d3311f | ||
|
|
0832613b60 | ||
|
|
73a53b363a | ||
|
|
16b3475926 | ||
|
|
22367de553 | ||
|
|
906a979e63 | ||
|
|
bb872e4466 | ||
|
|
d19c5d6e35 | ||
|
|
e0e8df98d3 | ||
|
|
de8f0d621d | ||
|
|
db37744fa3 | ||
|
|
02f3d4dac2 | ||
|
|
7385d3702e | ||
|
|
581e2b6d84 | ||
|
|
08aebb5fb1 | ||
|
|
c7176c97e2 | ||
|
|
70358ad651 | ||
|
|
0f39d37ddc | ||
|
|
da3a3f4a0d | ||
|
|
380d858b13 | ||
|
|
9c4f5bc337 | ||
|
|
09ee10dbd1 | ||
|
|
690b9d4e21 | ||
|
|
080dd962a9 | ||
|
|
40bb525374 | ||
|
|
8e23e81c57 | ||
|
|
1f055d7eeb | ||
|
|
1f36fd0f32 | ||
|
|
9ea686a3f8 | ||
|
|
7e92db3413 | ||
|
|
f6c3cb9bdb | ||
|
|
088fc5e443 | ||
|
|
27bac50073 | ||
|
|
6fc5146706 | ||
|
|
0e5ae92d3e | ||
|
|
c145ee8842 | ||
|
|
bdfd14309f | ||
|
|
82f03604c3 | ||
|
|
5ac849313d | ||
|
|
c12da16a38 | ||
|
|
b78ca90df4 | ||
|
|
577c18c752 | ||
|
|
41cbee06d7 | ||
|
|
8eb2062e95 | ||
|
|
8f0760664f | ||
|
|
0be82d6832 | ||
|
|
d0fa8689cb | ||
|
|
896e738156 | ||
|
|
cf84381127 | ||
|
|
806d548629 | ||
|
|
959c093620 | ||
|
|
fc2c46bd3a | ||
|
|
f1ecabdb52 | ||
|
|
83f17b0652 | ||
|
|
ebe0ff9eda | ||
|
|
6e141ba6a2 | ||
|
|
23af69cbf8 | ||
|
|
c0d24b556c | ||
|
|
99a03b4679 | ||
|
|
9337ac01d9 | ||
|
|
dee0f55b1e | ||
|
|
377c3233d9 | ||
|
|
029125490d | ||
|
|
20b200c7bc | ||
|
|
3e7291eaee | ||
|
|
149756e5ed |
No files matched your search
+10
-4
@@ -1,5 +1,3 @@
|
|||||||
!http://img411.imageshack.us/img411/2851/scribelogo.png!
|
|
||||||
|
|
||||||
h2. Welcome to the home of Scribe, the simple OAuth Java lib!
|
h2. Welcome to the home of Scribe, the simple OAuth Java lib!
|
||||||
|
|
||||||
h1. Why use Scribe?
|
h1. Why use Scribe?
|
||||||
@@ -20,10 +18,12 @@ h3. Threadsafe
|
|||||||
|
|
||||||
Hit Scribe as hard and with many threads as you like.
|
Hit Scribe as hard and with many threads as you like.
|
||||||
|
|
||||||
h3. Supports all major OAuth APIs out-of-the-box
|
h3. Supports all major 1.0a and 2.0 OAuth APIs out-of-the-box
|
||||||
|
|
||||||
* Google
|
* Google
|
||||||
|
|
||||||
|
* Facebook
|
||||||
|
|
||||||
* Yahoo
|
* Yahoo
|
||||||
|
|
||||||
* LinkedIn
|
* LinkedIn
|
||||||
@@ -32,6 +32,12 @@ h3. Supports all major OAuth APIs out-of-the-box
|
|||||||
|
|
||||||
* Foursquare
|
* Foursquare
|
||||||
|
|
||||||
|
* Evernote
|
||||||
|
|
||||||
|
* Vimeo
|
||||||
|
|
||||||
|
* Yammer
|
||||||
|
|
||||||
* 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
|
||||||
@@ -55,7 +61,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.0.6</version>@
|
@<version>1.2.1</version>@
|
||||||
@</dependency>@
|
@</dependency>@
|
||||||
|
|
||||||
h1. Getting started in less than 2 minutes
|
h1. Getting started in less than 2 minutes
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
mvn source:jar javadoc:jar package gpg:sign repository:bundle-create -Dgpg.passphrase=$1
|
||||||
@@ -6,3 +6,53 @@
|
|||||||
|
|
||||||
* FEATURE: Added support for Yammer and Evernote ( thanks oparrsih )
|
* FEATURE: Added support for Yammer and Evernote ( thanks oparrsih )
|
||||||
* FIX: Fixed bug that preventing setting the scope param on Google Api
|
* FIX: Fixed bug that preventing setting the scope param on Google Api
|
||||||
|
|
||||||
|
[1.0.8]
|
||||||
|
|
||||||
|
* FEATURE: Added setConnectTimeout and setReadTimeout for the Request Object
|
||||||
|
* FIX: Fixed Evernote Api (uses GET for request and access tokens)
|
||||||
|
* REFACTOR: URLEncoding stuff. LinkedIn specific decoder is no longer needed
|
||||||
|
* FEATURE: Added Google example
|
||||||
|
|
||||||
|
[1.0.9]
|
||||||
|
|
||||||
|
* FEATURE: Token made Serializable
|
||||||
|
* FIX: Google Api encoding issues (thanks @roger and @klakegg)
|
||||||
|
|
||||||
|
[1.1.0]
|
||||||
|
|
||||||
|
* FEATURE: OAuth 2.0 Support and Facebook support out of the box (thanks Diego Silveira)
|
||||||
|
* REFACTOR: Api creation and OAuthService refactors
|
||||||
|
* REFACTOR: Connections are created lazily (just before sending the request)
|
||||||
|
|
||||||
|
[1.1.1]
|
||||||
|
|
||||||
|
* FIX: Removed isEmpty() from OAuth2.0 code in order to support older JVMs
|
||||||
|
* FEATURE: Overloaded provider() method to support instances as well as classes (thanks dustismo)
|
||||||
|
* FIX: White-space parameter bug (thanks Krogoth)
|
||||||
|
* FEATURE: OAuth 2.0 now implements server-flow (thanks Diego Silveira)
|
||||||
|
|
||||||
|
[1.1.2]
|
||||||
|
|
||||||
|
* FIX: Fixed ugly bug that made 1.1.1 practically unusable (thanks Stubbs)
|
||||||
|
|
||||||
|
[1.1.3]
|
||||||
|
|
||||||
|
* FEATURE: Added rawResponse to Token, for extracting additional parameters (thanks Dirk McCormick)
|
||||||
|
* FEATURE: Added Dropbox Api
|
||||||
|
* FIX: Token responses now work despite of the token/secret order
|
||||||
|
* 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>
|
<groupId>org.scribe</groupId>
|
||||||
<artifactId>scribe</artifactId>
|
<artifactId>scribe</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<version>1.0.7</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>
|
||||||
@@ -22,6 +22,12 @@
|
|||||||
<email>fernandezpablo85@gmail.com</email>
|
<email>fernandezpablo85@gmail.com</email>
|
||||||
<timezone>-3</timezone>
|
<timezone>-3</timezone>
|
||||||
</developer>
|
</developer>
|
||||||
|
<developer>
|
||||||
|
<id>diegossilveira</id>
|
||||||
|
<name>Diego Silveira</name>
|
||||||
|
<email>diegossilveira@gmail.com</email>
|
||||||
|
<timezone>-3</timezone>
|
||||||
|
</developer>
|
||||||
</developers>
|
</developers>
|
||||||
|
|
||||||
<licenses>
|
<licenses>
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package org.scribe.builder;
|
|||||||
|
|
||||||
import org.scribe.builder.api.*;
|
import org.scribe.builder.api.*;
|
||||||
import org.scribe.exceptions.*;
|
import org.scribe.exceptions.*;
|
||||||
|
|
||||||
import org.scribe.model.*;
|
import org.scribe.model.*;
|
||||||
import org.scribe.oauth.*;
|
import org.scribe.oauth.*;
|
||||||
import org.scribe.utils.*;
|
import org.scribe.utils.*;
|
||||||
@@ -21,6 +20,7 @@ public class ServiceBuilder
|
|||||||
private String callback;
|
private String callback;
|
||||||
private Api api;
|
private Api api;
|
||||||
private String scope;
|
private String scope;
|
||||||
|
private SignatureType signatureType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor
|
* Default constructor
|
||||||
@@ -56,7 +56,22 @@ public class ServiceBuilder
|
|||||||
}
|
}
|
||||||
return api;
|
return api;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures the {@link Api}
|
||||||
|
*
|
||||||
|
* Overloaded version. Let's you use an instance instead of a class.
|
||||||
|
*
|
||||||
|
* @param api instance of {@link Api}s
|
||||||
|
* @return the {@link ServiceBuilder} instance for method chaining
|
||||||
|
*/
|
||||||
|
public ServiceBuilder provider(Api api)
|
||||||
|
{
|
||||||
|
Preconditions.checkNotNull(api, "Api cannot be null");
|
||||||
|
this.api = api;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an OAuth callback url
|
* Adds an OAuth callback url
|
||||||
*
|
*
|
||||||
@@ -108,6 +123,19 @@ public class ServiceBuilder
|
|||||||
this.scope = scope;
|
this.scope = scope;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures the signature type, choose between header, querystring, etc. Defaults to Header
|
||||||
|
*
|
||||||
|
* @param scope The OAuth scope
|
||||||
|
* @return the {@link ServiceBuilder} instance for method chaining
|
||||||
|
*/
|
||||||
|
public ServiceBuilder signatureType(SignatureType type)
|
||||||
|
{
|
||||||
|
Preconditions.checkNotNull(type, "Signature type can't be null");
|
||||||
|
this.signatureType = type;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the fully configured {@link OAuthService}
|
* Returns the fully configured {@link OAuthService}
|
||||||
@@ -119,6 +147,6 @@ public class ServiceBuilder
|
|||||||
Preconditions.checkNotNull(api, "You must specify a valid api through the provider() method");
|
Preconditions.checkNotNull(api, "You must specify a valid api through the provider() method");
|
||||||
Preconditions.checkEmptyString(apiKey, "You must provide an api key");
|
Preconditions.checkEmptyString(apiKey, "You must provide an api key");
|
||||||
Preconditions.checkEmptyString(apiSecret, "You must provide an api secret");
|
Preconditions.checkEmptyString(apiSecret, "You must provide an api secret");
|
||||||
return api.createService(apiKey, apiSecret, callback, scope);
|
return api.createService(new OAuthConfig(apiKey, apiSecret, callback, signatureType, scope));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.scribe.builder.api;
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
|
import org.scribe.model.*;
|
||||||
import org.scribe.oauth.*;
|
import org.scribe.oauth.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,5 +21,5 @@ public interface Api
|
|||||||
*
|
*
|
||||||
* @return fully configured {@link OAuthService}
|
* @return fully configured {@link OAuthService}
|
||||||
*/
|
*/
|
||||||
OAuthService createService(String apiKey, String apiSecret, String callback, String scope);
|
OAuthService createService(OAuthConfig config);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
|
import org.scribe.model.*;
|
||||||
|
|
||||||
|
public class ConstantContactApi extends DefaultApi10a
|
||||||
|
{
|
||||||
|
private static final String AUTHORIZE_URL = "https://oauth.constantcontact.com/ws/oauth/confirm_access?oauth_token=%s";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAccessTokenEndpoint()
|
||||||
|
{
|
||||||
|
return "https://oauth.constantcontact.com/ws/oauth/access_token";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthorizationUrl(Token requestToken)
|
||||||
|
{
|
||||||
|
return String.format(AUTHORIZE_URL, requestToken.getToken());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getRequestTokenEndpoint()
|
||||||
|
{
|
||||||
|
return "https://oauth.constantcontact.com/ws/oauth/request_token";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,52 +8,97 @@ import org.scribe.services.*;
|
|||||||
/**
|
/**
|
||||||
* Default implementation of the OAuth protocol, version 1.0a
|
* Default implementation of the OAuth protocol, version 1.0a
|
||||||
*
|
*
|
||||||
* This class is meant to be extended by concrete implementations of the API.
|
* This class is meant to be extended by concrete implementations of the API,
|
||||||
|
* providing the endpoints and endpoint-http-verbs.
|
||||||
|
*
|
||||||
* If your Api adheres to the 1.0a protocol correctly, you just need to extend
|
* If your Api adheres to the 1.0a protocol correctly, you just need to extend
|
||||||
* this class and define the getters for your endpoints.
|
* this class and define the getters for your endpoints.
|
||||||
*
|
*
|
||||||
|
* If your Api does something a bit different, you can override the different
|
||||||
|
* extractors or services, in order to fine-tune the process. Please read the
|
||||||
|
* javadocs of the interfaces to get an idea of what to do.
|
||||||
|
*
|
||||||
* @author Pablo Fernandez
|
* @author Pablo Fernandez
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public abstract class DefaultApi10a implements Api
|
public abstract class DefaultApi10a implements Api
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
protected AccessTokenExtractor getAccessTokenExtractor()
|
* Returns the access token extractor.
|
||||||
|
*
|
||||||
|
* @return access token extractor
|
||||||
|
*/
|
||||||
|
public AccessTokenExtractor getAccessTokenExtractor()
|
||||||
{
|
{
|
||||||
return new TokenExtractorImpl();
|
return new TokenExtractorImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected BaseStringExtractor getBaseStringExtractor()
|
/**
|
||||||
|
* Returns the base string extractor.
|
||||||
|
*
|
||||||
|
* @return base string extractor
|
||||||
|
*/
|
||||||
|
public BaseStringExtractor getBaseStringExtractor()
|
||||||
{
|
{
|
||||||
return new BaseStringExtractorImpl();
|
return new BaseStringExtractorImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected HeaderExtractor getHeaderExtractor()
|
/**
|
||||||
|
* Returns the header extractor.
|
||||||
|
*
|
||||||
|
* @return header extractor
|
||||||
|
*/
|
||||||
|
public HeaderExtractor getHeaderExtractor()
|
||||||
{
|
{
|
||||||
return new HeaderExtractorImpl();
|
return new HeaderExtractorImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected RequestTokenExtractor getRequestTokenExtractor()
|
/**
|
||||||
|
* Returns the request token extractor.
|
||||||
|
*
|
||||||
|
* @return request token extractor
|
||||||
|
*/
|
||||||
|
public RequestTokenExtractor getRequestTokenExtractor()
|
||||||
{
|
{
|
||||||
return new TokenExtractorImpl();
|
return new TokenExtractorImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected SignatureService getSignatureService()
|
/**
|
||||||
|
* Returns the signature service.
|
||||||
|
*
|
||||||
|
* @return signature service
|
||||||
|
*/
|
||||||
|
public SignatureService getSignatureService()
|
||||||
{
|
{
|
||||||
return new HMACSha1SignatureService();
|
return new HMACSha1SignatureService();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected TimestampService getTimestampService()
|
/**
|
||||||
|
* Returns the timestamp service.
|
||||||
|
*
|
||||||
|
* @return timestamp service
|
||||||
|
*/
|
||||||
|
public TimestampService getTimestampService()
|
||||||
{
|
{
|
||||||
return new TimestampServiceImpl();
|
return new TimestampServiceImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Verb getAccessTokenVerb()
|
/**
|
||||||
|
* Returns the verb for the access token endpoint (defaults to POST)
|
||||||
|
*
|
||||||
|
* @return access token endpoint verb
|
||||||
|
*/
|
||||||
|
public Verb getAccessTokenVerb()
|
||||||
{
|
{
|
||||||
return Verb.POST;
|
return Verb.POST;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Verb getRequestTokenVerb()
|
/**
|
||||||
|
* Returns the verb for the request token endpoint (defaults to POST)
|
||||||
|
*
|
||||||
|
* @return request token endpoint verb
|
||||||
|
*/
|
||||||
|
public Verb getRequestTokenVerb()
|
||||||
{
|
{
|
||||||
return Verb.POST;
|
return Verb.POST;
|
||||||
}
|
}
|
||||||
@@ -63,14 +108,23 @@ public abstract class DefaultApi10a implements Api
|
|||||||
*
|
*
|
||||||
* @return request token URL
|
* @return request token URL
|
||||||
*/
|
*/
|
||||||
protected abstract String getRequestTokenEndpoint();
|
public abstract String getRequestTokenEndpoint();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the URL that receives the access token requests.
|
* Returns the URL that receives the access token requests.
|
||||||
*
|
*
|
||||||
* @return access token URL
|
* @return access token URL
|
||||||
*/
|
*/
|
||||||
protected abstract String getAccessTokenEndpoint();
|
public abstract String getAccessTokenEndpoint();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the URL where you should redirect your users to authenticate
|
||||||
|
* your application.
|
||||||
|
*
|
||||||
|
* @param requestToken the request token you need to authorize
|
||||||
|
* @return the URL where you should redirect your users
|
||||||
|
*/
|
||||||
|
public abstract String getAuthorizationUrl(Token requestToken);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the {@link OAuthService} for this Api
|
* Returns the {@link OAuthService} for this Api
|
||||||
@@ -80,35 +134,8 @@ public abstract class DefaultApi10a implements Api
|
|||||||
* @param callback OAuth callback (either URL or 'oob')
|
* @param callback OAuth callback (either URL or 'oob')
|
||||||
* @param scope OAuth scope (optional)
|
* @param scope OAuth scope (optional)
|
||||||
*/
|
*/
|
||||||
public OAuthService createService(String apiKey, String apiSecret, String callback, String scope)
|
public OAuthService createService(OAuthConfig config)
|
||||||
{
|
{
|
||||||
OAuthService service = createService(apiKey, apiSecret, callback);
|
return new OAuth10aServiceImpl(this, config);
|
||||||
service.addScope(scope);
|
|
||||||
return service;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private OAuthService createService(String apiKey, String apiSecret, String callback)
|
|
||||||
{
|
|
||||||
return new OAuth10aServiceImpl( getSignatureService(),
|
|
||||||
getTimestampService(),
|
|
||||||
getBaseStringExtractor(),
|
|
||||||
getHeaderExtractor(),
|
|
||||||
getRequestTokenExtractor(),
|
|
||||||
getAccessTokenExtractor(),
|
|
||||||
createConfig(apiKey, apiSecret, callback));
|
|
||||||
}
|
|
||||||
|
|
||||||
private OAuthConfig createConfig(String apiKey, String apiSecret, String callback)
|
|
||||||
{
|
|
||||||
OAuthConfig config = new OAuthConfig();
|
|
||||||
config.setRequestTokenVerb(getRequestTokenVerb());
|
|
||||||
config.setRequestTokenEndpoint(getRequestTokenEndpoint());
|
|
||||||
config.setAccessTokenVerb(getAccessTokenVerb());
|
|
||||||
config.setAccessTokenEndpoint(getAccessTokenEndpoint());
|
|
||||||
config.setApiKey(apiKey);
|
|
||||||
config.setApiSecret(apiSecret);
|
|
||||||
config.setCallback(callback);
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
|
import org.scribe.extractors.*;
|
||||||
|
import org.scribe.model.*;
|
||||||
|
import org.scribe.oauth.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default implementation of the OAuth protocol, version 2.0 (draft 11)
|
||||||
|
*
|
||||||
|
* This class is meant to be extended by concrete implementations of the API,
|
||||||
|
* providing the endpoints and endpoint-http-verbs.
|
||||||
|
*
|
||||||
|
* If your Api adheres to the 2.0 (draft 11) protocol correctly, you just need to extend
|
||||||
|
* this class and define the getters for your endpoints.
|
||||||
|
*
|
||||||
|
* If your Api does something a bit different, you can override the different
|
||||||
|
* extractors or services, in order to fine-tune the process. Please read the
|
||||||
|
* javadocs of the interfaces to get an idea of what to do.
|
||||||
|
*
|
||||||
|
* @author Diego Silveira
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public abstract class DefaultApi20 implements Api
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the access token extractor.
|
||||||
|
*
|
||||||
|
* @return access token extractor
|
||||||
|
*/
|
||||||
|
public AccessTokenExtractor getAccessTokenExtractor()
|
||||||
|
{
|
||||||
|
return new TokenExtractor20Impl();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the verb for the access token endpoint (defaults to GET)
|
||||||
|
*
|
||||||
|
* @return access token endpoint verb
|
||||||
|
*/
|
||||||
|
public Verb getAccessTokenVerb()
|
||||||
|
{
|
||||||
|
return Verb.GET;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the URL that receives the access token requests.
|
||||||
|
*
|
||||||
|
* @return access token URL
|
||||||
|
*/
|
||||||
|
public abstract String getAccessTokenEndpoint();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the URL where you should redirect your users to authenticate
|
||||||
|
* your application.
|
||||||
|
*
|
||||||
|
* @param config OAuth 2.0 configuration param object
|
||||||
|
* @return the URL where you should redirect your users
|
||||||
|
*/
|
||||||
|
public abstract String getAuthorizationUrl(OAuthConfig config);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public OAuthService createService(OAuthConfig config)
|
||||||
|
{
|
||||||
|
return new OAuth20ServiceImpl(this, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
|
import org.scribe.model.*;
|
||||||
|
|
||||||
|
public class DropBoxApi extends DefaultApi10a
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public String getAccessTokenEndpoint()
|
||||||
|
{
|
||||||
|
return "https://api.dropbox.com/0/oauth/access_token";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthorizationUrl(Token requestToken)
|
||||||
|
{
|
||||||
|
return "https://www.dropbox.com/0/oauth/authorize?oauth_token="+requestToken.getToken();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getRequestTokenEndpoint()
|
||||||
|
{
|
||||||
|
return "https://api.dropbox.com/0/oauth/request_token";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,7 +1,17 @@
|
|||||||
package org.scribe.builder.api;
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
|
import org.scribe.model.Token;
|
||||||
|
import org.scribe.model.Verb;
|
||||||
|
|
||||||
public class EvernoteApi extends DefaultApi10a
|
public class EvernoteApi extends DefaultApi10a
|
||||||
{
|
{
|
||||||
|
private static final String AUTHORIZATION_URL = "https://www.evernote.com/OAuth.action?oauth_token=%s";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Verb getRequestTokenVerb()
|
||||||
|
{
|
||||||
|
return Verb.GET;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getRequestTokenEndpoint()
|
public String getRequestTokenEndpoint()
|
||||||
@@ -10,9 +20,43 @@ public class EvernoteApi extends DefaultApi10a
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAccessTokenEndpoint()
|
public Verb getAccessTokenVerb()
|
||||||
{
|
{
|
||||||
return "https://www.evernote.com/oauth";
|
return Verb.GET;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAccessTokenEndpoint()
|
||||||
|
{
|
||||||
|
return "https://www.evernote.com/oauth";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthorizationUrl(Token requestToken)
|
||||||
|
{
|
||||||
|
return String.format(AUTHORIZATION_URL, requestToken.getToken());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Sandbox extends EvernoteApi
|
||||||
|
{
|
||||||
|
private static final String SANDBOX_URL = "https://sandbox.evernote.com/oauth";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getRequestTokenEndpoint()
|
||||||
|
{
|
||||||
|
return SANDBOX_URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAccessTokenEndpoint()
|
||||||
|
{
|
||||||
|
return SANDBOX_URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthorizationUrl(Token requestToken)
|
||||||
|
{
|
||||||
|
return String.format(SANDBOX_URL + "?oauth_token=%s", requestToken.getToken());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +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");
|
||||||
|
|
||||||
|
// 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,29 @@
|
|||||||
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
|
import org.scribe.extractors.*;
|
||||||
|
import org.scribe.model.*;
|
||||||
|
import org.scribe.utils.*;
|
||||||
|
|
||||||
|
public class Foursquare2Api extends DefaultApi20
|
||||||
|
{
|
||||||
|
private static final String AUTHORIZATION_URL = "https://foursquare.com/oauth2/authenticate?client_id=%s&response_type=code&redirect_uri=%s";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAccessTokenEndpoint()
|
||||||
|
{
|
||||||
|
return "https://foursquare.com/oauth2/access_token?grant_type=authorization_code";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthorizationUrl(OAuthConfig config)
|
||||||
|
{
|
||||||
|
Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. Foursquare2 does not support OOB");
|
||||||
|
return String.format(AUTHORIZATION_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AccessTokenExtractor getAccessTokenExtractor()
|
||||||
|
{
|
||||||
|
return new JsonTokenExtractor();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +1,26 @@
|
|||||||
package org.scribe.builder.api;
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
|
import org.scribe.model.Token;
|
||||||
|
|
||||||
public class FoursquareApi extends DefaultApi10a
|
public class FoursquareApi extends DefaultApi10a
|
||||||
{
|
{
|
||||||
|
private static final String AUTHORIZATION_URL = "http://foursquare.com/oauth/authorize?oauth_token=%s";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getAccessTokenEndpoint()
|
public String getAccessTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "http://foursquare.com/oauth/access_token";
|
return "http://foursquare.com/oauth/access_token";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getRequestTokenEndpoint()
|
public String getRequestTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "http://foursquare.com/oauth/request_token";
|
return "http://foursquare.com/oauth/request_token";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthorizationUrl(Token requestToken)
|
||||||
|
{
|
||||||
|
return String.format(AUTHORIZATION_URL, requestToken.getToken());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,38 @@
|
|||||||
package org.scribe.builder.api;
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
|
import org.scribe.model.*;
|
||||||
|
|
||||||
public class GoogleApi extends DefaultApi10a
|
public class GoogleApi extends DefaultApi10a
|
||||||
{
|
{
|
||||||
|
private static final String AUTHORIZATION_URL = "https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=%s";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getAccessTokenEndpoint()
|
public String getAccessTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "https://www.google.com/accounts/OAuthGetAccessToken";
|
return "https://www.google.com/accounts/OAuthGetAccessToken";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getRequestTokenEndpoint()
|
public String getRequestTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "https://www.google.com/accounts/OAuthGetRequestToken";
|
return "https://www.google.com/accounts/OAuthGetRequestToken";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Verb getAccessTokenVerb()
|
||||||
|
{
|
||||||
|
return Verb.GET;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Verb getRequestTokenVerb()
|
||||||
|
{
|
||||||
|
return Verb.GET;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthorizationUrl(Token requestToken)
|
||||||
|
{
|
||||||
|
return String.format(AUTHORIZATION_URL, requestToken.getToken());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,27 +1,27 @@
|
|||||||
package org.scribe.builder.api;
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
import org.scribe.extensions.linkedin.*;
|
import org.scribe.model.*;
|
||||||
import org.scribe.extractors.*;
|
|
||||||
|
|
||||||
public class LinkedInApi extends DefaultApi10a
|
public class LinkedInApi extends DefaultApi10a
|
||||||
{
|
{
|
||||||
|
private static final String AUTHORIZE_URL = "https://api.linkedin.com/uas/oauth/authorize?oauth_token=%s";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getAccessTokenEndpoint()
|
public String getAccessTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "https://api.linkedin.com/uas/oauth/accessToken";
|
return "https://api.linkedin.com/uas/oauth/accessToken";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getRequestTokenEndpoint()
|
public String getRequestTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "https://api.linkedin.com/uas/oauth/requestToken";
|
return "https://api.linkedin.com/uas/oauth/requestToken";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected BaseStringExtractor getBaseStringExtractor()
|
public String getAuthorizationUrl(Token requestToken)
|
||||||
{
|
{
|
||||||
return new LinkedInBaseStringExtractorImpl();
|
return String.format(AUTHORIZE_URL, requestToken.getToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
|
import org.scribe.model.Token;
|
||||||
|
|
||||||
|
public class LoveFilmApi extends DefaultApi10a
|
||||||
|
{
|
||||||
|
private static final String REQUEST_TOKEN_URL = "http://openapi.lovefilm.com/oauth/request_token";
|
||||||
|
private static final String ACCESS_TOKEN_URL = "http://openapi.lovefilm.com/oauth/access_token";
|
||||||
|
private static final String AUTHORIZE_URL = "https://www.lovefilm.com/activate?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,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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +1,43 @@
|
|||||||
package org.scribe.builder.api;
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
|
import org.scribe.model.Token;
|
||||||
|
|
||||||
public class TwitterApi extends DefaultApi10a
|
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
|
@Override
|
||||||
protected String getAccessTokenEndpoint()
|
public String getAccessTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "http://twitter.com/oauth/access_token";
|
return "http://" + ACCESS_TOKEN_RESOURCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getRequestTokenEndpoint()
|
public String getRequestTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "http://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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,26 @@
|
|||||||
package org.scribe.builder.api;
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
|
import org.scribe.model.Token;
|
||||||
|
|
||||||
public class VimeoApi extends DefaultApi10a
|
public class VimeoApi extends DefaultApi10a
|
||||||
{
|
{
|
||||||
|
private static final String AUTHORIZATION_URL = "http://vimeo.com/oauth/authorize?oauth_token=%s";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getAccessTokenEndpoint()
|
public String getAccessTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "http://vimeo.com/oauth/access_token";
|
return "http://vimeo.com/oauth/access_token";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getRequestTokenEndpoint()
|
public String getRequestTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "http://vimeo.com/oauth/request_token";
|
return "http://vimeo.com/oauth/request_token";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthorizationUrl(Token requestToken)
|
||||||
|
{
|
||||||
|
return String.format(AUTHORIZATION_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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +1,26 @@
|
|||||||
package org.scribe.builder.api;
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
|
import org.scribe.model.Token;
|
||||||
|
|
||||||
public class YahooApi extends DefaultApi10a
|
public class YahooApi extends DefaultApi10a
|
||||||
{
|
{
|
||||||
|
private static final String AUTHORIZE_URL = "https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token=%s";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getAccessTokenEndpoint()
|
public String getAccessTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "https://api.login.yahoo.com/oauth/v2/get_token";
|
return "https://api.login.yahoo.com/oauth/v2/get_token";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getRequestTokenEndpoint()
|
public String getRequestTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "https://api.login.yahoo.com/oauth/v2/get_request_token";
|
return "https://api.login.yahoo.com/oauth/v2/get_request_token";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthorizationUrl(Token requestToken)
|
||||||
|
{
|
||||||
|
return String.format(AUTHORIZE_URL, requestToken.getToken());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,18 +1,33 @@
|
|||||||
package org.scribe.builder.api;
|
package org.scribe.builder.api;
|
||||||
|
|
||||||
|
import org.scribe.model.*;
|
||||||
|
import org.scribe.services.*;
|
||||||
|
|
||||||
public class YammerApi extends DefaultApi10a
|
public class YammerApi extends DefaultApi10a
|
||||||
{
|
{
|
||||||
|
private static final String AUTHORIZATION_URL = "'https://www.yammer.com/oauth/authorize?oauth_token=%s'";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getRequestTokenEndpoint()
|
public String getRequestTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "https://www.yammer.com/oauth/request_token";
|
return "https://www.yammer.com/oauth/request_token";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAccessTokenEndpoint()
|
public String getAccessTokenEndpoint()
|
||||||
{
|
{
|
||||||
return "https://www.yammer.com/oauth/access_token";
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package org.scribe.extensions.linkedin;
|
|
||||||
|
|
||||||
import org.scribe.extractors.*;
|
|
||||||
import org.scribe.model.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* LinkedIn's specific extractor that does not encode the tilde character.
|
|
||||||
*
|
|
||||||
* @author Pablo Fernandez
|
|
||||||
*/
|
|
||||||
public class LinkedInBaseStringExtractorImpl extends BaseStringExtractorImpl
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public String extract(OAuthRequest request)
|
|
||||||
{
|
|
||||||
String baseString = super.extract(request);
|
|
||||||
return baseString.replace("%7E", "~");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -32,11 +32,11 @@ public class BaseStringExtractorImpl implements BaseStringExtractor
|
|||||||
private String getSortedAndEncodedParams(OAuthRequest request)
|
private String getSortedAndEncodedParams(OAuthRequest request)
|
||||||
{
|
{
|
||||||
Map<String, String> params = new HashMap<String, String>();
|
Map<String, String> params = new HashMap<String, String>();
|
||||||
params.putAll(request.getQueryStringParams());
|
MapUtils.decodeAndAppendEntries(request.getQueryStringParams(), params);
|
||||||
params.putAll(request.getBodyParams());
|
MapUtils.decodeAndAppendEntries(request.getBodyParams(), params);
|
||||||
params.putAll(request.getOauthParameters());
|
MapUtils.decodeAndAppendEntries(request.getOauthParameters(), params);
|
||||||
params = MapUtils.sort(params);
|
params = MapUtils.sort(params);
|
||||||
return URLUtils.percentEncode(URLUtils.formURLEncodeMap(params));
|
return URLUtils.percentEncode(URLUtils.concatSortedPercentEncodedParams(params));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkPreconditions(OAuthRequest request)
|
private void checkPreconditions(OAuthRequest request)
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package org.scribe.extractors;
|
||||||
|
|
||||||
|
import java.util.regex.*;
|
||||||
|
|
||||||
|
import org.scribe.exceptions.*;
|
||||||
|
import org.scribe.model.*;
|
||||||
|
import org.scribe.utils.*;
|
||||||
|
|
||||||
|
public class JsonTokenExtractor implements AccessTokenExtractor
|
||||||
|
{
|
||||||
|
private Pattern accessTokenPattern = Pattern.compile("\"access_token\":\"(\\S*?)\"");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Token extract(String response)
|
||||||
|
{
|
||||||
|
Preconditions.checkEmptyString(response, "Cannot extract a token from a null or empty String");
|
||||||
|
Matcher matcher = accessTokenPattern.matcher(response);
|
||||||
|
if(matcher.find())
|
||||||
|
{
|
||||||
|
return new Token(matcher.group(1), "", response);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new OAuthException("Cannot extract an acces token. Response was: " + response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.scribe.extractors;
|
||||||
|
|
||||||
|
import java.util.regex.*;
|
||||||
|
|
||||||
|
import org.scribe.exceptions.*;
|
||||||
|
import org.scribe.model.*;
|
||||||
|
import org.scribe.utils.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default implementation of {@AccessTokenExtractor}. Conforms to OAuth 2.0
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class TokenExtractor20Impl implements AccessTokenExtractor
|
||||||
|
{
|
||||||
|
private static final String TOKEN_REGEX = "access_token=(\\S*?)(&(\\S*))?";
|
||||||
|
private static final String EMPTY_SECRET = "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public Token extract(String response)
|
||||||
|
{
|
||||||
|
Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string");
|
||||||
|
|
||||||
|
Matcher matcher = Pattern.compile(TOKEN_REGEX).matcher(response);
|
||||||
|
if (matcher.matches())
|
||||||
|
{
|
||||||
|
String token = URLUtils.formURLDecode(matcher.group(1));
|
||||||
|
return new Token(token, EMPTY_SECRET, response);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new OAuthException("Response body is incorrect. Can't extract a token from this: '" + response + "'", null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,24 +15,30 @@ import org.scribe.utils.*;
|
|||||||
*/
|
*/
|
||||||
public class TokenExtractorImpl implements RequestTokenExtractor, AccessTokenExtractor
|
public class TokenExtractorImpl implements RequestTokenExtractor, AccessTokenExtractor
|
||||||
{
|
{
|
||||||
private static final String TOKEN_REGEX = "oauth_token=(\\S*)&oauth_token_secret=(\\S*?)(&(.*))?";
|
private static final Pattern TOKEN_REGEX = Pattern.compile("oauth_token=([^&]+)");
|
||||||
|
private static final Pattern SECRET_REGEX = Pattern.compile("oauth_token_secret=([^&]+)");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
public Token extract(String response)
|
public Token extract(String response)
|
||||||
{
|
{
|
||||||
Preconditions.checkEmptyString(response, "Cant extract a token from null object or an empty string.");
|
Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string");
|
||||||
|
String token = extract(response, TOKEN_REGEX);
|
||||||
|
String secret = extract(response, SECRET_REGEX);
|
||||||
|
return new Token(token, secret, response);
|
||||||
|
}
|
||||||
|
|
||||||
Matcher matcher = Pattern.compile(TOKEN_REGEX).matcher(response);
|
private String extract(String response, Pattern p)
|
||||||
if (matcher.matches())
|
{
|
||||||
|
Matcher matcher = p.matcher(response);
|
||||||
|
if (matcher.find() && matcher.groupCount() >= 1)
|
||||||
{
|
{
|
||||||
String token = URLUtils.percentDecode(matcher.group(1));
|
return URLUtils.formURLDecode(matcher.group(1));
|
||||||
String secret = URLUtils.percentDecode(matcher.group(2));
|
}
|
||||||
return new Token(token, secret);
|
else
|
||||||
} else
|
|
||||||
{
|
{
|
||||||
throw new OAuthException("Could not find request token or secret in response: " + response, null);
|
throw new OAuthException("Response body is incorrect. Can't extract token and secret from this: '" + response + "'", null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,82 +7,53 @@ package org.scribe.model;
|
|||||||
*/
|
*/
|
||||||
public class OAuthConfig
|
public class OAuthConfig
|
||||||
{
|
{
|
||||||
private String apiKey;
|
private final String apiKey;
|
||||||
private String apiSecret;
|
private final String apiSecret;
|
||||||
private String callback;
|
private final String callback;
|
||||||
private String requestTokenEndpoint;
|
private final SignatureType signatureType;
|
||||||
private String accessTokenEndpoint;
|
private final String scope;
|
||||||
private Verb requestTokenVerb;
|
|
||||||
private Verb accessTokenVerb;
|
public OAuthConfig(String key, String secret)
|
||||||
|
{
|
||||||
|
this(key, secret, null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OAuthConfig(String key, String secret, String callback, SignatureType type, String scope)
|
||||||
|
{
|
||||||
|
this.apiKey = key;
|
||||||
|
this.apiSecret = secret;
|
||||||
|
this.callback = callback != null ? callback : OAuthConstants.OUT_OF_BAND;
|
||||||
|
this.signatureType = (type != null) ? type : SignatureType.Header;
|
||||||
|
this.scope = scope;
|
||||||
|
}
|
||||||
|
|
||||||
public String getApiKey()
|
public String getApiKey()
|
||||||
{
|
{
|
||||||
return apiKey;
|
return apiKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setApiKey(String apiKey)
|
|
||||||
{
|
|
||||||
this.apiKey = apiKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getApiSecret()
|
public String getApiSecret()
|
||||||
{
|
{
|
||||||
return apiSecret;
|
return apiSecret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setApiSecret(String apiSecret)
|
|
||||||
{
|
|
||||||
this.apiSecret = apiSecret;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getCallback()
|
public String getCallback()
|
||||||
{
|
{
|
||||||
return callback != null ? callback : OAuthConstants.OUT_OF_BAND;
|
return callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCallback(String callback)
|
public SignatureType getSignatureType()
|
||||||
{
|
{
|
||||||
this.callback = callback;
|
return signatureType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRequestTokenEndpoint()
|
public String getScope()
|
||||||
{
|
{
|
||||||
return requestTokenEndpoint;
|
return scope;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRequestTokenEndpoint(String requestTokenEndpoint)
|
public boolean hasScope()
|
||||||
{
|
{
|
||||||
this.requestTokenEndpoint = requestTokenEndpoint;
|
return scope != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAccessTokenEndpoint()
|
|
||||||
{
|
|
||||||
return accessTokenEndpoint;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAccessTokenEndpoint(String accessTokenEndpoint)
|
|
||||||
{
|
|
||||||
this.accessTokenEndpoint = accessTokenEndpoint;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Verb getRequestTokenVerb()
|
|
||||||
{
|
|
||||||
return requestTokenVerb != null ? requestTokenVerb : Verb.GET;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRequestTokenVerb(Verb requestTokenVerb)
|
|
||||||
{
|
|
||||||
this.requestTokenVerb = requestTokenVerb;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Verb getAccessTokenVerb()
|
|
||||||
{
|
|
||||||
return accessTokenVerb != null ? accessTokenVerb : Verb.GET;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAccessTokenVerb(Verb accessTokenVerb)
|
|
||||||
{
|
|
||||||
this.accessTokenVerb = accessTokenVerb;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -38,4 +38,12 @@ public class OAuthConstants
|
|||||||
public static final String HEADER = "Authorization";
|
public static final String HEADER = "Authorization";
|
||||||
public static final Token EMPTY_TOKEN = new Token("", "");
|
public static final Token EMPTY_TOKEN = new Token("", "");
|
||||||
public static final String SCOPE = "scope";
|
public static final String SCOPE = "scope";
|
||||||
|
|
||||||
|
//OAuth 2.0
|
||||||
|
public static final String ACCESS_TOKEN = "access_token";
|
||||||
|
public static final String CLIENT_ID = "client_id";
|
||||||
|
public static final String CLIENT_SECRET = "client_secret";
|
||||||
|
public static final String REDIRECT_URI = "redirect_uri";
|
||||||
|
public static final String CODE = "code";
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,9 @@ 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 org.scribe.exceptions.*;
|
import org.scribe.exceptions.*;
|
||||||
import org.scribe.utils.*;
|
import org.scribe.utils.*;
|
||||||
@@ -18,11 +20,14 @@ class Request
|
|||||||
|
|
||||||
private String url;
|
private String url;
|
||||||
private Verb verb;
|
private Verb verb;
|
||||||
|
private Map<String, String> querystringParams;
|
||||||
private Map<String, String> bodyParams;
|
private Map<String, String> bodyParams;
|
||||||
private Map<String, String> headers;
|
private Map<String, String> headers;
|
||||||
private String payload = null;
|
private String payload = null;
|
||||||
private Integer timeout = null;
|
|
||||||
private HttpURLConnection connection;
|
private HttpURLConnection connection;
|
||||||
|
private String charset;
|
||||||
|
private byte[] bytePayload = null;
|
||||||
|
private boolean connectionKeepAlive = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Http Request
|
* Creates a new Http Request
|
||||||
@@ -34,16 +39,9 @@ class Request
|
|||||||
{
|
{
|
||||||
this.verb = verb;
|
this.verb = verb;
|
||||||
this.url = url;
|
this.url = url;
|
||||||
|
this.querystringParams = new HashMap<String, String>();
|
||||||
this.bodyParams = new HashMap<String, String>();
|
this.bodyParams = new HashMap<String, String>();
|
||||||
this.headers = new HashMap<String, String>();
|
this.headers = new HashMap<String, String>();
|
||||||
try
|
|
||||||
{
|
|
||||||
connection = (HttpURLConnection) new URL(url).openConnection();
|
|
||||||
if(timeout != null) connection.setConnectTimeout(timeout);
|
|
||||||
} catch (IOException ioe)
|
|
||||||
{
|
|
||||||
throw new OAuthException("Could not open connection to: " + url, ioe);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,6 +55,7 @@ class Request
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
createConnection();
|
||||||
return doSend();
|
return doSend();
|
||||||
} catch (IOException ioe)
|
} catch (IOException ioe)
|
||||||
{
|
{
|
||||||
@@ -64,13 +63,23 @@ class Request
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void createConnection() throws IOException
|
||||||
|
{
|
||||||
|
String effectiveUrl = URLUtils.appendParametersToQueryString(url, querystringParams);
|
||||||
|
if (connection == null)
|
||||||
|
{
|
||||||
|
System.setProperty("http.keepAlive", connectionKeepAlive ? "true" : "false");
|
||||||
|
connection = (HttpURLConnection) new URL(effectiveUrl).openConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Response doSend() throws IOException
|
Response doSend() throws IOException
|
||||||
{
|
{
|
||||||
connection.setRequestMethod(this.verb.name());
|
connection.setRequestMethod(this.verb.name());
|
||||||
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);
|
||||||
}
|
}
|
||||||
@@ -81,18 +90,18 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add an HTTP Header to the Request
|
* Add an HTTP Header to the Request
|
||||||
*
|
*
|
||||||
* @param name
|
* @param key the header name
|
||||||
* @param value
|
* @param value the header value
|
||||||
*/
|
*/
|
||||||
public void addHeader(String key, String value)
|
public void addHeader(String key, String value)
|
||||||
{
|
{
|
||||||
@@ -102,14 +111,25 @@ class Request
|
|||||||
/**
|
/**
|
||||||
* Add a body Parameter (for POST/ PUT Requests)
|
* Add a body Parameter (for POST/ PUT Requests)
|
||||||
*
|
*
|
||||||
* @param name
|
* @param key the parameter name
|
||||||
* @param value
|
* @param value the parameter value
|
||||||
*/
|
*/
|
||||||
public void addBodyParameter(String key, String value)
|
public void addBodyParameter(String key, String value)
|
||||||
{
|
{
|
||||||
this.bodyParams.put(key, value);
|
this.bodyParams.put(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a QueryString parameter
|
||||||
|
*
|
||||||
|
* @param key the parameter name
|
||||||
|
* @param value the parameter value
|
||||||
|
*/
|
||||||
|
public void addQuerystringParameter(String key, String value)
|
||||||
|
{
|
||||||
|
this.querystringParams.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add body payload.
|
* Add body payload.
|
||||||
*
|
*
|
||||||
@@ -118,34 +138,40 @@ class Request
|
|||||||
*
|
*
|
||||||
* Note: The contents are not part of the OAuth signature
|
* Note: The contents are not part of the OAuth signature
|
||||||
*
|
*
|
||||||
* @param payload
|
* @param payload the body of the request
|
||||||
*/
|
*/
|
||||||
public void addPayload(String payload)
|
public void addPayload(String payload)
|
||||||
{
|
{
|
||||||
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.
|
||||||
*
|
*
|
||||||
* @return a map containing the query string parameters
|
* @return a map containing the query string parameters
|
||||||
|
* @throws OAuthException if the URL is not valid
|
||||||
*/
|
*/
|
||||||
public Map<String, String> getQueryStringParams()
|
public Map<String, String> getQueryStringParams()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Map<String, String> params = new HashMap<String, String>();
|
Map<String, String> params = new HashMap<String, String>();
|
||||||
String query = new URL(url).getQuery();
|
String queryString = new URL(url).getQuery();
|
||||||
if (query != null)
|
params.putAll(URLUtils.queryStringToMap(queryString));
|
||||||
{
|
params.putAll(this.querystringParams);
|
||||||
for (String param : query.split("&"))
|
|
||||||
{
|
|
||||||
String pair[] = param.split("=");
|
|
||||||
params.put(pair[0], pair[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return params;
|
return params;
|
||||||
} catch (MalformedURLException mue)
|
}
|
||||||
|
catch (MalformedURLException mue)
|
||||||
{
|
{
|
||||||
throw new OAuthException("Malformed URL", mue);
|
throw new OAuthException("Malformed URL", mue);
|
||||||
}
|
}
|
||||||
@@ -185,10 +211,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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -212,15 +260,60 @@ class Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the connection timeout in milliseconds for the underlying {@link HttpURLConnection}
|
* Returns the connection charset. Defaults to {@link Charset} defaultCharset if not set
|
||||||
*
|
*
|
||||||
* @param timeout in milliseconds
|
* @return charset
|
||||||
*/
|
*/
|
||||||
public void setTimeout(int timeout)
|
public String getCharset()
|
||||||
{
|
{
|
||||||
this.timeout = timeout;
|
return charset == null ? Charset.defaultCharset().name() : charset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the connect timeout for the underlying {@link HttpURLConnection}
|
||||||
|
*
|
||||||
|
* @param duration duration of the timeout
|
||||||
|
*
|
||||||
|
* @param unit unit of time (milliseconds, seconds, etc)
|
||||||
|
*/
|
||||||
|
public void setConnectTimeout(int duration, TimeUnit unit)
|
||||||
|
{
|
||||||
|
this.connection.setConnectTimeout((int) unit.toMillis(duration));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the read timeout for the underlying {@link HttpURLConnection}
|
||||||
|
*
|
||||||
|
* @param duration duration of the timeout
|
||||||
|
*
|
||||||
|
* @param unit unit of time (milliseconds, seconds, etc)
|
||||||
|
*/
|
||||||
|
public void setReadTimeout(int duration, TimeUnit unit)
|
||||||
|
{
|
||||||
|
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
|
* We need this in order to stub the connection object for test cases
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.scribe.model;
|
||||||
|
|
||||||
|
public enum SignatureType
|
||||||
|
{
|
||||||
|
Header,
|
||||||
|
QueryString
|
||||||
|
}
|
||||||
@@ -1,14 +1,19 @@
|
|||||||
package org.scribe.model;
|
package org.scribe.model;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an OAuth token (either request or access token) and its secret
|
* Represents an OAuth token (either request or access token) and its secret
|
||||||
*
|
*
|
||||||
* @author Pablo Fernandez
|
* @author Pablo Fernandez
|
||||||
*/
|
*/
|
||||||
public class Token
|
public class Token implements Serializable
|
||||||
{
|
{
|
||||||
|
private static final long serialVersionUID = 715000866082812683L;
|
||||||
|
|
||||||
private final String token;
|
private final String token;
|
||||||
private final String secret;
|
private final String secret;
|
||||||
|
private final String rawResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor
|
* Default constructor
|
||||||
@@ -17,9 +22,15 @@ public class Token
|
|||||||
* @param secret token secret
|
* @param secret token secret
|
||||||
*/
|
*/
|
||||||
public Token(String token, String secret)
|
public Token(String token, String secret)
|
||||||
|
{
|
||||||
|
this(token, secret, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Token(String token, String secret, String rawResponse)
|
||||||
{
|
{
|
||||||
this.token = token;
|
this.token = token;
|
||||||
this.secret = secret;
|
this.secret = secret;
|
||||||
|
this.rawResponse = rawResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getToken()
|
public String getToken()
|
||||||
@@ -32,6 +43,15 @@ public class Token
|
|||||||
return secret;
|
return secret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getRawResponse()
|
||||||
|
{
|
||||||
|
if (rawResponse == null)
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("This token object was not constructed by scribe and does not have a rawResponse");
|
||||||
|
}
|
||||||
|
return rawResponse;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString()
|
public String toString()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package org.scribe.model;
|
package org.scribe.model;
|
||||||
|
|
||||||
|
import org.scribe.utils.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an OAuth verifier code.
|
* Represents an OAuth verifier code.
|
||||||
*
|
*
|
||||||
@@ -7,6 +9,7 @@ package org.scribe.model;
|
|||||||
*/
|
*/
|
||||||
public class Verifier
|
public class Verifier
|
||||||
{
|
{
|
||||||
|
|
||||||
private final String value;
|
private final String value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,6 +19,7 @@ public class Verifier
|
|||||||
*/
|
*/
|
||||||
public Verifier(String value)
|
public Verifier(String value)
|
||||||
{
|
{
|
||||||
|
Preconditions.checkNotNull(value, "Must provide a valid string as verifier");
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package org.scribe.oauth;
|
package org.scribe.oauth;
|
||||||
|
|
||||||
import org.scribe.extractors.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import org.scribe.builder.api.*;
|
||||||
import org.scribe.model.*;
|
import org.scribe.model.*;
|
||||||
import org.scribe.services.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OAuth 1.0a implementation of {@link OAuthService}
|
* OAuth 1.0a implementation of {@link OAuthService}
|
||||||
@@ -11,40 +12,21 @@ import org.scribe.services.*;
|
|||||||
*/
|
*/
|
||||||
public class OAuth10aServiceImpl implements OAuthService
|
public class OAuth10aServiceImpl implements OAuthService
|
||||||
{
|
{
|
||||||
private static final String NO_SCOPE = null;
|
|
||||||
private static final String VERSION = "1.0";
|
private static final String VERSION = "1.0";
|
||||||
|
|
||||||
private OAuthConfig config;
|
private OAuthConfig config;
|
||||||
private SignatureService signatureService;
|
private DefaultApi10a api;
|
||||||
private TimestampService timestampService;
|
|
||||||
private BaseStringExtractor baseStringExtractor;
|
|
||||||
private HeaderExtractor headerExtractor;
|
|
||||||
private RequestTokenExtractor rtExtractor;
|
|
||||||
private AccessTokenExtractor atExtractor;
|
|
||||||
private String scope;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor
|
* Default constructor
|
||||||
*
|
*
|
||||||
* @param signatureService OAuth 1.0a signature service
|
* @param api OAuth1.0a api information
|
||||||
* @param timestampService OAuth 1.0a timestamp service
|
|
||||||
* @param baseStringExtractor OAuth 1.0a base string extractor
|
|
||||||
* @param headerExtractor OAuth 1.0a http header extractor
|
|
||||||
* @param rtExtractor OAuth 1.0a request token extractor
|
|
||||||
* @param atExtractor OAuth 1.0a access token extractor
|
|
||||||
* @param config OAuth 1.0a configuration param object
|
* @param config OAuth 1.0a configuration param object
|
||||||
*/
|
*/
|
||||||
public OAuth10aServiceImpl(SignatureService signatureService, TimestampService timestampService, BaseStringExtractor baseStringExtractor,
|
public OAuth10aServiceImpl(DefaultApi10a api, OAuthConfig config)
|
||||||
HeaderExtractor headerExtractor, RequestTokenExtractor rtExtractor, AccessTokenExtractor atExtractor, OAuthConfig config)
|
|
||||||
{
|
{
|
||||||
this.signatureService = signatureService;
|
this.api = api;
|
||||||
this.timestampService = timestampService;
|
|
||||||
this.baseStringExtractor = baseStringExtractor;
|
|
||||||
this.headerExtractor = headerExtractor;
|
|
||||||
this.rtExtractor = rtExtractor;
|
|
||||||
this.atExtractor = atExtractor;
|
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.scope = NO_SCOPE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -52,22 +34,22 @@ public class OAuth10aServiceImpl implements OAuthService
|
|||||||
*/
|
*/
|
||||||
public Token getRequestToken()
|
public Token getRequestToken()
|
||||||
{
|
{
|
||||||
OAuthRequest request = new OAuthRequest(config.getRequestTokenVerb(), config.getRequestTokenEndpoint());
|
OAuthRequest request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint());
|
||||||
|
request.addOAuthParameter(OAuthConstants.CALLBACK, config.getCallback());
|
||||||
addOAuthParams(request, OAuthConstants.EMPTY_TOKEN);
|
addOAuthParams(request, OAuthConstants.EMPTY_TOKEN);
|
||||||
addOAuthHeader(request);
|
addSignature(request);
|
||||||
Response response = request.send();
|
Response response = request.send();
|
||||||
return rtExtractor.extract(response.getBody());
|
return api.getRequestTokenExtractor().extract(response.getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addOAuthParams(OAuthRequest request, Token token)
|
private void addOAuthParams(OAuthRequest request, Token token)
|
||||||
{
|
{
|
||||||
request.addOAuthParameter(OAuthConstants.TIMESTAMP, timestampService.getTimestampInSeconds());
|
request.addOAuthParameter(OAuthConstants.TIMESTAMP, api.getTimestampService().getTimestampInSeconds());
|
||||||
request.addOAuthParameter(OAuthConstants.NONCE, timestampService.getNonce());
|
request.addOAuthParameter(OAuthConstants.NONCE, api.getTimestampService().getNonce());
|
||||||
request.addOAuthParameter(OAuthConstants.CONSUMER_KEY, config.getApiKey());
|
request.addOAuthParameter(OAuthConstants.CONSUMER_KEY, config.getApiKey());
|
||||||
request.addOAuthParameter(OAuthConstants.SIGN_METHOD, signatureService.getSignatureMethod());
|
request.addOAuthParameter(OAuthConstants.SIGN_METHOD, api.getSignatureService().getSignatureMethod());
|
||||||
request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
|
request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
|
||||||
request.addOAuthParameter(OAuthConstants.CALLBACK, config.getCallback());
|
if(config.hasScope()) request.addOAuthParameter(OAuthConstants.SCOPE, config.getScope());
|
||||||
if(scope != NO_SCOPE) request.addOAuthParameter(OAuthConstants.SCOPE, scope);
|
|
||||||
request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));
|
request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,13 +58,13 @@ public class OAuth10aServiceImpl implements OAuthService
|
|||||||
*/
|
*/
|
||||||
public Token getAccessToken(Token requestToken, Verifier verifier)
|
public Token getAccessToken(Token requestToken, Verifier verifier)
|
||||||
{
|
{
|
||||||
OAuthRequest request = new OAuthRequest(config.getAccessTokenVerb(), config.getAccessTokenEndpoint());
|
OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
|
||||||
request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken());
|
request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken());
|
||||||
request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue());
|
request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue());
|
||||||
addOAuthParams(request, requestToken);
|
addOAuthParams(request, requestToken);
|
||||||
addOAuthHeader(request);
|
addSignature(request);
|
||||||
Response response = request.send();
|
Response response = request.send();
|
||||||
return atExtractor.extract(response.getBody());
|
return api.getAccessTokenExtractor().extract(response.getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -92,7 +74,7 @@ public class OAuth10aServiceImpl implements OAuthService
|
|||||||
{
|
{
|
||||||
request.addOAuthParameter(OAuthConstants.TOKEN, token.getToken());
|
request.addOAuthParameter(OAuthConstants.TOKEN, token.getToken());
|
||||||
addOAuthParams(request, token);
|
addOAuthParams(request, token);
|
||||||
addOAuthHeader(request);
|
addSignature(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -106,20 +88,31 @@ public class OAuth10aServiceImpl implements OAuthService
|
|||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
public void addScope(String scope)
|
public String getAuthorizationUrl(Token requestToken)
|
||||||
{
|
{
|
||||||
this.scope = scope;
|
return api.getAuthorizationUrl(requestToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getSignature(OAuthRequest request, Token token)
|
private String getSignature(OAuthRequest request, Token token)
|
||||||
{
|
{
|
||||||
String baseString = baseStringExtractor.extract(request);
|
String baseString = api.getBaseStringExtractor().extract(request);
|
||||||
return signatureService.getSignature(baseString, config.getApiSecret(), token.getSecret());
|
return api.getSignatureService().getSignature(baseString, config.getApiSecret(), token.getSecret());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addOAuthHeader(OAuthRequest request)
|
private void addSignature(OAuthRequest request)
|
||||||
{
|
{
|
||||||
String oauthHeader = headerExtractor.extract(request);
|
switch (config.getSignatureType())
|
||||||
request.addHeader(OAuthConstants.HEADER, oauthHeader);
|
{
|
||||||
|
case Header:
|
||||||
|
String oauthHeader = api.getHeaderExtractor().extract(request);
|
||||||
|
request.addHeader(OAuthConstants.HEADER, oauthHeader);
|
||||||
|
break;
|
||||||
|
case QueryString:
|
||||||
|
for (Map.Entry<String, String> entry : request.getOauthParameters().entrySet())
|
||||||
|
{
|
||||||
|
request.addQuerystringParameter(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package org.scribe.oauth;
|
||||||
|
|
||||||
|
import org.scribe.builder.api.*;
|
||||||
|
import org.scribe.model.*;
|
||||||
|
|
||||||
|
public class OAuth20ServiceImpl implements OAuthService
|
||||||
|
{
|
||||||
|
private static final String VERSION = "2.0";
|
||||||
|
|
||||||
|
private final DefaultApi20 api;
|
||||||
|
private final OAuthConfig config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default constructor
|
||||||
|
*
|
||||||
|
* @param api OAuth2.0 api information
|
||||||
|
* @param config OAuth 2.0 configuration param object
|
||||||
|
*/
|
||||||
|
public OAuth20ServiceImpl(DefaultApi20 api, OAuthConfig config)
|
||||||
|
{
|
||||||
|
this.api = api;
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public Token getAccessToken(Token requestToken, Verifier verifier)
|
||||||
|
{
|
||||||
|
OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
|
||||||
|
request.addQuerystringParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public Token getRequestToken()
|
||||||
|
{
|
||||||
|
throw new UnsupportedOperationException("Unsupported operation, please use 'getAuthorizationUrl' and redirect your users there");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public String getVersion()
|
||||||
|
{
|
||||||
|
return VERSION;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public void signRequest(Token accessToken, OAuthRequest request)
|
||||||
|
{
|
||||||
|
request.addQuerystringParameter(OAuthConstants.ACCESS_TOKEN, accessToken.getToken());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public String getAuthorizationUrl(Token requestToken)
|
||||||
|
{
|
||||||
|
return api.getAuthorizationUrl(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -41,11 +41,13 @@ public interface OAuthService
|
|||||||
* @return oauth version as string
|
* @return oauth version as string
|
||||||
*/
|
*/
|
||||||
public String getVersion();
|
public String getVersion();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds the 'scope' parameter. This is **not** a default OAuth parameter and the setting is optional.
|
* Returns the URL where you should redirect your users to authenticate
|
||||||
|
* your application.
|
||||||
*
|
*
|
||||||
* @param scope OAuth Api scope (optional)
|
* @param requestToken the request token you need to authorize
|
||||||
|
* @return the URL where you should redirect your users
|
||||||
*/
|
*/
|
||||||
public void addScope(String scope);
|
public String getAuthorizationUrl(Token requestToken);
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,7 @@ public class HMACSha1SignatureService implements SignatureService
|
|||||||
{
|
{
|
||||||
Preconditions.checkEmptyString(baseString, "Base string cant be null or empty string");
|
Preconditions.checkEmptyString(baseString, "Base string cant be null or empty string");
|
||||||
Preconditions.checkEmptyString(apiSecret, "Api secret cant be null or empty string");
|
Preconditions.checkEmptyString(apiSecret, "Api secret cant be null or empty string");
|
||||||
return doSign(baseString, apiSecret + '&' + tokenSecret);
|
return doSign(baseString, URLUtils.percentEncode(apiSecret) + '&' + URLUtils.percentEncode(tokenSecret));
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -33,4 +33,19 @@ public class MapUtils
|
|||||||
Collections.sort(keys);
|
Collections.sort(keys);
|
||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Form-urlDecodes and appends all keys from the source {@link Map} to the
|
||||||
|
* target {@link Map}
|
||||||
|
*
|
||||||
|
* @param source Map from where the keys get copied and decoded
|
||||||
|
* @param target Map where the decoded keys are copied to
|
||||||
|
*/
|
||||||
|
public static void decodeAndAppendEntries(Map<String, String> source, Map<String, String> target)
|
||||||
|
{
|
||||||
|
for (String key: source.keySet())
|
||||||
|
{
|
||||||
|
target.put(URLUtils.percentEncode(key), URLUtils.percentEncode(source.get(key)));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -36,7 +36,7 @@ public class StreamUtils
|
|||||||
return out.toString();
|
return out.toString();
|
||||||
} catch (IOException ioe)
|
} catch (IOException ioe)
|
||||||
{
|
{
|
||||||
throw new RuntimeException("Error while reading response body", ioe);
|
throw new IllegalStateException("Error while reading response body", ioe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,24 +4,34 @@ import java.io.*;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import org.scribe.exceptions.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utils to deal with URL and url-encodings
|
* Utils to deal with URL and url-encodings
|
||||||
*
|
*
|
||||||
* @author Pablo Fernandez
|
* @author Pablo Fernandez
|
||||||
*/
|
*/
|
||||||
public class URLUtils
|
public class URLUtils
|
||||||
{
|
{
|
||||||
private static final String EMPTY_STRING = "";
|
private static final String EMPTY_STRING = "";
|
||||||
private static final String UTF_8 = "UTF-8";
|
private static final String UTF_8 = "UTF-8";
|
||||||
private static final char PAIR_SEPARATOR = '=';
|
private static final String PAIR_SEPARATOR = "=";
|
||||||
private static final char PARAM_SEPARATOR = '&';
|
private static final String PARAM_SEPARATOR = "&";
|
||||||
|
private static final char QUERY_STRING_SEPARATOR = '?';
|
||||||
|
|
||||||
private static final String ERROR_MSG = String.format("Cannot find specified encoding: %s", UTF_8);
|
private static final String ERROR_MSG = String.format("Cannot find specified encoding: %s", UTF_8);
|
||||||
|
|
||||||
|
private static final Set<EncodingRule> ENCODING_RULES;
|
||||||
|
|
||||||
|
static
|
||||||
|
{
|
||||||
|
Set<EncodingRule> rules = new HashSet<EncodingRule>();
|
||||||
|
rules.add(new EncodingRule("*","%2A"));
|
||||||
|
rules.add(new EncodingRule("+","%20"));
|
||||||
|
rules.add(new EncodingRule("%7E", "~"));
|
||||||
|
ENCODING_RULES = Collections.unmodifiableSet(rules);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turns a map into a form-url-encoded string (key=value&key2=value2)
|
* Turns a map into a form-urlencoded string
|
||||||
*
|
*
|
||||||
* @param map any map
|
* @param map any map
|
||||||
* @return form-url-encoded string
|
* @return form-url-encoded string
|
||||||
@@ -37,49 +47,146 @@ public class URLUtils
|
|||||||
StringBuffer encodedString = new StringBuffer(map.size() * 20);
|
StringBuffer encodedString = new StringBuffer(map.size() * 20);
|
||||||
for (String key : map.keySet())
|
for (String key : map.keySet())
|
||||||
{
|
{
|
||||||
if(encodedString.length() > 0)
|
encodedString.append(PARAM_SEPARATOR).append(formURLEncode(key));
|
||||||
|
if(map.get(key) != null)
|
||||||
{
|
{
|
||||||
encodedString.append(PARAM_SEPARATOR);
|
encodedString.append(PAIR_SEPARATOR).append(formURLEncode(map.get(key)));
|
||||||
}
|
}
|
||||||
encodedString.append(percentEncode(key)).append(PAIR_SEPARATOR).append(percentEncode(map.get(key)));
|
|
||||||
}
|
}
|
||||||
return encodedString.toString();
|
return encodedString.toString().substring(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Percent encodes a string
|
* Percent encodes a string
|
||||||
*
|
*
|
||||||
* @param plain
|
* @param string plain string
|
||||||
* @return percent encoded string
|
* @return percent encoded string
|
||||||
*/
|
*/
|
||||||
public static String percentEncode(String string)
|
public static String percentEncode(String string)
|
||||||
|
{
|
||||||
|
String encoded = formURLEncode(string);
|
||||||
|
for (EncodingRule rule : ENCODING_RULES)
|
||||||
|
{
|
||||||
|
encoded = rule.apply(encoded);
|
||||||
|
}
|
||||||
|
return encoded;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translates a string into application/x-www-form-urlencoded format
|
||||||
|
*
|
||||||
|
* @param plain
|
||||||
|
* @return form-urlencoded string
|
||||||
|
*/
|
||||||
|
public static String formURLEncode(String string)
|
||||||
{
|
{
|
||||||
Preconditions.checkNotNull(string, "Cannot encode null string");
|
Preconditions.checkNotNull(string, "Cannot encode null string");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return URLEncoder.encode(string, UTF_8).replaceAll("\\+", "%20");
|
return URLEncoder.encode(string, UTF_8);
|
||||||
}
|
}
|
||||||
catch (UnsupportedEncodingException uee)
|
catch (UnsupportedEncodingException uee)
|
||||||
{
|
{
|
||||||
throw new OAuthException(ERROR_MSG, uee);
|
throw new IllegalStateException(ERROR_MSG, uee);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Percent decodes a string
|
* Decodes a application/x-www-form-urlencoded string
|
||||||
*
|
*
|
||||||
* @param string percent encoded string
|
* @param string form-urlencoded string
|
||||||
* @return plain string
|
* @return plain string
|
||||||
*/
|
*/
|
||||||
public static String percentDecode(String string)
|
public static String formURLDecode(String string)
|
||||||
{
|
{
|
||||||
Preconditions.checkNotNull(string, "Cannot decode null string");
|
Preconditions.checkNotNull(string, "Cannot decode null string");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return URLDecoder.decode(string, UTF_8);
|
return URLDecoder.decode(string, UTF_8);
|
||||||
} catch (UnsupportedEncodingException uee)
|
}
|
||||||
|
catch (UnsupportedEncodingException uee)
|
||||||
{
|
{
|
||||||
throw new OAuthException(ERROR_MSG, uee);
|
throw new IllegalStateException(ERROR_MSG, uee);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append given parameters to the query string of the url
|
||||||
|
*
|
||||||
|
* @param url the url to append parameters to
|
||||||
|
* @param params any map
|
||||||
|
* @return new url with parameters on query string
|
||||||
|
*/
|
||||||
|
public static String appendParametersToQueryString(String url, Map<String, String> params)
|
||||||
|
{
|
||||||
|
Preconditions.checkNotNull(url, "Cannot append to null URL");
|
||||||
|
String queryString = URLUtils.formURLEncodeMap(params);
|
||||||
|
if (queryString.equals(EMPTY_STRING))
|
||||||
|
{
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
url += url.indexOf(QUERY_STRING_SEPARATOR) != -1 ? PARAM_SEPARATOR : QUERY_STRING_SEPARATOR;
|
||||||
|
url += queryString;
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 final String ch;
|
||||||
|
private final String toCh;
|
||||||
|
|
||||||
|
EncodingRule(String ch, String toCh)
|
||||||
|
{
|
||||||
|
this.ch = ch;
|
||||||
|
this.toCh = toCh;
|
||||||
|
}
|
||||||
|
|
||||||
|
String apply(String string) {
|
||||||
|
return string.replace(ch, toCh);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package org.scribe.builder;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.*;
|
||||||
|
import org.scribe.builder.api.*;
|
||||||
|
import org.scribe.model.*;
|
||||||
|
import org.scribe.oauth.*;
|
||||||
|
|
||||||
|
public class ServiceBuilderTest
|
||||||
|
{
|
||||||
|
private ServiceBuilder builder;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup()
|
||||||
|
{
|
||||||
|
builder = new ServiceBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnConfigDefaultValues()
|
||||||
|
{
|
||||||
|
builder.provider(ApiMock.class).apiKey("key").apiSecret("secret").build();
|
||||||
|
assertEquals(ApiMock.config.getApiKey(), "key");
|
||||||
|
assertEquals(ApiMock.config.getApiSecret(), "secret");
|
||||||
|
assertEquals(ApiMock.config.getCallback(), OAuthConstants.OUT_OF_BAND);
|
||||||
|
assertEquals(ApiMock.config.getSignatureType(), SignatureType.Header);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldAcceptValidCallbackUrl()
|
||||||
|
{
|
||||||
|
builder.provider(ApiMock.class).apiKey("key").apiSecret("secret").callback("http://example.com").build();
|
||||||
|
assertEquals(ApiMock.config.getApiKey(), "key");
|
||||||
|
assertEquals(ApiMock.config.getApiSecret(), "secret");
|
||||||
|
assertEquals(ApiMock.config.getCallback(), "http://example.com");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldAcceptASignatureType()
|
||||||
|
{
|
||||||
|
builder.provider(ApiMock.class).apiKey("key").apiSecret("secret").signatureType(SignatureType.QueryString).build();
|
||||||
|
assertEquals(ApiMock.config.getApiKey(), "key");
|
||||||
|
assertEquals(ApiMock.config.getApiSecret(), "secret");
|
||||||
|
assertEquals(ApiMock.config.getSignatureType(), SignatureType.QueryString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void shouldNotAcceptAnInvalidUrlAsCallback()
|
||||||
|
{
|
||||||
|
builder.provider(ApiMock.class).apiKey("key").apiSecret("secret").callback("example.com").build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldAcceptAnScope()
|
||||||
|
{
|
||||||
|
builder.provider(ApiMock.class).apiKey("key").apiSecret("secret").scope("rss-api").build();
|
||||||
|
assertEquals(ApiMock.config.getApiKey(), "key");
|
||||||
|
assertEquals(ApiMock.config.getApiSecret(), "secret");
|
||||||
|
assertEquals(ApiMock.config.getScope(), "rss-api");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ApiMock implements Api
|
||||||
|
{
|
||||||
|
public static OAuthConfig config;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OAuthService createService(OAuthConfig config)
|
||||||
|
{
|
||||||
|
ApiMock.config = config;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 FacebookExample
|
||||||
|
{
|
||||||
|
private static final String NETWORK_NAME = "Facebook";
|
||||||
|
private static final String PROTECTED_RESOURCE_URL = "https://graph.facebook.com/me";
|
||||||
|
private static final Token EMPTY_TOKEN = null;
|
||||||
|
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
// Replace these with your own api key and secret
|
||||||
|
String apiKey = "your_app_id";
|
||||||
|
String apiSecret = "your_api_secret";
|
||||||
|
OAuthService service = new ServiceBuilder()
|
||||||
|
.provider(FacebookApi.class)
|
||||||
|
.apiKey(apiKey)
|
||||||
|
.apiSecret(apiSecret)
|
||||||
|
.callback("http://www.example.com/oauth_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! :)");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
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 Foursquare2Example
|
||||||
|
{
|
||||||
|
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 = "FEGFXJUFANVVDHVSNUAMUKTTXCP1AJQD53E33XKJ44YP1S4I";
|
||||||
|
String apiSecret = "AYWKUL5SWPNC0CTQ202QXRUG2NLZYXMRA34ZSDW4AUYBG2RC";
|
||||||
|
OAuthService service = new ServiceBuilder()
|
||||||
|
.provider(Foursquare2Api.class)
|
||||||
|
.apiKey(apiKey)
|
||||||
|
.apiSecret(apiSecret)
|
||||||
|
.callback("http://localhost:9000/")
|
||||||
|
.build();
|
||||||
|
Scanner in = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("=== Foursquare2'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! :)");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,6 @@ import org.scribe.oauth.*;
|
|||||||
|
|
||||||
public class FoursquareExample
|
public class FoursquareExample
|
||||||
{
|
{
|
||||||
private static final String AUTHORIZE_URL = "http://foursquare.com/oauth/authorize?oauth_token=";
|
|
||||||
private static final String PROTECTED_RESOURCE_URL = "http://api.foursquare.com/v1/user";
|
private static final String PROTECTED_RESOURCE_URL = "http://api.foursquare.com/v1/user";
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
@@ -31,7 +30,7 @@ public class FoursquareExample
|
|||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
System.out.println("Now go and authorize Scribe here:");
|
System.out.println("Now go and authorize Scribe here:");
|
||||||
System.out.println(AUTHORIZE_URL + requestToken.getToken());
|
System.out.println(service.getAuthorizationUrl(requestToken));
|
||||||
System.out.println("And paste the verifier here");
|
System.out.println("And paste the verifier here");
|
||||||
System.out.print(">>");
|
System.out.print(">>");
|
||||||
Verifier verifier = new Verifier(in.nextLine());
|
Verifier verifier = new Verifier(in.nextLine());
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
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 GoogleExample
|
||||||
|
{
|
||||||
|
private static final String NETWORK_NAME = "Google";
|
||||||
|
private static final String AUTHORIZE_URL = "https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=";
|
||||||
|
private static final String PROTECTED_RESOURCE_URL = "https://docs.google.com/feeds/default/private/full/";
|
||||||
|
private static final String SCOPE = "https://docs.google.com/feeds/";
|
||||||
|
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
OAuthService service = new ServiceBuilder()
|
||||||
|
.provider(GoogleApi.class)
|
||||||
|
.apiKey("anonymous")
|
||||||
|
.apiSecret("anonymous")
|
||||||
|
.scope(SCOPE)
|
||||||
|
.build();
|
||||||
|
Scanner in = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
// Obtain the Request Token
|
||||||
|
System.out.println("Fetching the Request Token...");
|
||||||
|
Token requestToken = service.getRequestToken();
|
||||||
|
System.out.println("Got the Request Token!");
|
||||||
|
System.out.println("(if your curious it looks like this: " + requestToken + " )");
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
System.out.println("Now go and authorize Scribe here:");
|
||||||
|
System.out.println(AUTHORIZE_URL + requestToken.getToken());
|
||||||
|
System.out.println("And paste the verifier 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);
|
||||||
|
request.addHeader("GData-Version", "3.0");
|
||||||
|
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! :)");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,18 +9,17 @@ import org.scribe.oauth.*;
|
|||||||
|
|
||||||
public class LinkedInExample
|
public class LinkedInExample
|
||||||
{
|
{
|
||||||
private static final String AUTHORIZE_URL = "https://api.linkedin.com/uas/oauth/authorize?oauth_token=";
|
|
||||||
private static final String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~/connections:(id,last-name)";
|
private static final String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~/connections:(id,last-name)";
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
OAuthService service = new ServiceBuilder()
|
OAuthService service = new ServiceBuilder()
|
||||||
.provider(LinkedInApi.class)
|
.provider(LinkedInApi.class)
|
||||||
.apiKey("CiEgwWDkA5BFpNrc0RfGyVuSlOh4tig5kOTZ9q97qcXNrFl7zqk-Ts7DqRGaKDCV")
|
.apiKey("CiEgwWDkA5BFpNrc0RfGyVuSlOh4tig5kOTZ9q97qcXNrFl7zqk-Ts7DqRGaKDCV")
|
||||||
.apiSecret("dhho4dfoCmiQXrkw4yslork5XWLFnPSuMR-8gscPVjY4jqFFHPYWJKgpFl4uLTM6")
|
.apiSecret("dhho4dfoCmiQXrkw4yslork5XWLFnPSuMR-8gscPVjY4jqFFHPYWJKgpFl4uLTM6")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Scanner in = new Scanner(System.in);
|
Scanner in = new Scanner(System.in);
|
||||||
|
|
||||||
System.out.println("=== LinkedIn's OAuth Workflow ===");
|
System.out.println("=== LinkedIn's OAuth Workflow ===");
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
@@ -31,7 +30,7 @@ public class LinkedInExample
|
|||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
System.out.println("Now go and authorize Scribe here:");
|
System.out.println("Now go and authorize Scribe here:");
|
||||||
System.out.println(AUTHORIZE_URL + requestToken.getToken());
|
System.out.println(service.getAuthorizationUrl(requestToken));
|
||||||
System.out.println("And paste the verifier here");
|
System.out.println("And paste the verifier here");
|
||||||
System.out.print(">>");
|
System.out.print(">>");
|
||||||
Verifier verifier = new Verifier(in.nextLine());
|
Verifier verifier = new Verifier(in.nextLine());
|
||||||
@@ -55,8 +54,6 @@ public class LinkedInExample
|
|||||||
|
|
||||||
System.out.println();
|
System.out.println();
|
||||||
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
|
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
|
||||||
|
|
||||||
System.out.println(service.getRequestToken());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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 LoveFilmExample
|
||||||
|
{
|
||||||
|
private static final String NETWORK_NAME = "LoveFilm";
|
||||||
|
private static final String PROTECTED_RESOURCE_URL = "https://api.lovefilm.com/users";
|
||||||
|
|
||||||
|
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(LoveFilmApi.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 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! :)");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,6 @@ import org.scribe.oauth.*;
|
|||||||
|
|
||||||
public class TwitterExample
|
public class TwitterExample
|
||||||
{
|
{
|
||||||
private static final String AUTHORIZE_URL = "https://twitter.com/oauth/authorize?oauth_token=";
|
|
||||||
private static final String PROTECTED_RESOURCE_URL = "http://api.twitter.com/1/account/verify_credentials.xml";
|
private static final String PROTECTED_RESOURCE_URL = "http://api.twitter.com/1/account/verify_credentials.xml";
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
@@ -31,7 +30,7 @@ public class TwitterExample
|
|||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
System.out.println("Now go and authorize Scribe here:");
|
System.out.println("Now go and authorize Scribe here:");
|
||||||
System.out.println(AUTHORIZE_URL + requestToken.getToken());
|
System.out.println(service.getAuthorizationUrl(requestToken));
|
||||||
System.out.println("And paste the verifier here");
|
System.out.println("And paste the verifier here");
|
||||||
System.out.print(">>");
|
System.out.print(">>");
|
||||||
Verifier verifier = new Verifier(in.nextLine());
|
Verifier verifier = new Verifier(in.nextLine());
|
||||||
|
|||||||
@@ -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! :)");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,20 +9,18 @@ import org.scribe.oauth.*;
|
|||||||
|
|
||||||
public class YahooExample
|
public class YahooExample
|
||||||
{
|
{
|
||||||
private static final String NETWORK_NAME = "Yahoo";
|
|
||||||
private static final String AUTHORIZE_URL = "https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token=";
|
|
||||||
private static final String PROTECTED_RESOURCE_URL = "http://social.yahooapis.com/v1/user/A6ROU63MXWDCW3Y5MGCYWVHDJI/profile/status?format=json";
|
private static final String PROTECTED_RESOURCE_URL = "http://social.yahooapis.com/v1/user/A6ROU63MXWDCW3Y5MGCYWVHDJI/profile/status?format=json";
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
OAuthService service = new ServiceBuilder()
|
OAuthService service = new ServiceBuilder()
|
||||||
.provider(YahooApi.class)
|
.provider(YahooApi.class)
|
||||||
.apiKey("dj0yJmk9TXZDWVpNVVdGaVFmJmQ9WVdrOWMweHZXbkZLTkhVbWNHbzlNVEl5TWprd05qUTJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD0wMw--")
|
.apiKey("dj0yJmk9TXZDWVpNVVdGaVFmJmQ9WVdrOWMweHZXbkZLTkhVbWNHbzlNVEl5TWprd05qUTJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD0wMw--")
|
||||||
.apiSecret("262be559f92a2be20c4c039419018f2b48cdfce9")
|
.apiSecret("262be559f92a2be20c4c039419018f2b48cdfce9")
|
||||||
.build();
|
.build();
|
||||||
Scanner in = new Scanner(System.in);
|
Scanner in = new Scanner(System.in);
|
||||||
|
|
||||||
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
|
System.out.println("=== Yahoo's OAuth Workflow ===");
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
// Obtain the Request Token
|
// Obtain the Request Token
|
||||||
@@ -32,7 +30,7 @@ public class YahooExample
|
|||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
System.out.println("Now go and authorize Scribe here:");
|
System.out.println("Now go and authorize Scribe here:");
|
||||||
System.out.println(AUTHORIZE_URL + requestToken.getToken());
|
System.out.println(service.getAuthorizationUrl(requestToken));
|
||||||
System.out.println("And paste the verifier here");
|
System.out.println("And paste the verifier here");
|
||||||
System.out.print(">>");
|
System.out.print(">>");
|
||||||
Verifier verifier = new Verifier(in.nextLine());
|
Verifier verifier = new Verifier(in.nextLine());
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public class BaseStringExtractorTest
|
|||||||
@Test
|
@Test
|
||||||
public void shouldExtractBaseStringFromOAuthRequest()
|
public void shouldExtractBaseStringFromOAuthRequest()
|
||||||
{
|
{
|
||||||
String expected = "GET&http%3A%2F%2Fexample.com&oauth_callback%3Dhttp%253A%252F%252Fexample%252Fcallback%26oauth_consumer_key%3DAS%2523%2524%255E*%2540%2526%26oauth_signature%3DOAuth-Signature%26oauth_timestamp%3D123456";
|
String expected = "GET&http%3A%2F%2Fexample.com&oauth_callback%3Dhttp%253A%252F%252Fexample%252Fcallback%26oauth_consumer_key%3DAS%2523%2524%255E%252A%2540%2526%26oauth_signature%3DOAuth-Signature%26oauth_timestamp%3D123456";
|
||||||
String baseString = extractor.extract(request);
|
String baseString = extractor.extract(request);
|
||||||
assertEquals(expected, baseString);
|
assertEquals(expected, baseString);
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ public class BaseStringExtractorTest
|
|||||||
@Test
|
@Test
|
||||||
public void shouldProperlyEncodeSpaces()
|
public void shouldProperlyEncodeSpaces()
|
||||||
{
|
{
|
||||||
String expected = "GET&http%3A%2F%2Fexample.com&body%3Dthis%2520param%2520has%2520whitespace%26oauth_callback%3Dhttp%253A%252F%252Fexample%252Fcallback%26oauth_consumer_key%3DAS%2523%2524%255E*%2540%2526%26oauth_signature%3DOAuth-Signature%26oauth_timestamp%3D123456";
|
String expected = "GET&http%3A%2F%2Fexample.com&body%3Dthis%2520param%2520has%2520whitespace%26oauth_callback%3Dhttp%253A%252F%252Fexample%252Fcallback%26oauth_consumer_key%3DAS%2523%2524%255E%252A%2540%2526%26oauth_signature%3DOAuth-Signature%26oauth_timestamp%3D123456";
|
||||||
request.addBodyParameter("body", "this param has whitespace");
|
request.addBodyParameter("body", "this param has whitespace");
|
||||||
assertEquals(expected, extractor.extract(request));
|
assertEquals(expected, extractor.extract(request));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ public class HeaderExtractorTest
|
|||||||
public void shouldExtractStandardHeader()
|
public void shouldExtractStandardHeader()
|
||||||
{
|
{
|
||||||
String expected = "OAuth oauth_callback=\"http%3A%2F%2Fexample%2Fcallback\", " + "oauth_signature=\"OAuth-Signature\", "
|
String expected = "OAuth oauth_callback=\"http%3A%2F%2Fexample%2Fcallback\", " + "oauth_signature=\"OAuth-Signature\", "
|
||||||
+ "oauth_consumer_key=\"AS%23%24%5E*%40%26\", " + "oauth_timestamp=\"123456\"";
|
+ "oauth_consumer_key=\"AS%23%24%5E%2A%40%26\", " + "oauth_timestamp=\"123456\"";
|
||||||
String header = extractor.extract(request);
|
String header = extractor.extract(request);
|
||||||
assertEquals(expected, header);
|
assertEquals(expected, header);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package org.scribe.extractors;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.*;
|
||||||
|
import org.scribe.model.*;
|
||||||
|
|
||||||
|
public class JsonTokenExtractorTest
|
||||||
|
{
|
||||||
|
private String response = "'{ \"access_token\":\"I0122HHJKLEM21F3WLPYHDKGKZULAUO4SGMV3ABKFTDT3T3X\"}'";
|
||||||
|
private JsonTokenExtractor extractor = new JsonTokenExtractor();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldParseResponse()
|
||||||
|
{
|
||||||
|
Token token = extractor.extract(response);
|
||||||
|
assertEquals(token.getToken(), "I0122HHJKLEM21F3WLPYHDKGKZULAUO4SGMV3ABKFTDT3T3X");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void shouldThrowExceptionIfForNullParameters()
|
||||||
|
{
|
||||||
|
extractor.extract(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void shouldThrowExceptionIfForEmptyStrings()
|
||||||
|
{
|
||||||
|
extractor.extract("");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package org.scribe.extractors;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.*;
|
||||||
|
import org.scribe.exceptions.*;
|
||||||
|
import org.scribe.model.*;
|
||||||
|
|
||||||
|
public class TokenExtractor20Test
|
||||||
|
{
|
||||||
|
|
||||||
|
private TokenExtractor20Impl extractor;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup()
|
||||||
|
{
|
||||||
|
extractor = new TokenExtractor20Impl();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldExtractTokenFromOAuthStandardResponse()
|
||||||
|
{
|
||||||
|
String response = "access_token=166942940015970|2.2ltzWXYNDjCtg5ZDVVJJeg__.3600.1295816400-548517159|RsXNdKrpxg8L6QNLWcs2TVTmcaE";
|
||||||
|
Token extracted = extractor.extract(response);
|
||||||
|
assertEquals("166942940015970|2.2ltzWXYNDjCtg5ZDVVJJeg__.3600.1295816400-548517159|RsXNdKrpxg8L6QNLWcs2TVTmcaE", extracted.getToken());
|
||||||
|
assertEquals("", extracted.getSecret());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldExtractTokenFromResponseWithExpiresParam()
|
||||||
|
{
|
||||||
|
String response = "access_token=166942940015970|2.2ltzWXYNDjCtg5ZDVVJJeg__.3600.1295816400-548517159|RsXNdKrpxg8L6QNLWcs2TVTmcaE&expires=5108";
|
||||||
|
Token extracted = extractor.extract(response);
|
||||||
|
assertEquals("166942940015970|2.2ltzWXYNDjCtg5ZDVVJJeg__.3600.1295816400-548517159|RsXNdKrpxg8L6QNLWcs2TVTmcaE", extracted.getToken());
|
||||||
|
assertEquals("", extracted.getSecret());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = OAuthException.class)
|
||||||
|
public void shouldThrowExceptionIfTokenIsAbsent()
|
||||||
|
{
|
||||||
|
String response = "&expires=5108";
|
||||||
|
extractor.extract(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void shouldThrowExceptionIfResponseIsNull()
|
||||||
|
{
|
||||||
|
String response = null;
|
||||||
|
extractor.extract(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void shouldThrowExceptionIfResponseIsEmptyString()
|
||||||
|
{
|
||||||
|
String response = "";
|
||||||
|
extractor.extract(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,6 +26,15 @@ public class TokenExtractorTest
|
|||||||
assertEquals("hdhd0244k9j7ao03", extracted.getSecret());
|
assertEquals("hdhd0244k9j7ao03", extracted.getSecret());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldExtractTokenFromInvertedOAuthStandardResponse()
|
||||||
|
{
|
||||||
|
String response = "oauth_token_secret=hh5s93j4hdidpola&oauth_token=hdhd0244k9j7ao03";
|
||||||
|
Token extracted = extractor.extract(response);
|
||||||
|
assertEquals("hh5s93j4hdidpola", extracted.getSecret());
|
||||||
|
assertEquals("hdhd0244k9j7ao03", extracted.getToken());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldExtractTokenFromResponseWithCallbackConfirmed()
|
public void shouldExtractTokenFromResponseWithCallbackConfirmed()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,22 +10,19 @@ public class OAuthConfigTest
|
|||||||
@Test
|
@Test
|
||||||
public void shouldReturnDefaultValuesIfNotSet()
|
public void shouldReturnDefaultValuesIfNotSet()
|
||||||
{
|
{
|
||||||
OAuthConfig config = new OAuthConfig();
|
OAuthConfig config = new OAuthConfig("key", "secret");
|
||||||
assertEquals(Verb.GET, config.getAccessTokenVerb());
|
|
||||||
assertEquals(Verb.GET, config.getRequestTokenVerb());
|
|
||||||
assertEquals(OAuthConstants.OUT_OF_BAND, config.getCallback());
|
assertEquals(OAuthConstants.OUT_OF_BAND, config.getCallback());
|
||||||
|
assertEquals(SignatureType.Header, config.getSignatureType());
|
||||||
|
assertFalse(config.hasScope());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldOverrideDefaultsIfSet()
|
public void shouldOverrideDefaultsIfSet()
|
||||||
{
|
{
|
||||||
OAuthConfig config = new OAuthConfig();
|
OAuthConfig config = new OAuthConfig("key", "secret", "http://callback", SignatureType.Header, "scope");
|
||||||
config.setCallback("http://callback");
|
|
||||||
config.setAccessTokenVerb(Verb.POST);
|
|
||||||
config.setRequestTokenVerb(Verb.POST);
|
|
||||||
assertEquals("http://callback", config.getCallback());
|
assertEquals("http://callback", config.getCallback());
|
||||||
assertEquals(Verb.POST, config.getAccessTokenVerb());
|
assertEquals("key", config.getApiKey());
|
||||||
assertEquals(Verb.POST, config.getRequestTokenVerb());
|
assertEquals("secret", config.getApiSecret());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -21,8 +21,9 @@ public class OAuthRequestTest
|
|||||||
request.addOAuthParameter(OAuthConstants.TOKEN, "token");
|
request.addOAuthParameter(OAuthConstants.TOKEN, "token");
|
||||||
request.addOAuthParameter(OAuthConstants.NONCE, "nonce");
|
request.addOAuthParameter(OAuthConstants.NONCE, "nonce");
|
||||||
request.addOAuthParameter(OAuthConstants.TIMESTAMP, "ts");
|
request.addOAuthParameter(OAuthConstants.TIMESTAMP, "ts");
|
||||||
|
request.addOAuthParameter(OAuthConstants.SCOPE, "feeds");
|
||||||
|
|
||||||
assertEquals(3, request.getOauthParameters().size());
|
assertEquals(4, request.getOauthParameters().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
|||||||
@@ -16,8 +16,10 @@ public class RequestTest
|
|||||||
{
|
{
|
||||||
connection = new ConnectionStub();
|
connection = new ConnectionStub();
|
||||||
postRequest = new Request(Verb.POST, "http://example.com");
|
postRequest = new Request(Verb.POST, "http://example.com");
|
||||||
|
postRequest.addBodyParameter("param", "value");
|
||||||
|
postRequest.addBodyParameter("param with spaces", "value with spaces");
|
||||||
postRequest.setConnection(connection);
|
postRequest.setConnection(connection);
|
||||||
getRequest = new Request(Verb.GET, "http://example.com?qsparam=value&other=value+with+spaces");
|
getRequest = new Request(Verb.GET, "http://example.com?qsparam=value&other+param=value+with+spaces");
|
||||||
getRequest.setConnection(connection);
|
getRequest.setConnection(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,6 +36,7 @@ public class RequestTest
|
|||||||
assertEquals(2, getRequest.getQueryStringParams().size());
|
assertEquals(2, getRequest.getQueryStringParams().size());
|
||||||
assertEquals(0, postRequest.getQueryStringParams().size());
|
assertEquals(0, postRequest.getQueryStringParams().size());
|
||||||
assertTrue(getRequest.getQueryStringParams().containsKey("qsparam"));
|
assertTrue(getRequest.getQueryStringParams().containsKey("qsparam"));
|
||||||
|
assertTrue(getRequest.getQueryStringParams().get("qsparam").equals("value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -47,12 +50,10 @@ public class RequestTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldSetBodyParamsAndHeaders()
|
public void shouldSetBodyParamsAndAddContentLength()
|
||||||
{
|
{
|
||||||
postRequest.addBodyParameter("param", "value");
|
assertEquals("param=value¶m+with+spaces=value+with+spaces", postRequest.getBodyContents());
|
||||||
postRequest.addBodyParameter("param two", "value with spaces");
|
|
||||||
postRequest.send();
|
postRequest.send();
|
||||||
assertEquals("param%20two=value%20with%20spaces¶m=value", postRequest.getBodyContents());
|
|
||||||
assertTrue(connection.getHeaders().containsKey("Content-Length"));
|
assertTrue(connection.getHeaders().containsKey("Content-Length"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,4 +66,18 @@ public class RequestTest
|
|||||||
assertTrue(connection.getHeaders().containsKey("Content-Length"));
|
assertTrue(connection.getHeaders().containsKey("Content-Length"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldAllowAddingQuerystringParametersAfterCreation()
|
||||||
|
{
|
||||||
|
Request request = new Request(Verb.GET, "http://example.com?one=val");
|
||||||
|
request.addQuerystringParameter("two", "other val");
|
||||||
|
request.addQuerystringParameter("more", "params");
|
||||||
|
assertEquals(3, request.getQueryStringParams().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHandleQueryStringSpaceEncodingProperly()
|
||||||
|
{
|
||||||
|
assertTrue(getRequest.getQueryStringParams().get("other param").equals("value with spaces"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,7 @@ package org.scribe.services;
|
|||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
import org.scribe.exceptions.OAuthException;
|
import org.scribe.exceptions.*;
|
||||||
|
|
||||||
public class HMACSha1SignatureServiceTest
|
public class HMACSha1SignatureServiceTest
|
||||||
{
|
{
|
||||||
@@ -29,7 +29,7 @@ public class HMACSha1SignatureServiceTest
|
|||||||
String apiSecret = "api secret";
|
String apiSecret = "api secret";
|
||||||
String tokenSecret = "token secret";
|
String tokenSecret = "token secret";
|
||||||
String baseString = "base string";
|
String baseString = "base string";
|
||||||
String signature = "cyxvUDZY/5cQBcBiKulaNgnhN/w=";
|
String signature = "uGymw2KHOTWI699YEaoi5xyLT50=";
|
||||||
assertEquals(signature, service.getSignature(baseString, apiSecret, tokenSecret));
|
assertEquals(signature, service.getSignature(baseString, apiSecret, tokenSecret));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,13 +8,6 @@ import org.junit.*;
|
|||||||
|
|
||||||
public class URLUtilsTest
|
public class URLUtilsTest
|
||||||
{
|
{
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setup()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldPercentEncodeMap()
|
public void shouldPercentEncodeMap()
|
||||||
{
|
{
|
||||||
@@ -23,7 +16,7 @@ public class URLUtilsTest
|
|||||||
params.put("key with spaces", "value with spaces");
|
params.put("key with spaces", "value with spaces");
|
||||||
params.put("&symbols!", "#!");
|
params.put("&symbols!", "#!");
|
||||||
|
|
||||||
String expected = "key=value&key%20with%20spaces=value%20with%20spaces&%26symbols%21=%23%21";
|
String expected = "key=value&key+with+spaces=value+with+spaces&%26symbols%21=%23%21";
|
||||||
assertEquals(expected, URLUtils.formURLEncodeMap(params));
|
assertEquals(expected, URLUtils.formURLEncodeMap(params));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,6 +28,17 @@ public class URLUtilsTest
|
|||||||
assertEquals(expected, URLUtils.formURLEncodeMap(params));
|
assertEquals(expected, URLUtils.formURLEncodeMap(params));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldFormURLEncodeMapWithMissingValues()
|
||||||
|
{
|
||||||
|
Map<String, String> params = new LinkedHashMap<String, String>();
|
||||||
|
params.put("key", "value");
|
||||||
|
params.put("key with spaces", null);
|
||||||
|
|
||||||
|
String expected = "key=value&key+with+spaces";
|
||||||
|
assertEquals(expected, URLUtils.formURLEncodeMap(params));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldPercentEncodeString()
|
public void shouldPercentEncodeString()
|
||||||
{
|
{
|
||||||
@@ -44,11 +48,36 @@ public class URLUtilsTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldPercentDecodeString()
|
public void shouldFormURLEncodeString()
|
||||||
|
{
|
||||||
|
String toEncode = "this is a test &^";
|
||||||
|
String expected = "this+is+a+test+%26%5E";
|
||||||
|
assertEquals(expected, URLUtils.formURLEncode(toEncode));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldFormURLDecodeString()
|
||||||
{
|
{
|
||||||
String toDecode = "this+is+a+test+%26%5E";
|
String toDecode = "this+is+a+test+%26%5E";
|
||||||
String expected = "this is a test &^";
|
String expected = "this is a test &^";
|
||||||
assertEquals(expected, URLUtils.percentDecode(toDecode));
|
assertEquals(expected, URLUtils.formURLDecode(toDecode));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldPercentEncodeAllSpecialCharacters()
|
||||||
|
{
|
||||||
|
String plain = "!*'();:@&=+$,/?#[]";
|
||||||
|
String encoded = "%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D";
|
||||||
|
assertEquals(encoded, URLUtils.percentEncode(plain));
|
||||||
|
assertEquals(plain, URLUtils.formURLDecode(encoded));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotPercentEncodeReservedCharacters()
|
||||||
|
{
|
||||||
|
String plain = "abcde123456-._~";
|
||||||
|
String encoded = plain;
|
||||||
|
assertEquals(encoded, URLUtils.percentEncode(plain));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
@@ -69,7 +98,69 @@ public class URLUtilsTest
|
|||||||
public void shouldThrowExceptionIfStringToDecodeIsNull()
|
public void shouldThrowExceptionIfStringToDecodeIsNull()
|
||||||
{
|
{
|
||||||
String toDecode = null;
|
String toDecode = null;
|
||||||
URLUtils.percentDecode(toDecode);
|
URLUtils.formURLDecode(toDecode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void shouldThrowExceptionWhenAppendingNullMapToQuerystring()
|
||||||
|
{
|
||||||
|
String url = "http://www.example.com";
|
||||||
|
Map<String, String> nullMap = null;
|
||||||
|
URLUtils.appendParametersToQueryString(url, nullMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldAppendNothingToQuerystringIfGivenEmptyMap()
|
||||||
|
{
|
||||||
|
String url = "http://www.example.com";
|
||||||
|
Map<String, String> emptyMap = new HashMap<String, String>();
|
||||||
|
String newUrl = URLUtils.appendParametersToQueryString(url, emptyMap);
|
||||||
|
Assert.assertEquals(url, newUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldAppendParametersToSimpleUrl()
|
||||||
|
{
|
||||||
|
String url = "http://www.example.com";
|
||||||
|
String expectedUrl = "http://www.example.com?param1=value1¶m2=value+with+spaces";
|
||||||
|
|
||||||
|
Map<String, String> params = new HashMap<String, String>();
|
||||||
|
params.put("param1", "value1");
|
||||||
|
params.put("param2", "value with spaces");
|
||||||
|
|
||||||
|
url = URLUtils.appendParametersToQueryString(url, params);
|
||||||
|
Assert.assertEquals(url, expectedUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldAppendParametersToUrlWithQuerystring()
|
||||||
|
{
|
||||||
|
String url = "http://www.example.com?already=present";
|
||||||
|
String expectedUrl = "http://www.example.com?already=present¶m1=value1¶m2=value+with+spaces";
|
||||||
|
|
||||||
|
Map<String, String> params = new HashMap<String, String>();
|
||||||
|
params.put("param1", "value1");
|
||||||
|
params.put("param2", "value with spaces");
|
||||||
|
|
||||||
|
url = URLUtils.appendParametersToQueryString(url, params);
|
||||||
|
Assert.assertEquals(url, expectedUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldPercentEncodePlusSymbol()
|
||||||
|
{
|
||||||
|
String plain = "7aEP+jNAwvjc0mjhqg0nuXPf";
|
||||||
|
String encoded = "7aEP%2BjNAwvjc0mjhqg0nuXPf";
|
||||||
|
|
||||||
|
Assert.assertEquals(encoded, URLUtils.percentEncode(plain));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldURLDecodePlusSymbol()
|
||||||
|
{
|
||||||
|
String encoded = "oauth_verifier=7aEP%2BjNAwvjc0mjhqg0nuXPf";
|
||||||
|
String expected = "oauth_verifier=7aEP+jNAwvjc0mjhqg0nuXPf";
|
||||||
|
|
||||||
|
Assert.assertEquals(expected, URLUtils.formURLDecode(encoded));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user