Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfb24008

Browse files
committed
add raw Response (with HTTP repsponse code and body) as member to the OAuth2AccessTokenErrorResponse
1 parent1dbeabb commitfb24008

File tree

12 files changed

+157
-47
lines changed

12 files changed

+157
-47
lines changed

‎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
packagecom.github.scribejava.apis.facebook;
22

33
importcom.github.scribejava.core.exceptions.OAuthException;
4+
importcom.github.scribejava.core.model.Response;
5+
importjava.io.IOException;
46
importjava.util.Objects;
57

68
/**
@@ -21,15 +23,32 @@ public class FacebookAccessTokenErrorResponse extends OAuthException {
2123
privatefinalStringtype;
2224
privatefinalintcodeInt;
2325
privatefinalStringfbtraceId;
24-
privatefinalStringrawResponse;
26+
privatefinalResponseresponse;
2527

2628
publicFacebookAccessTokenErrorResponse(Stringmessage,Stringtype,intcode,StringfbtraceId,
27-
StringrawResponse) {
29+
Responseresponse) {
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+
publicFacebookAccessTokenErrorResponse(Stringmessage,Stringtype,intcode,StringfbtraceId,
50+
StringrawResponse) {
51+
this(message,type,code,fbtraceId,newResponse(-1,null,null,rawResponse));
3352
}
3453

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

47-
publicStringgetRawResponse() {
48-
returnrawResponse;
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+
publicStringgetRawResponse()throwsIOException {
74+
returnresponse.getBody();
75+
}
76+
77+
publicResponsegetResponse() {
78+
returnresponse;
4979
}
5080

5181
@Override
5282
publicinthashCode() {
5383
inthash =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
returnfalse;
72102
}
73103
finalFacebookAccessTokenErrorResponseother = (FacebookAccessTokenErrorResponse)obj;
74-
if (!Objects.equals(rawResponse,other.getRawResponse())) {
104+
if (!Objects.equals(response,other.getResponse())) {
75105
returnfalse;
76106
}
77107
if (!Objects.equals(getMessage(),other.getMessage())) {
@@ -89,7 +119,7 @@ public boolean equals(Object obj) {
89119
@Override
90120
publicStringtoString() {
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
importcom.fasterxml.jackson.databind.JsonNode;
44
importcom.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
5+
importcom.github.scribejava.core.model.Response;
56
importjava.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-
publicvoidgenerateError(StringrawResponse)throwsIOException {
35-
finalJsonNodeerrorNode =OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER.readTree(rawResponse).get("error");
37+
publicvoidgenerateError(Responseresponse)throwsIOException {
38+
finalJsonNodeerrorNode =OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER
39+
.readTree(response.getBody())
40+
.get("error");
3641

3742
thrownewFacebookAccessTokenErrorResponse(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
packagecom.github.scribejava.apis.fitbit;
22

3+
importcom.fasterxml.jackson.core.JsonProcessingException;
34
importcom.fasterxml.jackson.databind.JsonNode;
45
importcom.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
56
importcom.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
7+
importcom.github.scribejava.core.model.Response;
68
importcom.github.scribejava.core.oauth2.OAuth2Error;
79
importjava.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-
publicvoidgenerateError(StringrawResponse)throwsIOException {
35-
finalJsonNodeerrorNode =OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER.readTree(rawResponse)
36-
.get("errors").get(0);
36+
publicvoidgenerateError(Responseresponse)throwsIOException {
37+
finalJsonNodeerrorNode;
38+
try {
39+
errorNode =OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER.readTree(response.getBody()).get("errors").get(0);
40+
}catch (JsonProcessingExceptionex) {
41+
thrownewOAuth2AccessTokenErrorResponse(null,null,null,response);
42+
}
3743

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

46-
thrownewOAuth2AccessTokenErrorResponse(errorCode,errorNode.get("message").asText(),null,rawResponse);
53+
thrownewOAuth2AccessTokenErrorResponse(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
packagecom.github.scribejava.apis.polar;
22

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

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

3435
@Override
35-
publicvoidgenerateError(StringrawResponse)throwsIOException {
36-
finalJsonNodeerrorNode =OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER.readTree(rawResponse)
37-
.get("errors").get(0);
36+
publicvoidgenerateError(Responseresponse)throwsIOException {
37+
finalJsonNodeerrorNode;
38+
try {
39+
errorNode =OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER.readTree(response.getBody()).get("errors").get(0);
40+
}catch (JsonProcessingExceptionex) {
41+
thrownewOAuth2AccessTokenErrorResponse(null,null,null,response);
42+
}
3843

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

47-
thrownewOAuth2AccessTokenErrorResponse(errorCode,errorNode.get("message").asText(),null,rawResponse);
53+
thrownewOAuth2AccessTokenErrorResponse(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
packagecom.github.scribejava.apis.fitbit;
22

33
importcom.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
4+
importcom.github.scribejava.core.model.Response;
45
importcom.github.scribejava.core.oauth2.OAuth2Error;
56
importjava.io.IOException;
67

@@ -28,7 +29,7 @@ public void testErrorExtraction() throws IOException {
2829
newThrowingRunnable() {
2930
@Override
3031
publicvoidrun()throwsThrowable {
31-
extractor.generateError(ERROR_JSON);
32+
extractor.generateError(newResponse(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
publicDeviceAuthorizationextract(Responseresponse)throwsIOException {
25-
26-
finalStringbody =response.getBody();
27-
2825
if (response.getCode() !=200) {
29-
generateError(body);
26+
generateError(response);
3027
}
31-
returncreateDeviceAuthorization(body);
28+
returncreateDeviceAuthorization(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
publicvoidgenerateError(StringrawResponse)throwsIOException {
35-
OAuth2AccessTokenJsonExtractor.instance().generateError(rawResponse);
39+
generateError(newResponse(-1,null,null,rawResponse));
40+
}
41+
42+
publicvoidgenerateError(Responseresponse)throwsIOException {
43+
OAuth2AccessTokenJsonExtractor.instance().generateError(response);
3644
}
3745

3846
privateDeviceAuthorizationcreateDeviceAuthorization(StringrawResponse)throwsIOException {

‎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
packagecom.github.scribejava.core.extractors;
22

3+
importcom.fasterxml.jackson.core.JsonProcessingException;
34
importcom.fasterxml.jackson.databind.JsonNode;
45
importjava.io.IOException;
56
importjava.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
returncreateToken(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
publicvoidgenerateError(StringrawResponse)throwsIOException {
48-
finalJsonNoderesponse =OBJECT_MAPPER.readTree(rawResponse);
50+
generateError(newResponse(-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+
publicvoidgenerateError(Responseresponse)throwsIOException {
61+
finalStringresponseBody =response.getBody();
62+
finalJsonNoderesponseBodyJson;
63+
try {
64+
responseBodyJson =OBJECT_MAPPER.readTree(responseBody);
65+
}catch (JsonProcessingExceptionex) {
66+
thrownewOAuth2AccessTokenErrorResponse(null,null,null,response);
67+
}
4968

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

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

66-
finalJsonNodeerrorDescription =response.get("error_description");
86+
finalJsonNodeerrorDescription =responseBodyJson.get("error_description");
6787

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

7292
privateOAuth2AccessTokencreateToken(StringrawResponse)throwsIOException {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp