简体   繁体   中英

oAuth Token with Java Apache http Client

I am trying to get an oAuth token from a Java client (Apache HTTP client). Unfortunately, I don't know much about the error message. I am not a professional in Java but I don't know if it's an error of mine or if it has something to do with the infrastructure, eg firewall or something similar. Here goes the Code:

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class App {

    public static void main(String[] args) throws ClientProtocolException, IOException {            
        final HttpUriRequest request = RequestBuilder.get()
                .setUri("SOME_URL")
                .setHeader("Content-Type", "application/x-www-form-urlencoded")
                .setHeader("Accept", "application/json")
                .setHeader("Authorization", "Basic ZXA6ZXAyMDE2")
                .addParameter("username", "USERNAME")
                .addParameter("password", "SOMEPASSWORD")
                .addParameter("grant_type", "password")
                .addParameter("scope", "read write")
                .build();
        final HttpClient client = HttpClientBuilder.create().build();
        final HttpResponse response = client.execute(request);
        final HttpEntity entity = response.getEntity();


        System.out.println(EntityUtils.toString(entity));


    }
}

**Edit:**Here comes the Error Message:

Dez 06, 2017 12:24:45 PM org.apache.http.impl.execchain.RetryExec execute
INFORMATION: I/O exception (java.net.SocketException) caught when processing request to {s}->https://someurl: Invalid argument: create
Dez 06, 2017 12:24:45 PM org.apache.http.impl.execchain.RetryExec execute
INFORMATION: Retrying request to {s}->someurl
Dez 06, 2017 12:24:45 PM org.apache.http.impl.execchain.RetryExec execute
INFORMATION: I/O exception (java.net.SocketException) caught when processing request to {s}->someurl: Invalid argument: create
Dez 06, 2017 12:24:45 PM org.apache.http.impl.execchain.RetryExec execute
INFORMATION: Retrying request to {s}->someurl
Dez 06, 2017 12:24:45 PM org.apache.http.impl.execchain.RetryExec execute
INFORMATION: I/O exception (java.net.SocketException) caught when processing request to {s}->someurl: Invalid argument: create
Dez 06, 2017 12:24:45 PM org.apache.http.impl.execchain.RetryExec execute
INFORMATION: Retrying request to {s}->someurl
Exception in thread "main" java.net.SocketException: Invalid argument: create
    at java.net.Socket.createImpl(Socket.java:460)
    at java.net.Socket.getImpl(Socket.java:520)
    at java.net.Socket.setSoTimeout(Socket.java:1141)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:120)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:373)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
    at App.main(App.java:28)

Here is my curl command i used instead:

curl -H "Authorization: Basic SOME_PW" --form "grant_type=password" --form "username=USERNAME" --form "password=PASSWORD" HERE_COMES_THE_URL --insecure

With this curl command i get a working token.

The problem was, as the user JEY said: I was using a GET request and not a Post request. To obtain a token, you have to use the Post request. What you do is sending your credentials and receiving a Token. Using REST you propably have to use a Post request when sending your credentials to Authenticate. In my case, i was using the payload as Parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM