【发布时间】:2016-03-12 15:31:47
【问题描述】:
我正在尝试使用 Apache Httpcomponents 4.5.1 通过 Instagram 进行 oauth 登录,但未能成功获取访问令牌。
我很确定这是库本身的问题,因为如果我 curl 我会得到我想要的结果。
所以,我尝试了几种不同的方式来进行 post call,但它们都给了我相同的结果,所以我将发布我发现的最优雅的方式是使用 fluent-hc 库:
@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: 名称或服务不是 已知 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)
现在我不知道如何继续。我使用这个库提出这个请求的每一种方式都会给我这个错误。尝试添加 Authentication 标头传递 key:secret 但也没有用。
我错过了什么?
PS:我正在使用 docker。
【问题讨论】:
标签: java docker http-post httpclient apache-httpclient-4.x