Skip to content

Commit 9c8e4c1

Browse files
committed
* add Instagram (https://www.instagram.com/) API (thanks to https://github.com/faent)
* add getErrorMessage method to FacebookAccessTokenErrorResponse. Should be used instead of general getMessage method from the parent Exception
1 parent 78aac50 commit 9c8e4c1

File tree

14 files changed

+237
-264
lines changed

14 files changed

+237
-264
lines changed

changelog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
[SNAPSHOT]
2+
* add Instagram (https://www.instagram.com/) API (thanks to https://github.com/faent)
3+
* add getErrorMessage method to FacebookAccessTokenErrorResponse. Should be used instead of general getMessage method
4+
from the parent Exception
5+
16
[8.2.0]
27
* add ScopeBuilder to easily specify multiple scopes while requesting OAuth2.0 Access Tokens
38
* make Base64 en/de-coding not dependent from java8 implementation (use three optional implementation

scribejava-apis/src/main/java/com/github/scribejava/apis/FacebookApi.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
import com.github.scribejava.core.oauth2.clientauthentication.ClientAuthentication;
1414
import com.github.scribejava.core.oauth2.clientauthentication.RequestBodyAuthenticationScheme;
1515

16-
/**
17-
* Facebook API
18-
*/
1916
public class FacebookApi extends DefaultApi20 {
2017

2118
private final String version;

scribejava-apis/src/main/java/com/github/scribejava/apis/InstagramApi.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88
import com.github.scribejava.core.httpclient.HttpClient;
99
import com.github.scribejava.core.httpclient.HttpClientConfig;
1010
import com.github.scribejava.core.model.OAuth2AccessToken;
11-
import com.github.scribejava.core.model.Verb;
12-
import com.github.scribejava.core.oauth.OAuth20Service;
1311
import com.github.scribejava.core.oauth2.clientauthentication.ClientAuthentication;
1412
import com.github.scribejava.core.oauth2.clientauthentication.RequestBodyAuthenticationScheme;
1513

16-
/**
17-
* Instagram API
18-
*/
1914
public class InstagramApi extends DefaultApi20 {
2015

16+
public static final String LONG_LIVED_ACCESS_TOKEN_ENDPOINT = "https://graph.instagram.com/access_token";
17+
2118
private static class InstanceHolder {
2219

2320
private static final InstagramApi INSTANCE = new InstagramApi();
@@ -27,11 +24,6 @@ public static InstagramApi instance() {
2724
return InstanceHolder.INSTANCE;
2825
}
2926

30-
@Override
31-
public Verb getAccessTokenVerb() {
32-
return Verb.POST;
33-
}
34-
3527
@Override
3628
public String getAccessTokenEndpoint() {
3729
return "https://api.instagram.com/oauth/access_token";
@@ -49,19 +41,19 @@ protected String getAuthorizationBaseUrl() {
4941

5042
@Override
5143
public TokenExtractor<OAuth2AccessToken> getAccessTokenExtractor() {
52-
return InstagramAccessTokenJsonExtractor.instance();
44+
return InstagramAccessTokenJsonExtractor.instance();
5345
}
5446

5547
@Override
5648
public ClientAuthentication getClientAuthentication() {
57-
return RequestBodyAuthenticationScheme.instance();
49+
return RequestBodyAuthenticationScheme.instance();
5850
}
5951

6052
@Override
61-
public OAuth20Service createService(String apiKey, String apiSecret, String callback, String defaultScope,
62-
String responseType, OutputStream debugStream, String userAgent, HttpClientConfig httpClientConfig,
63-
HttpClient httpClient) {
64-
return new InstagramService(this, apiKey, apiSecret, callback, defaultScope, responseType,
65-
debugStream, userAgent, httpClientConfig, httpClient);
53+
public InstagramService createService(String apiKey, String apiSecret, String callback, String defaultScope,
54+
String responseType, OutputStream debugStream, String userAgent, HttpClientConfig httpClientConfig,
55+
HttpClient httpClient) {
56+
return new InstagramService(this, apiKey, apiSecret, callback, defaultScope, responseType, debugStream,
57+
userAgent, httpClientConfig, httpClient);
6658
}
6759
}
Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.scribejava.apis.facebook;
22

3-
import com.github.scribejava.core.exceptions.OAuthException;
3+
import com.github.scribejava.core.model.OAuthResponseException;
44
import com.github.scribejava.core.model.Response;
55
import java.io.IOException;
66
import java.util.Objects;
@@ -16,39 +16,27 @@
1616
* '{"error":{"message":"Error validating application. Invalid application
1717
* ID.","type":"OAuthException","code":101,"fbtrace_id":"CvDR+X4WWIx"}}'
1818
*/
19-
public class FacebookAccessTokenErrorResponse extends OAuthException {
19+
public class FacebookAccessTokenErrorResponse extends OAuthResponseException {
2020

2121
private static final long serialVersionUID = -1277129766099856895L;
2222

23+
private final String errorMessage;
2324
private final String type;
2425
private final int codeInt;
2526
private final String fbtraceId;
26-
private final Response response;
2727

28-
public FacebookAccessTokenErrorResponse(String message, String type, int code, String fbtraceId,
29-
Response response) {
30-
super(message);
28+
public FacebookAccessTokenErrorResponse(String errorMessage, String type, int code, String fbtraceId,
29+
Response response)
30+
throws IOException {
31+
super(response);
32+
this.errorMessage = errorMessage;
3133
this.type = type;
3234
this.codeInt = code;
3335
this.fbtraceId = fbtraceId;
34-
this.response = response;
3536
}
3637

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));
38+
public String getErrorMessage() {
39+
return errorMessage;
5240
}
5341

5442
public String getType() {
@@ -63,26 +51,10 @@ public String getFbtraceId() {
6351
return fbtraceId;
6452
}
6553

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;
79-
}
80-
8154
@Override
8255
public int hashCode() {
83-
int hash = 5;
84-
hash = 83 * hash + Objects.hashCode(response);
85-
hash = 83 * hash + Objects.hashCode(getMessage());
56+
int hash = super.hashCode();
57+
hash = 83 * hash + Objects.hashCode(errorMessage);
8658
hash = 83 * hash + Objects.hashCode(type);
8759
hash = 83 * hash + Objects.hashCode(codeInt);
8860
hash = 83 * hash + Objects.hashCode(fbtraceId);
@@ -100,11 +72,13 @@ public boolean equals(Object obj) {
10072
if (getClass() != obj.getClass()) {
10173
return false;
10274
}
103-
final FacebookAccessTokenErrorResponse other = (FacebookAccessTokenErrorResponse) obj;
104-
if (!Objects.equals(response, other.getResponse())) {
75+
if (!super.equals(obj)) {
10576
return false;
10677
}
107-
if (!Objects.equals(getMessage(), other.getMessage())) {
78+
79+
final FacebookAccessTokenErrorResponse other = (FacebookAccessTokenErrorResponse) obj;
80+
81+
if (!Objects.equals(errorMessage, other.getErrorMessage())) {
10882
return false;
10983
}
11084
if (!Objects.equals(type, other.getType())) {
@@ -119,7 +93,7 @@ public boolean equals(Object obj) {
11993
@Override
12094
public String toString() {
12195
return "FacebookAccessTokenErrorResponse{'type'='" + type + "', 'codeInt'='" + codeInt
122-
+ "', 'fbtraceId'='" + fbtraceId + "', 'response'='" + response
123-
+ "', 'message'='" + getMessage() + "'}";
96+
+ "', 'fbtraceId'='" + fbtraceId + "', 'response'='" + getResponse()
97+
+ "', 'errorMessage'='" + errorMessage + "'}";
12498
}
12599
}
Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,84 @@
11
package com.github.scribejava.apis.instagram;
22

3+
import com.github.scribejava.core.model.OAuthResponseException;
34
import java.io.IOException;
45
import java.util.Objects;
5-
import com.github.scribejava.core.exceptions.OAuthException;
66
import com.github.scribejava.core.model.Response;
77

88
/**
9-
* non standard Instagram replace for
10-
* {@link com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse}
9+
* non standard Instagram replace for {@link com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse}
1110
*
1211
* examples:<br>
1312
*
1413
* '{"error_type": "OAuthException", "code": 400, "error_message": "Missing required field client_id"}'
1514
*/
16-
public class InstagramAccessTokenErrorResponse extends OAuthException {
15+
public class InstagramAccessTokenErrorResponse extends OAuthResponseException {
1716

18-
private static final long serialVersionUID = -1277129766099856895L;
17+
private static final long serialVersionUID = -1277129706699856895L;
1918

2019
private final String errorType;
21-
private final int codeInt;
20+
private final int code;
21+
private final String errorMessage;
2222
private final Response response;
2323

24-
public InstagramAccessTokenErrorResponse(String errorType, int code,
25-
String errorMessage, Response response) {
26-
super(errorMessage);
24+
public InstagramAccessTokenErrorResponse(String errorType, int code, String errorMessage, Response response)
25+
throws IOException {
26+
super(response);
2727
this.errorType = errorType;
28-
this.codeInt = code;
28+
this.code = code;
29+
this.errorMessage = errorMessage;
2930
this.response = response;
3031
}
3132

3233
public String getErrorType() {
3334
return errorType;
3435
}
3536

36-
public int getCodeInt() {
37-
return codeInt;
37+
public int getCode() {
38+
return code;
3839
}
3940

40-
/**
41-
*
42-
* @return body of response
43-
* @throws IOException IOException
44-
* @deprecated use {@link #getResponse()} and then {@link Response#getBody()}
45-
*/
46-
@Deprecated
47-
public String getRawResponse() throws IOException {
48-
return response.getBody();
49-
}
50-
51-
public Response getResponse() {
52-
return response;
41+
public String getErrorMessage() {
42+
return errorMessage;
5343
}
5444

5545
@Override
56-
public boolean equals(Object o) {
57-
if (this == o) {
58-
return true;
46+
public boolean equals(Object obj) {
47+
if (this == obj) {
48+
return true;
49+
}
50+
if (obj == null || getClass() != obj.getClass()) {
51+
return false;
5952
}
60-
if (o == null || getClass() != o.getClass()) {
61-
return false;
53+
if (!super.equals(obj)) {
54+
return false;
55+
}
56+
57+
final InstagramAccessTokenErrorResponse that = (InstagramAccessTokenErrorResponse) obj;
58+
if (!Objects.equals(errorMessage, that.getErrorMessage())) {
59+
return false;
6260
}
63-
InstagramAccessTokenErrorResponse that = (InstagramAccessTokenErrorResponse) o;
64-
return codeInt == that.codeInt && Objects.equals(errorType, that.errorType)
65-
&& Objects.equals(response, that.response);
61+
return code == that.code && Objects.equals(errorType, that.errorType)
62+
&& Objects.equals(response, that.response);
6663
}
6764

6865
@Override
6966
public int hashCode() {
70-
int hash = 5;
67+
int hash = super.hashCode();
7168
hash = 83 * hash + Objects.hashCode(response);
72-
hash = 83 * hash + Objects.hashCode(getMessage());
69+
hash = 83 * hash + Objects.hashCode(errorMessage);
7370
hash = 83 * hash + Objects.hashCode(errorType);
74-
hash = 83 * hash + Objects.hashCode(codeInt);
71+
hash = 83 * hash + Objects.hashCode(code);
7572
return hash;
7673
}
7774

7875
@Override
7976
public String toString() {
80-
return "InstagramAccessTokenErrorResponse{" +
81-
"errorType='" + errorType + '\'' +
82-
", codeInt=" + codeInt +
83-
", response=" + response +
84-
'}';
77+
return "InstagramAccessTokenErrorResponse{"
78+
+ "errorType='" + errorType
79+
+ "', code=" + code
80+
+ "', errorMessage='" + errorMessage
81+
+ "', response=" + response
82+
+ '}';
8583
}
8684
}

scribejava-apis/src/main/java/com/github/scribejava/apis/instagram/InstagramAccessTokenJsonExtractor.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import com.github.scribejava.core.model.Response;
88

99
/**
10-
* non standard Facebook Extractor
10+
* non standard Instagram Extractor
1111
*/
1212
public class InstagramAccessTokenJsonExtractor extends OAuth2AccessTokenJsonExtractor {
1313

@@ -24,36 +24,32 @@ public static InstagramAccessTokenJsonExtractor instance() {
2424
}
2525

2626
/**
27-
* Non standard error message. Could be Instagram or Facebook specific.
28-
* Usually Instagram type is used for getting access tokens. Facebook type is used for
29-
* refreshing tokens.
27+
* Non standard error message. Could be Instagram or Facebook specific. Usually Instagram type is used for getting
28+
* access tokens. Facebook type is used for refreshing tokens.
3029
*
3130
* examples:<br>
3231
*
33-
* Instagram specific:
34-
* '{"error_type": "OAuthException", "code": 400, "error_message": "Missing required field client_id"}'
32+
* Instagram specific: '{"error_type": "OAuthException", "code": 400, "error_message": "Missing required field
33+
* client_id"}'
3534
*
36-
* Facebook specific:
37-
* '{"error":{"message":"Error validating application. Invalid application
35+
* Facebook specific: '{"error":{"message":"Error validating application. Invalid application
3836
* ID.","type":"OAuthException","code":101,"fbtrace_id":"CvDR+X4WWIx"}}'
3937
*
4038
* @param response response
4139
*/
4240
@Override
4341
public void generateError(Response response) throws IOException {
44-
final JsonNode errorNode = OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER
45-
.readTree(response.getBody());
46-
JsonNode error = errorNode.get("error");
42+
final JsonNode errorNode = OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER.readTree(response.getBody());
43+
final JsonNode error = errorNode.get("error");
4744
if (error != null) {
48-
FacebookAccessTokenJsonExtractor.instance().generateError(response);
45+
FacebookAccessTokenJsonExtractor.instance().generateError(response);
4946
} else {
50-
throw new InstagramAccessTokenErrorResponse(
51-
errorNode.get("error_type").asText(),
52-
errorNode.get("code").asInt(),
53-
errorNode.get("error_message").asText(),
54-
response
55-
);
47+
throw new InstagramAccessTokenErrorResponse(
48+
errorNode.get("error_type").asText(),
49+
errorNode.get("code").asInt(),
50+
errorNode.get("error_message").asText(),
51+
response
52+
);
5653
}
5754
}
58-
5955
}

0 commit comments

Comments
 (0)