InstagramのグラフAPIを、Spring BootのRestTemplateの中でコールしようとしているのですが、エラーが発生しており、解決方法をご教示いただきたいです。
エラーメッセージを見る限り、URIに含まれる波括弧 {}
が原因ではないかと思っているのですが、いくつか試してみたのですが解決することができていません。
なお、ブラウザやターミナルから実行した場合にレスポンスが正常に返ってくることは確認しています。
実行環境
・IDE : IntelliJ IDEA 2023.3.3 (Community Edition)
・Java 17
・Spring Boot 3.2.2
ブラウザやcurlでのレスポンス
{"business_discovery":{"followers_count":464946,"media_count":2193,"id":[ビジネスアカウント情報]},"id":[ビジネスアカウント情報]}
試してみたこと
{
を%7B
に変更、}
を%7D
に変更- 以下のようにURL直指定に変更
String url = "https://graph.facebook.com/v19.0/[ビジネスアカウント情報]?fields=business_discovery.username(bluebottle){followers_count,media_count}&access_token=[アクセストークン]";
参考サイト
Spring Bootリファレンス
https://spring.pleiades.io/guides/gs/consuming-rest
Instagram グラフAPIリファレンス
https://developers.facebook.com/docs/instagram-api/reference/ig-user
ItsApplication.java
package com.example.its;
import com.example.its.domain.instagram.InstagramAccountEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
@SpringBootApplication
public class ItsApplication {
public static final Logger log = LoggerFactory.getLogger(ItsApplication.class);
public static void main(String[] args) {
SpringApplication.run(ItsApplication.class, args);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
@Profile("!test")
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString("https://graph.facebook.com/v19.0/[ビジネスアカウント情報]")
.queryParam("fields", "business_discovery.username(bluebottle){followers_count,media_count}")
.queryParam("access_token", "[アクセストークン]");
String url = uriComponentsBuilder.toUriString();
InstagramAccountEntity instagramAccountEntity = restTemplate.getForObject(url, InstagramAccountEntity.class);
log.info(instagramAccountEntity.toString());
};
}
}
InstagramAccountEntity.java
package com.example.its.domain.instagram;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public record InstagramAccountEntity(BusinessDiscovery businessDiscovery, String id) {
}
BusinessDiscovery.java
package com.example.its.domain.instagram;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public record BusinessDiscovery(Long followers_count, Long media_count, String id) {
}
スタックトレースログ
org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: "{"error":{"message":"Syntax error \"Expected end of string instead of \"\u0025\".\" at character 39: business_discovery.username(bluebottle)\u00257Bfollowers_count,media_count\u00257D","type":"OAuthException","code":2500,"fbtrace_id":"A9VWRptjuOlOiWI3MFe03Ma"}}"
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:103) ~[spring-web-6.1.3.jar:6.1.3]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:183) ~[spring-web-6.1.3.jar:6.1.3]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:137) ~[spring-web-6.1.3.jar:6.1.3]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-6.1.3.jar:6.1.3]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:932) ~[spring-web-6.1.3.jar:6.1.3]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:881) ~[spring-web-6.1.3.jar:6.1.3]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:781) ~[spring-web-6.1.3.jar:6.1.3]
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:395) ~[spring-web-6.1.3.jar:6.1.3]
at com.example.its.ItsApplication.lambda$run$0(ItsApplication.java:67) ~[main/:na]
at org.springframework.boot.SpringApplication.lambda$callRunner$5(SpringApplication.java:790) ~[spring-boot-3.2.2.jar:3.2.2]
at org.springframework.util.function.ThrowingConsumer$1.acceptWithException(ThrowingConsumer.java:83) ~[spring-core-6.1.3.jar:6.1.3]
at org.springframework.util.function.ThrowingConsumer.accept(ThrowingConsumer.java:60) ~[spring-core-6.1.3.jar:6.1.3]
at org.springframework.util.function.ThrowingConsumer$1.accept(ThrowingConsumer.java:88) ~[spring-core-6.1.3.jar:6.1.3]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) ~[spring-boot-3.2.2.jar:3.2.2]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:789) ~[spring-boot-3.2.2.jar:3.2.2]
at org.springframework.boot.SpringApplication.lambda$callRunners$3(SpringApplication.java:774) ~[spring-boot-3.2.2.jar:3.2.2]
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) ~[na:na]
at java.base/java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:357) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:510) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[na:na]
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) ~[na:na]
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596) ~[na:na]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:774) ~[spring-boot-3.2.2.jar:3.2.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:341) ~[spring-boot-3.2.2.jar:3.2.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354) ~[spring-boot-3.2.2.jar:3.2.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-3.2.2.jar:3.2.2]
at com.example.its.ItsApplication.main(ItsApplication.java:25) ~[main/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]