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

Commit78aac50

Browse files
author
Maria Besfamilnaya
committed
add Instagram API (https://www.instagram.com/)
1 parent02cf695 commit78aac50

File tree

7 files changed

+389
-1
lines changed

7 files changed

+389
-1
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ ScribeJava support out-of-box several HTTP clients:
7777
* HeadHunter ХэдХантер (https://hh.ru/)[example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/HHExample.java)
7878
* HiOrg-Server (https://www.hiorg-server.de/)[example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/HiOrgServerExample.java)
7979
* Imgur (http://imgur.com/)[example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/ImgurExample.java)
80+
* Instagram (https://www.instagram.com/)[example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/InstagramExample.java)
8081
* Kaixin 开心网 (http://www.kaixin001.com/)[example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/Kaixin20Example.java)
8182
* Kakao (https://kakao.com/)[example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/KakaoExample.java)
8283
* Keycloak (https://www.keycloak.org/)[example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/KeycloakExample.java)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
packagecom.github.scribejava.apis;
2+
3+
importjava.io.OutputStream;
4+
importcom.github.scribejava.apis.instagram.InstagramAccessTokenJsonExtractor;
5+
importcom.github.scribejava.apis.instagram.InstagramService;
6+
importcom.github.scribejava.core.builder.api.DefaultApi20;
7+
importcom.github.scribejava.core.extractors.TokenExtractor;
8+
importcom.github.scribejava.core.httpclient.HttpClient;
9+
importcom.github.scribejava.core.httpclient.HttpClientConfig;
10+
importcom.github.scribejava.core.model.OAuth2AccessToken;
11+
importcom.github.scribejava.core.model.Verb;
12+
importcom.github.scribejava.core.oauth.OAuth20Service;
13+
importcom.github.scribejava.core.oauth2.clientauthentication.ClientAuthentication;
14+
importcom.github.scribejava.core.oauth2.clientauthentication.RequestBodyAuthenticationScheme;
15+
16+
/**
17+
* Instagram API
18+
*/
19+
publicclassInstagramApiextendsDefaultApi20 {
20+
21+
privatestaticclassInstanceHolder {
22+
23+
privatestaticfinalInstagramApiINSTANCE =newInstagramApi();
24+
}
25+
26+
publicstaticInstagramApiinstance() {
27+
returnInstanceHolder.INSTANCE;
28+
}
29+
30+
@Override
31+
publicVerbgetAccessTokenVerb() {
32+
returnVerb.POST;
33+
}
34+
35+
@Override
36+
publicStringgetAccessTokenEndpoint() {
37+
return"https://api.instagram.com/oauth/access_token";
38+
}
39+
40+
@Override
41+
publicStringgetRefreshTokenEndpoint() {
42+
return"https://graph.instagram.com/refresh_access_token";
43+
}
44+
45+
@Override
46+
protectedStringgetAuthorizationBaseUrl() {
47+
return"https://api.instagram.com/oauth/authorize";
48+
}
49+
50+
@Override
51+
publicTokenExtractor<OAuth2AccessToken>getAccessTokenExtractor() {
52+
returnInstagramAccessTokenJsonExtractor.instance();
53+
}
54+
55+
@Override
56+
publicClientAuthenticationgetClientAuthentication() {
57+
returnRequestBodyAuthenticationScheme.instance();
58+
}
59+
60+
@Override
61+
publicOAuth20ServicecreateService(StringapiKey,StringapiSecret,Stringcallback,StringdefaultScope,
62+
StringresponseType,OutputStreamdebugStream,StringuserAgent,HttpClientConfighttpClientConfig,
63+
HttpClienthttpClient) {
64+
returnnewInstagramService(this,apiKey,apiSecret,callback,defaultScope,responseType,
65+
debugStream,userAgent,httpClientConfig,httpClient);
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
packagecom.github.scribejava.apis.instagram;
2+
3+
importjava.io.IOException;
4+
importjava.util.Objects;
5+
importcom.github.scribejava.core.exceptions.OAuthException;
6+
importcom.github.scribejava.core.model.Response;
7+
8+
/**
9+
* non standard Instagram replace for
10+
* {@link com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse}
11+
*
12+
* examples:<br>
13+
*
14+
* '{"error_type": "OAuthException", "code": 400, "error_message": "Missing required field client_id"}'
15+
*/
16+
publicclassInstagramAccessTokenErrorResponseextendsOAuthException {
17+
18+
privatestaticfinallongserialVersionUID = -1277129766099856895L;
19+
20+
privatefinalStringerrorType;
21+
privatefinalintcodeInt;
22+
privatefinalResponseresponse;
23+
24+
publicInstagramAccessTokenErrorResponse(StringerrorType,intcode,
25+
StringerrorMessage,Responseresponse) {
26+
super(errorMessage);
27+
this.errorType =errorType;
28+
this.codeInt =code;
29+
this.response =response;
30+
}
31+
32+
publicStringgetErrorType() {
33+
returnerrorType;
34+
}
35+
36+
publicintgetCodeInt() {
37+
returncodeInt;
38+
}
39+
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+
publicStringgetRawResponse()throwsIOException {
48+
returnresponse.getBody();
49+
}
50+
51+
publicResponsegetResponse() {
52+
returnresponse;
53+
}
54+
55+
@Override
56+
publicbooleanequals(Objecto) {
57+
if (this ==o) {
58+
returntrue;
59+
}
60+
if (o ==null ||getClass() !=o.getClass()) {
61+
returnfalse;
62+
}
63+
InstagramAccessTokenErrorResponsethat = (InstagramAccessTokenErrorResponse)o;
64+
returncodeInt ==that.codeInt &&Objects.equals(errorType,that.errorType)
65+
&&Objects.equals(response,that.response);
66+
}
67+
68+
@Override
69+
publicinthashCode() {
70+
inthash =5;
71+
hash =83 *hash +Objects.hashCode(response);
72+
hash =83 *hash +Objects.hashCode(getMessage());
73+
hash =83 *hash +Objects.hashCode(errorType);
74+
hash =83 *hash +Objects.hashCode(codeInt);
75+
returnhash;
76+
}
77+
78+
@Override
79+
publicStringtoString() {
80+
return"InstagramAccessTokenErrorResponse{" +
81+
"errorType='" +errorType +'\'' +
82+
", codeInt=" +codeInt +
83+
", response=" +response +
84+
'}';
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
packagecom.github.scribejava.apis.instagram;
2+
3+
importjava.io.IOException;
4+
importcom.fasterxml.jackson.databind.JsonNode;
5+
importcom.github.scribejava.apis.facebook.FacebookAccessTokenJsonExtractor;
6+
importcom.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
7+
importcom.github.scribejava.core.model.Response;
8+
9+
/**
10+
* non standard Facebook Extractor
11+
*/
12+
publicclassInstagramAccessTokenJsonExtractorextendsOAuth2AccessTokenJsonExtractor {
13+
14+
protectedInstagramAccessTokenJsonExtractor() {
15+
}
16+
17+
privatestaticclassInstanceHolder {
18+
19+
privatestaticfinalInstagramAccessTokenJsonExtractorINSTANCE =newInstagramAccessTokenJsonExtractor();
20+
}
21+
22+
publicstaticInstagramAccessTokenJsonExtractorinstance() {
23+
returnInstanceHolder.INSTANCE;
24+
}
25+
26+
/**
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.
30+
*
31+
* examples:<br>
32+
*
33+
* Instagram specific:
34+
* '{"error_type": "OAuthException", "code": 400, "error_message": "Missing required field client_id"}'
35+
*
36+
* Facebook specific:
37+
* '{"error":{"message":"Error validating application. Invalid application
38+
* ID.","type":"OAuthException","code":101,"fbtrace_id":"CvDR+X4WWIx"}}'
39+
*
40+
* @param response response
41+
*/
42+
@Override
43+
publicvoidgenerateError(Responseresponse)throwsIOException {
44+
finalJsonNodeerrorNode =OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER
45+
.readTree(response.getBody());
46+
JsonNodeerror =errorNode.get("error");
47+
if (error !=null) {
48+
FacebookAccessTokenJsonExtractor.instance().generateError(response);
49+
}else {
50+
thrownewInstagramAccessTokenErrorResponse(
51+
errorNode.get("error_type").asText(),
52+
errorNode.get("code").asInt(),
53+
errorNode.get("error_message").asText(),
54+
response
55+
);
56+
}
57+
}
58+
59+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
packagecom.github.scribejava.apis.instagram;
2+
3+
importjava.io.IOException;
4+
importjava.io.OutputStream;
5+
importjava.util.concurrent.ExecutionException;
6+
importcom.github.scribejava.core.builder.api.DefaultApi20;
7+
importcom.github.scribejava.core.httpclient.HttpClient;
8+
importcom.github.scribejava.core.httpclient.HttpClientConfig;
9+
importcom.github.scribejava.core.model.OAuth2AccessToken;
10+
importcom.github.scribejava.core.model.OAuthConstants;
11+
importcom.github.scribejava.core.model.OAuthRequest;
12+
importcom.github.scribejava.core.model.Verb;
13+
importcom.github.scribejava.core.oauth.OAuth20Service;
14+
15+
publicclassInstagramServiceextendsOAuth20Service {
16+
17+
privatestaticfinalStringLONG_LIVED_ACCESS_TOKEN_ENDPOINT ="https://graph.instagram.com/access_token";
18+
19+
publicInstagramService(DefaultApi20api,StringapiKey,StringapiSecret,Stringcallback,
20+
StringdefaultScope,StringresponseType,OutputStreamdebugStream,StringuserAgent,
21+
HttpClientConfighttpClientConfig,HttpClienthttpClient) {
22+
super(api,apiKey,apiSecret,callback,defaultScope,responseType,debugStream,
23+
userAgent,httpClientConfig,httpClient);
24+
}
25+
26+
/**
27+
* Refresh a long-lived Instagram User Access Token that is at least 24 hours old but has not expired.
28+
* Refreshed tokens are valid for 60 days from the date at which they are refreshed.
29+
*
30+
* @param accessToken long-lived access token
31+
* @param scope (not used)
32+
* @return refresh token request
33+
*/
34+
@Override
35+
protectedOAuthRequestcreateRefreshTokenRequest(StringaccessToken,Stringscope) {
36+
if (accessToken ==null ||accessToken.isEmpty()) {
37+
thrownewIllegalArgumentException("The refreshToken cannot be null or empty");
38+
}
39+
finalOAuthRequestrequest =newOAuthRequest(Verb.GET,getApi().getRefreshTokenEndpoint());
40+
41+
request.addParameter(OAuthConstants.GRANT_TYPE,"ig_refresh_token");
42+
request.addParameter(OAuthConstants.ACCESS_TOKEN,accessToken);
43+
44+
logRequestWithParams("refresh token",request);
45+
46+
returnrequest;
47+
}
48+
49+
/**
50+
* Get long-lived access token.
51+
*
52+
* Initial accessToken is valid for 1 hour so one can get long-lived access token.
53+
* Long-lived access token is valid for 60 days.
54+
*
55+
* @param accessToken short-lived access token
56+
* @return long-lived access token with filled expireIn and refreshToken
57+
*/
58+
publicOAuth2AccessTokengetLongLivedAccessToken(OAuth2AccessTokenaccessToken)
59+
throwsInterruptedException,ExecutionException,IOException {
60+
StringshortLivedAccessToken =accessToken.getAccessToken();
61+
OAuthRequestrequest =createLongLivedAccessTokenRequest(shortLivedAccessToken);
62+
returnsendAccessTokenRequestSync(request);
63+
}
64+
65+
privateOAuthRequestcreateLongLivedAccessTokenRequest(StringshortLivedAccessToken) {
66+
finalOAuthRequestrequest =newOAuthRequest(Verb.GET,LONG_LIVED_ACCESS_TOKEN_ENDPOINT);
67+
68+
getApi().getClientAuthentication().addClientAuthentication(request,getApiKey(),getApiSecret());
69+
70+
request.addParameter(OAuthConstants.GRANT_TYPE,"ig_exchange_token");
71+
request.addParameter(OAuthConstants.ACCESS_TOKEN,shortLivedAccessToken);
72+
73+
if (isDebug()) {
74+
log("created long-lived access token request with body params [%s], query string params [%s]",
75+
request.getBodyParams().asFormUrlEncodedString(),
76+
request.getQueryStringParams().asFormUrlEncodedString());
77+
}
78+
returnrequest;
79+
}
80+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
packagecom.github.scribejava.apis.examples;
2+
3+
importjava.io.IOException;
4+
importjava.util.Random;
5+
importjava.util.Scanner;
6+
importjava.util.concurrent.ExecutionException;
7+
importcom.github.scribejava.apis.InstagramApi;
8+
importcom.github.scribejava.apis.instagram.InstagramService;
9+
importcom.github.scribejava.core.builder.ServiceBuilder;
10+
importcom.github.scribejava.core.model.OAuth2AccessToken;
11+
importcom.github.scribejava.core.model.OAuthRequest;
12+
importcom.github.scribejava.core.model.Response;
13+
importcom.github.scribejava.core.model.Verb;
14+
importcom.github.scribejava.core.oauth.OAuth20Service;
15+
16+
publicclassInstagramExample {
17+
18+
privatestaticfinalStringNETWORK_NAME ="Instagram";
19+
privatestaticfinalStringPROTECTED_RESOURCE_URL =
20+
"https://graph.instagram.com/me/media?fields=id,caption,media_type,media_url,username";
21+
22+
privateInstagramExample() {
23+
}
24+
25+
@SuppressWarnings("PMD.SystemPrintln")
26+
publicstaticvoidmain(String...args)throwsIOException,InterruptedException,ExecutionException {
27+
// Replace these with your client id and secret
28+
finalStringclientId ="client-id";
29+
finalStringclientSecret ="client-secret";
30+
finalStringsecretState ="secret" +newRandom().nextInt(999_999);
31+
finalOAuth20Serviceservice =newServiceBuilder(clientId)
32+
.apiSecret(clientSecret)
33+
.defaultScope("user_profile,user_media")
34+
.callback("http://example.com")
35+
.build(InstagramApi.instance());
36+
37+
finalScannerin =newScanner(System.in,"UTF-8");
38+
39+
System.out.println("=== " +NETWORK_NAME +"'s OAuth Workflow ===");
40+
System.out.println();
41+
42+
// Obtain the Authorization URL
43+
System.out.println("Fetching the Authorization URL...");
44+
finalStringauthorizationUrl =service.getAuthorizationUrl(secretState);
45+
System.out.println("Got the Authorization URL!");
46+
System.out.println("Now go and authorize ScribeJava here:");
47+
System.out.println(authorizationUrl);
48+
System.out.println("And paste the authorization code here");
49+
System.out.print(">>");
50+
finalStringcode =in.nextLine();
51+
System.out.println();
52+
53+
System.out.println("And paste the state from server here. We have set 'secretState'='" +secretState +"'.");
54+
System.out.print(">>");
55+
finalStringvalue =in.nextLine();
56+
if (secretState.equals(value)) {
57+
System.out.println("State value does match!");
58+
}else {
59+
System.out.println("Ooops, state value does not match!");
60+
System.out.println("Expected = " +secretState);
61+
System.out.println("Got = " +value);
62+
System.out.println();
63+
}
64+
65+
System.out.println("Trading the Authorization Code for an Access Token...");
66+
finalOAuth2AccessTokenaccessToken =service.getAccessToken(code);
67+
System.out.println("Got the Access Token!");
68+
System.out.println("(The raw response looks like this: " +accessToken.getRawResponse() +"')");
69+
System.out.println();
70+
71+
// Now let's go and ask for a protected resource!
72+
System.out.println("Now we're going to access a protected resource...");
73+
finalOAuthRequestrequest =newOAuthRequest(Verb.GET,PROTECTED_RESOURCE_URL);
74+
service.signRequest(accessToken,request);
75+
try (Responseresponse =service.execute(request)) {
76+
System.out.println("Got it! Lets see what we found...");
77+
System.out.println();
78+
System.out.println(response.getCode());
79+
System.out.println(response.getBody());
80+
}
81+
System.out.println();
82+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
83+
84+
InstagramServiceinstagramService = (InstagramService)service;
85+
System.out.println("Now let's exchange our short-lived token to long-lived access token...");
86+
OAuth2AccessTokenlongLivedAccessToken =instagramService.getLongLivedAccessToken(accessToken);
87+
System.out.println("Got the Access Token!");
88+
System.out.println("(The access token raw response looks like this: " +longLivedAccessToken.getRawResponse() +"')");
89+
90+
System.out.println("Now it's time to refresh long-lived token...");
91+
OAuth2AccessTokenrefreshAccessToken =service.refreshAccessToken(longLivedAccessToken.getAccessToken());
92+
System.out.println("Got the refreshed Access Token!");
93+
System.out.println("(The refreshed access token raw response looks like this: " +refreshAccessToken.getRawResponse() +"')");
94+
}
95+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp