我尝试使用 Apache Httpcomponents 4.5.1 通过 Instagram 进行 oauth 登录,但未能成功获取访问 token 。
我很确定这是库本身的问题,因为如果我 curl 我会得到我想要的结果。
所以,我尝试了几种不同的方法来进行后调用,但所有这些方法都给了我相同的结果,所以我将发布我发现的最优雅的方法是使用 Fluent-hc lib:
@Value("${instagram.client.id}")
private String clientID;
@Value("${instagram.client.secret}")
private String clientSecret;
@Value("${instagram.redirect.uri}")
private String redirectURI;
private static final String INSTAGRAM_ACCESS_TOKEN_URL = "https://api.instagram.com/oauth/access_token";
private Content requestAccessToken(String code, UserType userType) throws IOException {
// String authorization_header_string = URLEncoder.encode(clientID + ":" + clientSecret, "UTF-8");
return Request.Post(INSTAGRAM_ACCESS_TOKEN_URL)
.bodyForm(Form.form()
.add("client_id", clientID)
.add("client_secret", clientSecret)
.add("grant_type", "authorization_code")
.add("redirect_uri", redirectURI + "?type=" + userType)
.add("code", code)
.build())
// .addHeader("Authorization", authorization_header_string)
.execute().returnContent();
}
我得到的结果是:
java.net.UnknownHostException: api.instagram.com: Name or service not known java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:922) java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1316) java.net.InetAddress.getAllByName0(InetAddress.java:1269) java.net.InetAddress.getAllByName(InetAddress.java:1185) java.net.InetAddress.getAllByName(InetAddress.java:1119) org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45) org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:111) org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) org.apache.http.impl.client.InternaleHttpClient.execute(CloseableHttpClient.java:82) org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) org.apache.http.client.fluent.Request.internalExecute(Request.java:173) org.apache.http.client.fluent.Request.execute(Request.java:177)HttpClient.doExecute(InternalHttpClient.java:184) org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) org.apache.http.client.fluent.Request.internalExecute(Request.java:173) org.apache.http.client.fluent.Request.execute(Request.java:177)
现在我不知道如何继续。我使用此库发出此请求的每种方式都会返回此错误。尝试添加身份验证 header 传递 key:secret 但也不起作用。
我错过了什么?
PS:我正在使用 Docker。
最佳答案
好的。确实和Docker有关系。当我尝试在 Docker 外部运行代码时,它起作用了。
所以,我所做的是:
$ ping api.instagram.com
PING instagram.c10r.facebook.com (173.252.120.81) 56(84) bytes of data.
64 bytes from instagram-p3-shv-12-frc3.fbcdn.net (173.252.120.81): icmp_seq=1 ttl=74 time=146 ms
由于我使用的是 docker-compose,所以我添加到了我的 common.yml 配置中:
extra_hosts:
- "api.instagram.com:173.252.120.81"
现在一切正常。
关于java - Apache httpcomponent 返回 java.net.UnknownHostException : Name or service not known,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34143588/