1
votes

I'm trying to make an oauth login with Instagram using Apache Httpcomponents 4.5.1, but failing to succeed at getting the access token.

I'm pretty sure it's a problem with the library itself, since if I curl I get the result I wish.

So, I've trying a few different ways of doing the post call, but all of them gives me the same result, so I'll just post the most elegant way I found is using the 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();
    }

And the result I get is:

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)

Right now I have no idea how to proceed. Every way I make this request using this lib returns me this error. Tried to add the Authentication header passing key:secret but also didn't work.

What am I missing?

PS: I'm using docker.

2

2 Answers

1
votes

OK. It does indeed has to do with Docker. The moment I tried to run the code outside Docker it worked.

So, what I did was:

$ 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

And since I'm using docker-compose, I added to my common.yml config:

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

And now it's working just fine.

0
votes

Your code example does not seem to be using the HttpComponents API. Based on the example from the tutorial, I produced this code. It returns a 400 response, because it is in fact a bad request, but at least it does get a response.

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());
  }
}