Skip to content

Commit fb24008

Browse files
committed
add raw Response (with HTTP repsponse code and body) as member to the OAuth2AccessTokenErrorResponse
1 parent 1dbeabb commit fb24008

12 files changed

Lines changed: 157 additions & 47 deletions

File tree

changelog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[SNAPSHOT]
2+
* add raw Response (with HTTP repsponse code and body) as member to the OAuth2AccessTokenErrorResponse
3+
14
[8.0.0]
25
* add Kakao API (https://kakao.com/) (thanks to https://github.com/v0o0v)
36
* support chunks in JDKHttpClient's Multipart (thanks to https://github.com/eos1d3)

scribejava-apis/src/main/java/com/github/scribejava/apis/facebook/FacebookAccessTokenErrorResponse.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.github.scribejava.apis.facebook;
22

33
import com.github.scribejava.core.exceptions.OAuthException;
4+
import com.github.scribejava.core.model.Response;
5+
import java.io.IOException;
46
import java.util.Objects;
57

68
/**
@@ -21,15 +23,32 @@ public class FacebookAccessTokenErrorResponse extends OAuthException {
2123
private final String type;
2224
private final int codeInt;
2325
private final String fbtraceId;
24-
private final String rawResponse;
26+
private final Response response;
2527

2628
public FacebookAccessTokenErrorResponse(String message, String type, int code, String fbtraceId,
27-
String rawResponse) {
29+
Response response) {
2830
super(message);
2931
this.type = type;
3032
this.codeInt = code;
3133
this.fbtraceId = fbtraceId;
32-
this.rawResponse = rawResponse;
34+
this.response = response;
35+
}
36+
37+
/**
38+
*
39+
* @param message message
40+
* @param type type
41+
* @param code code
42+
* @param fbtraceId fbtraceId
43+
* @param rawResponse rawResponse
44+
* @deprecated use {@link #FacebookAccessTokenErrorResponse(java.lang.String, java.lang.String,
45+
* int, java.lang.String, com.github.scribejava.core.model.Response)
46+
* }
47+
*/
48+
@Deprecated
49+
public FacebookAccessTokenErrorResponse(String message, String type, int code, String fbtraceId,
50+
String rawResponse) {
51+
this(message, type, code, fbtraceId, new Response(-1, null, null, rawResponse));
3352
}
3453

3554
public String getType() {
@@ -44,14 +63,25 @@ public String getFbtraceId() {
4463
return fbtraceId;
4564
}
4665

47-
public String getRawResponse() {
48-
return rawResponse;
66+
/**
67+
*
68+
* @return body of response
69+
* @throws IOException IOException
70+
* @deprecated use {@link #getResponse()} and then {@link Response#getBody()}
71+
*/
72+
@Deprecated
73+
public String getRawResponse() throws IOException {
74+
return response.getBody();
75+
}
76+
77+
public Response getResponse() {
78+
return response;
4979
}
5080

5181
@Override
5282
public int hashCode() {
5383
int hash = 5;
54-
hash = 83 * hash + Objects.hashCode(rawResponse);
84+
hash = 83 * hash + Objects.hashCode(response);
5585
hash = 83 * hash + Objects.hashCode(getMessage());
5686
hash = 83 * hash + Objects.hashCode(type);
5787
hash = 83 * hash + Objects.hashCode(codeInt);
@@ -71,7 +101,7 @@ public boolean equals(Object obj) {
71101
return false;
72102
}
73103
final FacebookAccessTokenErrorResponse other = (FacebookAccessTokenErrorResponse) obj;
74-
if (!Objects.equals(rawResponse, other.getRawResponse())) {
104+
if (!Objects.equals(response, other.getResponse())) {
75105
return false;
76106
}
77107
if (!Objects.equals(getMessage(), other.getMessage())) {
@@ -89,7 +119,7 @@ public boolean equals(Object obj) {
89119
@Override
90120
public String toString() {
91121
return "FacebookAccessTokenErrorResponse{'type'='" + type + "', 'codeInt'='" + codeInt
92-
+ "', 'fbtraceId'='" + fbtraceId + "', 'rawResponse'='" + rawResponse
122+
+ "', 'fbtraceId'='" + fbtraceId + "', 'response'='" + response
93123
+ "', 'message'='" + getMessage() + "'}";
94124
}
95125
}

scribejava-apis/src/main/java/com/github/scribejava/apis/facebook/FacebookAccessTokenJsonExtractor.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
5+
import com.github.scribejava.core.model.Response;
56
import java.io.IOException;
67

78
/**
@@ -29,13 +30,17 @@ public static FacebookAccessTokenJsonExtractor instance() {
2930
*
3031
* '{"error":{"message":"Error validating application. Invalid application
3132
* ID.","type":"OAuthException","code":101,"fbtrace_id":"CvDR+X4WWIx"}}'
33+
*
34+
* @param response response
3235
*/
3336
@Override
34-
public void generateError(String rawResponse) throws IOException {
35-
final JsonNode errorNode = OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER.readTree(rawResponse).get("error");
37+
public void generateError(Response response) throws IOException {
38+
final JsonNode errorNode = OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER
39+
.readTree(response.getBody())
40+
.get("error");
3641

3742
throw new FacebookAccessTokenErrorResponse(errorNode.get("message").asText(), errorNode.get("type").asText(),
38-
errorNode.get("code").asInt(), errorNode.get("fbtrace_id").asText(), rawResponse);
43+
errorNode.get("code").asInt(), errorNode.get("fbtrace_id").asText(), response);
3944
}
4045

4146
}

scribejava-apis/src/main/java/com/github/scribejava/apis/fitbit/FitBitJsonTokenExtractor.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.github.scribejava.apis.fitbit;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import com.fasterxml.jackson.databind.JsonNode;
45
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
56
import com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
7+
import com.github.scribejava.core.model.Response;
68
import com.github.scribejava.core.oauth2.OAuth2Error;
79
import java.io.IOException;
810

@@ -31,18 +33,23 @@ protected FitBitOAuth2AccessToken createToken(String accessToken, String tokenTy
3133
* Related documentation: https://dev.fitbit.com/build/reference/web-api/oauth2/
3234
*/
3335
@Override
34-
public void generateError(String rawResponse) throws IOException {
35-
final JsonNode errorNode = OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER.readTree(rawResponse)
36-
.get("errors").get(0);
36+
public void generateError(Response response) throws IOException {
37+
final JsonNode errorNode;
38+
try {
39+
errorNode = OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER.readTree(response.getBody()).get("errors").get(0);
40+
} catch (JsonProcessingException ex) {
41+
throw new OAuth2AccessTokenErrorResponse(null, null, null, response);
42+
}
3743

3844
OAuth2Error errorCode;
3945
try {
40-
errorCode = OAuth2Error.parseFrom(extractRequiredParameter(errorNode, "errorType", rawResponse).asText());
46+
errorCode = OAuth2Error
47+
.parseFrom(extractRequiredParameter(errorNode, "errorType", response.getBody()).asText());
4148
} catch (IllegalArgumentException iaE) {
4249
//non oauth standard error code
4350
errorCode = null;
4451
}
4552

46-
throw new OAuth2AccessTokenErrorResponse(errorCode, errorNode.get("message").asText(), null, rawResponse);
53+
throw new OAuth2AccessTokenErrorResponse(errorCode, errorNode.get("message").asText(), null, response);
4754
}
4855
}

scribejava-apis/src/main/java/com/github/scribejava/apis/polar/PolarJsonTokenExtractor.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.github.scribejava.apis.polar;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import com.fasterxml.jackson.databind.JsonNode;
45
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
56
import com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
7+
import com.github.scribejava.core.model.Response;
68
import com.github.scribejava.core.oauth2.OAuth2Error;
7-
89
import java.io.IOException;
910

1011
/**
@@ -32,18 +33,23 @@ protected PolarOAuth2AccessToken createToken(String accessToken, String tokenTyp
3233
}
3334

3435
@Override
35-
public void generateError(String rawResponse) throws IOException {
36-
final JsonNode errorNode = OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER.readTree(rawResponse)
37-
.get("errors").get(0);
36+
public void generateError(Response response) throws IOException {
37+
final JsonNode errorNode;
38+
try {
39+
errorNode = OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER.readTree(response.getBody()).get("errors").get(0);
40+
} catch (JsonProcessingException ex) {
41+
throw new OAuth2AccessTokenErrorResponse(null, null, null, response);
42+
}
3843

3944
OAuth2Error errorCode;
4045
try {
41-
errorCode = OAuth2Error.parseFrom(extractRequiredParameter(errorNode, "errorType", rawResponse).asText());
46+
errorCode = OAuth2Error
47+
.parseFrom(extractRequiredParameter(errorNode, "errorType", response.getBody()).asText());
4248
} catch (IllegalArgumentException iaE) {
4349
//non oauth standard error code
4450
errorCode = null;
4551
}
4652

47-
throw new OAuth2AccessTokenErrorResponse(errorCode, errorNode.get("message").asText(), null, rawResponse);
53+
throw new OAuth2AccessTokenErrorResponse(errorCode, errorNode.get("message").asText(), null, response);
4854
}
4955
}

scribejava-apis/src/test/java/com/github/scribejava/apis/facebook/FacebookAccessTokenJsonExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void shouldThrowExceptionIfResponseIsError() throws IOException {
2626
assertEquals("OAuthException", fateR.getType());
2727
assertEquals(100, fateR.getCodeInt());
2828
assertEquals("DtxvtGRaxbB", fateR.getFbtraceId());
29-
assertEquals(body, fateR.getRawResponse());
29+
assertEquals(body, fateR.getResponse().getBody());
3030
}
3131
}
3232

scribejava-apis/src/test/java/com/github/scribejava/apis/fitbit/FitBitJsonTokenExtractorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.scribejava.apis.fitbit;
22

33
import com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
4+
import com.github.scribejava.core.model.Response;
45
import com.github.scribejava.core.oauth2.OAuth2Error;
56
import java.io.IOException;
67

@@ -28,7 +29,7 @@ public void testErrorExtraction() throws IOException {
2829
new ThrowingRunnable() {
2930
@Override
3031
public void run() throws Throwable {
31-
extractor.generateError(ERROR_JSON);
32+
extractor.generateError(new Response(403, null, null, ERROR_JSON));
3233
}
3334
});
3435
assertSame(OAuth2Error.INVALID_GRANT, thrown.getError());

scribejava-core/src/main/java/com/github/scribejava/core/extractors/DeviceAuthorizationJsonExtractor.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,25 @@ public static DeviceAuthorizationJsonExtractor instance() {
2222
}
2323

2424
public DeviceAuthorization extract(Response response) throws IOException {
25-
26-
final String body = response.getBody();
27-
2825
if (response.getCode() != 200) {
29-
generateError(body);
26+
generateError(response);
3027
}
31-
return createDeviceAuthorization(body);
28+
return createDeviceAuthorization(response.getBody());
3229
}
3330

31+
/**
32+
*
33+
* @param rawResponse rawResponse
34+
* @throws java.io.IOException IOException
35+
* @deprecated use {@link #generateError(com.github.scribejava.core.model.Response) }
36+
*/
37+
@Deprecated
3438
public void generateError(String rawResponse) throws IOException {
35-
OAuth2AccessTokenJsonExtractor.instance().generateError(rawResponse);
39+
generateError(new Response(-1, null, null, rawResponse));
40+
}
41+
42+
public void generateError(Response response) throws IOException {
43+
OAuth2AccessTokenJsonExtractor.instance().generateError(response);
3644
}
3745

3846
private DeviceAuthorization createDeviceAuthorization(String rawResponse) throws IOException {

scribejava-core/src/main/java/com/github/scribejava/core/extractors/OAuth2AccessTokenJsonExtractor.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.scribejava.core.extractors;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import com.fasterxml.jackson.databind.JsonNode;
45
import java.io.IOException;
56
import java.net.URI;
@@ -33,21 +34,39 @@ public OAuth2AccessToken extract(Response response) throws IOException {
3334
Preconditions.checkEmptyString(body, "Response body is incorrect. Can't extract a token from an empty string");
3435

3536
if (response.getCode() != 200) {
36-
generateError(body);
37+
generateError(response);
3738
}
3839
return createToken(body);
3940
}
4041

4142
/**
42-
* Related documentation: https://tools.ietf.org/html/rfc6749#section-5.2
4343
*
44-
* @param rawResponse response
45-
* @throws IOException IOException
44+
* @param rawResponse rawResponse
45+
* @throws java.io.IOException IOException
46+
* @deprecated use {@link #generateError(com.github.scribejava.core.model.Response) }
4647
*/
48+
@Deprecated
4749
public void generateError(String rawResponse) throws IOException {
48-
final JsonNode response = OBJECT_MAPPER.readTree(rawResponse);
50+
generateError(new Response(-1, null, null, rawResponse));
51+
}
52+
53+
/**
54+
* Related documentation: https://tools.ietf.org/html/rfc6749#section-5.2
55+
*
56+
* @param response response
57+
* @throws java.io.IOException IOException
58+
*
59+
*/
60+
public void generateError(Response response) throws IOException {
61+
final String responseBody = response.getBody();
62+
final JsonNode responseBodyJson;
63+
try {
64+
responseBodyJson = OBJECT_MAPPER.readTree(responseBody);
65+
} catch (JsonProcessingException ex) {
66+
throw new OAuth2AccessTokenErrorResponse(null, null, null, response);
67+
}
4968

50-
final JsonNode errorUriInString = response.get("error_uri");
69+
final JsonNode errorUriInString = responseBodyJson.get("error_uri");
5170
URI errorUri;
5271
try {
5372
errorUri = errorUriInString == null ? null : URI.create(errorUriInString.asText());
@@ -57,16 +76,17 @@ public void generateError(String rawResponse) throws IOException {
5776

5877
OAuth2Error errorCode;
5978
try {
60-
errorCode = OAuth2Error.parseFrom(extractRequiredParameter(response, "error", rawResponse).asText());
79+
errorCode = OAuth2Error
80+
.parseFrom(extractRequiredParameter(responseBodyJson, "error", responseBody).asText());
6181
} catch (IllegalArgumentException iaE) {
6282
//non oauth standard error code
6383
errorCode = null;
6484
}
6585

66-
final JsonNode errorDescription = response.get("error_description");
86+
final JsonNode errorDescription = responseBodyJson.get("error_description");
6787

6888
throw new OAuth2AccessTokenErrorResponse(errorCode, errorDescription == null ? null : errorDescription.asText(),
69-
errorUri, rawResponse);
89+
errorUri, response);
7090
}
7191

7292
private OAuth2AccessToken createToken(String rawResponse) throws IOException {

0 commit comments

Comments
 (0)