【问题标题】:Apache httpcomponent returns java.net.UnknownHostException: Name or service not knownApache httpcomponent 返回 java.net.UnknownHostException: Name or service not known
【发布时间】: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


    【解决方案1】:

    好的。它确实与 Docker 有关。当我尝试在 Docker 之外运行代码时,它就起作用了。

    所以,我所做的是:

    $ ping api.instagram.com

    PING instagram.c10r.facebook.com (173.252.120.81) 56(84) 字节数据。

    来自 instagram-p3-shv-12-frc3.fbcdn.net (173.252.120.81) 的 64 个字节:icmp_seq=1 ttl=74 time=146 ms

    由于我使用的是 docker-compose,因此我将其添加到我的 common.yml 配置中:

      extra_hosts:
        - "api.instagram.com:173.252.120.81"
    

    现在它工作得很好。

    【讨论】:

    • 我想你不知道这对我有多大帮助。一整天都在解决一个问题,这就像魔术一样解决了它。
    【解决方案2】:

    您的代码示例似乎没有使用 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());
      }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-27
    • 2014-03-22
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多