* http://tools.ietf.org/html/rfc6749#section-5.1 * * @see OAuth 2 Access Token Specification - *
*/ public class OAuth2AccessToken extends Token { diff --git a/scribejava-core/src/main/java/com/github/scribejava/core/model/OAuth2AccessTokenErrorResponse.java b/scribejava-core/src/main/java/com/github/scribejava/core/model/OAuth2AccessTokenErrorResponse.java index c64a3d005..5c4a65a19 100644 --- a/scribejava-core/src/main/java/com/github/scribejava/core/model/OAuth2AccessTokenErrorResponse.java +++ b/scribejava-core/src/main/java/com/github/scribejava/core/model/OAuth2AccessTokenErrorResponse.java @@ -1,6 +1,5 @@ package com.github.scribejava.core.model; -import com.github.scribejava.core.exceptions.OAuthException; import com.github.scribejava.core.oauth2.OAuth2Error; import java.io.IOException; @@ -9,39 +8,20 @@ /** * Representing "5.2. Error Response" */ -public class OAuth2AccessTokenErrorResponse extends OAuthException { +public class OAuth2AccessTokenErrorResponse extends OAuthResponseException { private static final long serialVersionUID = 2309424849700276816L; private final OAuth2Error error; private final String errorDescription; private final URI errorUri; - private final Response response; public OAuth2AccessTokenErrorResponse(OAuth2Error error, String errorDescription, URI errorUri, Response rawResponse) throws IOException { - super(rawResponse.getBody()); + super(rawResponse); this.error = error; this.errorDescription = errorDescription; this.errorUri = errorUri; - this.response = rawResponse; - } - - /** - * - * @param error error - * @param errorDescription errorDescription - * @param errorUri errorUri - * @param rawResponse rawResponse - * @throws java.io.IOException IOException - * @deprecated use {@link #OAuth2AccessTokenErrorResponse(com.github.scribejava.core.oauth2.OAuth2Error, - * java.lang.String, java.net.URI, com.github.scribejava.core.model.Response) - * } - */ - @Deprecated - public OAuth2AccessTokenErrorResponse(OAuth2Error error, String errorDescription, URI errorUri, - String rawResponse) throws IOException { - this(error, errorDescription, errorUri, new Response(-1, null, null, rawResponse)); } public OAuth2Error getError() { @@ -56,19 +36,4 @@ public URI getErrorUri() { return errorUri; } - /** - * - * @return body of response - * @throws IOException IOException - * @deprecated use {@link #getResponse()} and then {@link Response#getBody()} - */ - @Deprecated - public String getRawResponse() throws IOException { - return response.getBody(); - } - - public Response getResponse() { - return response; - } - } diff --git a/scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthResponseException.java b/scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthResponseException.java new file mode 100644 index 000000000..5b6da25cf --- /dev/null +++ b/scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthResponseException.java @@ -0,0 +1,44 @@ +package com.github.scribejava.core.model; + +import com.github.scribejava.core.exceptions.OAuthException; +import java.io.IOException; +import java.util.Objects; + +public class OAuthResponseException extends OAuthException { + + private static final long serialVersionUID = 1309424849700276816L; + + private final transient Response response; + + public OAuthResponseException(Response rawResponse) throws IOException { + super(rawResponse.getBody()); + this.response = rawResponse; + } + + public Response getResponse() { + return response; + } + + @Override + public int hashCode() { + int hash = 5; + hash = 29 * hash + Objects.hashCode(response); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final OAuthResponseException other = (OAuthResponseException) obj; + return Objects.equals(this.response, other.response); + } + +} diff --git a/scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuth10aService.java b/scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuth10aService.java index 89fd279b1..f48308b2e 100644 --- a/scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuth10aService.java +++ b/scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuth10aService.java @@ -39,7 +39,7 @@ public OAuth1RequestToken getRequestToken() throws IOException, InterruptedExcep final OAuthRequest request = prepareRequestTokenRequest(); log("sending request..."); - try (Response response = execute(request)) { + try ( Response response = execute(request)) { if (isDebug()) { final String body = response.getBody(); log("response status code: %s", response.getCode()); @@ -105,7 +105,7 @@ public OAuth1AccessToken getAccessToken(OAuth1RequestToken requestToken, String log("obtaining access token from %s", api.getAccessTokenEndpoint()); } final OAuthRequest request = prepareAccessTokenRequest(requestToken, oauthVerifier); - try (Response response = execute(request)) { + try ( Response response = execute(request)) { return api.getAccessTokenExtractor().extract(response); } } diff --git a/scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuth20Service.java b/scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuth20Service.java index 4b4bb6adb..82052e351 100644 --- a/scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuth20Service.java +++ b/scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuth20Service.java @@ -40,6 +40,108 @@ public OAuth20Service(DefaultApi20 api, String apiKey, String apiSecret, String this.defaultScope = defaultScope; } + // ===== common OAuth methods ===== + /** + * {@inheritDoc} + */ + @Override + public String getVersion() { + return VERSION; + } + + public void signRequest(String accessToken, OAuthRequest request) { + api.getBearerSignature().signRequest(accessToken, request); + } + + public void signRequest(OAuth2AccessToken accessToken, OAuthRequest request) { + signRequest(accessToken == null ? null : accessToken.getAccessToken(), request); + } + + /** + * Returns the URL where you should redirect your users to authenticate your application. + * + * @return the URL where you should redirect your users + */ + public String getAuthorizationUrl() { + return createAuthorizationUrlBuilder().build(); + } + + public String getAuthorizationUrl(String state) { + return createAuthorizationUrlBuilder() + .state(state) + .build(); + } + + /** + * Returns the URL where you should redirect your users to authenticate your application. + * + * @param additionalParams any additional GET params to add to the URL + * @return the URL where you should redirect your users + */ + public String getAuthorizationUrl(Map