Apache httpcomponent returns java.net.UnknownHostException: 名称或服务未知

Apache httpcomponent returns java.net.UnknownHostException: Name or service not known

我正在尝试使用 Apache Httpcomponents 4.5.1 通过 Instagram 进行 oauth 登录,但未能成功获取访问令牌。

我很确定这是库本身的问题,因为如果我卷曲我会得到我想要的结果。

所以,我尝试了几种不同的方式来进行 post 调用,但它们都给我相同的结果,所以我将 post 最优雅的方式发现正在使用 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: Name or service not known java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) java.net.InetAddress.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)

现在我不知道如何进行。我使用这个库 returns 发出这个请求的每一种方式都会出现这个错误。尝试添加身份验证 header 通过 key:secret 但也没有成功。

我错过了什么?

PS:我正在使用 docker。

您的代码示例似乎没有使用 HttpComponents API。基于 tutorial 中的示例,我生成了这段代码。它 returns 一个 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());
  }
}

好的。它确实与 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"

现在一切正常。