我尝试使用Apache Httpcomponents 4.5.1 在Instagram上进行oauth登录,但未能成功获取访问令牌。
我很确定图书馆本身存在问题,因为如果我卷曲,我会得到我想要的结果。
所以,我尝试了几种不同的方式来进行电话会议,但所有这些方式都给了我相同的结果,所以我发布了我发现使用流利的最优雅的方式 - 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:名称或服务不是 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)
现在我不知道如何继续。我使用此lib发出此请求的每一种方式都会返回此错误。试图添加Authentication头传递密钥:secret但也没有工作。
我错过了什么?
PS:我使用的是docker。
答案 0 :(得分:1)
行。它确实与Docker有关。我尝试在Docker之外运行代码的那一刻起作用了。
所以,我做的是:
$ ping api.instagram.com
PING instagram.c10r.facebook.com(173.252.120.81)56(84)字节数据。
64字节来自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"
现在它的工作正常。
答案 1 :(得分:0)
您的代码示例似乎没有使用HttpComponents API。基于tutorial的示例,我制作了这段代码。它返回400响应,因为它实际上是一个错误的请求,但至少它确实得到了响应。
public class HttpClientExample {
private static final String INSTAGRAM_ACCESS_TOKEN_URL = "https://api.instagram.com/oauth/access_token";
public static void main(String[] args) throws IOException {
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
formParams.add(new BasicNameValuePair("client_id", "someValue"));
formParams.add(new BasicNameValuePair("client_secret", "someValue"));
formParams.add(new BasicNameValuePair("grant_type", "someValue"));
formParams.add(new BasicNameValuePair("redirect_uri", "someValue"));
formParams.add(new BasicNameValuePair("code", "someValue"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8);
HttpPost post = new HttpPost(INSTAGRAM_ACCESS_TOKEN_URL);
post.setEntity(entity);
HttpClient client = HttpClients.createDefault();
HttpResponse response = client.execute(post);
System.out.println(response.getStatusLine());
System.out.println(response.getEntity());
}
}