Get access token with okhttp

Hi All,
I have an issue to fetch the access token by using OKhttp query. I manage to do it with postman and powershell but when I try it in Java, I got an unknown host exception.

public String getToken() throws IOException {

    OkHttpClient client = new OkHttpClient();

    MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
    String params = "grant_type=password&username=user_email&password=pwd&scope=openid";
    RequestBody body = RequestBody.create(params, mediaType);
    Request request = new Request.Builder()
            .url("https://dev-***********.okta.com/oauth2/default/v1/token")
            .post(body)
            .addHeader("Accept", "application/json")
            .addHeader("authorization", "Basic MG9hOTZ4amd6OFZxalBieXM1ZDc6WGUyejBncjVxMkhyUmQxWkttdGZqQmFzTnh4WDVNTDBLSjh3VjZDVQ")
            .addHeader("Content-Type", "application/x-www-form-urlencoded")
            .build();

    try (Response response = client.newCall(request).execute()) {
        return response.body().string();
    }
}

I don t manage to see where is the mistake. If you have an idea.

Regards
Mat

Hello,

Are you running this from a different host (container) then where you test with postman/powershell?
Sounds like maybe a dns/network issue, or you have a typo in your Org URL.

Hi Erik,
No I work directly from my laptop. I agree with you about the dns. I think this issue come from my proxy.
thanks

So after few other tests, I confirm that my bug was due to my proxy which was not taken in account by IntelliJ. After forcing it in java args, it s ok now.
Thanks

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.