Problems generating Oauth access token with Httpurlconnection

Asked

Viewed 205 times

0

I’m trying to file a request for a web service that uses Oauth to generate an access token. However, although the request returns the Http 200 code, json only appears as {"errors":{"Internal":["500"]}}, below the code:

NOTE: I did the same procedure for the terminal using Curl and the data was generated normally.

URL url = new URL( "https://api.myapi.com.br/api/oauth/token" );

HttpURLConnection huc = ( HttpURLConnection ) url.openConnection();

huc.setRequestMethod( "POST" );

huc.setRequestProperty( "grant_type", "client_credentials" );
huc.setRequestProperty( "client_id", CLIENT_ID );
huc.setRequestProperty( "client_secret", CLIENT_SECRET );        

if ( huc.getResponseCode() == HttpURLConnection.HTTP_OK )
{
    BufferedReader br = new BufferedReader( new InputStreamReader( huc.getInputStream(), "UTF-8" ) );

    while ( ( line = br.readLine() ) != null )
    {
        content.append( line ); 
    }

    JSONObject json = new JSONObject( content.toString() );

    System.out.println( json );
}
  • Its Corsfilter class Filter Implements; Applies Alloworigins to see if it goes.

  • The Auth Service is expecting a Json, or a form-enconde huc.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");

  • Auth usually expects a callback url, and its content type is Content-Type: application/json or it expects an object. Ele espera essas propriedade "grant_type": "authorization_code",
 "client_id": "YOUR_CLIENT_ID",
 "client_secret": "YOUR_CLIENT_SECRET",
 "code": "AUTHORIZATION_CODE",
 "redirect_uri": https://YOUR_APP/callback

  • Hello @alxwca. I tried to make the suggested adjustments and still the problem persisted. I set up the URL that generates Httpurlconnection manually. And after executing the code the json returned correctly. sbUrl.append( "https://api.myapi.com.br/api/oauth/token?" ) . append( "grant_type=" ).append( "client_credentials" ) . append( "&" ).append( "client_id=" ).append( CLIENT_ID ) . append( "&" ).append( "client_secret=" ).append( CLIENT_SECRET ); But I’d really like it to work in the standard way, using class methods.

  • Your code makes a request to the authorization server in the application/x-www-form-urlencoded format.

  • Exactly, I did as you had suggested, I put .setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );

  • huc.setRequestProperty( "Accept", "/" );

  • add the above comment to the code, debug. and put your request here.

  • If I understand correctly, follow below the requested.

  • requests Keys=(String[])(length=8) [0] = (String) "Content-Type" [1] = (String) "Accept" [2] = (String) "grant_type" [3] = (String) "client_id" [4] = (String) "client_secret" [5] = () null [6] = () null [7] = () null values = (String[]) #1055(length=8) [0] = (String) "application/x-www-form-urlencoded" [1] = (String) "/" [2] = (String) "client_credentials" [3] = (String) "74f092bb7c941c92847b290375db959b19ca07ae12661fc3f25d5cc48ec71c4b" [4] = (String) "0b4324d418045340601dee2c47b75fbcce57757181fbafa98f2067fca29668b2" [5] = () null [6] = () null [7] = () null

  • huc.setDoOutput(true); huc.setDoInput(true); . But I think these two will solve.

Show 6 more comments

1 answer

0

With is application/x-www-form-urlencoded you will have to submit this way:

 String params = "gran_type="+client_credentials+"&client_id="+CLIENT_ID+"&client_secret="+client_secret;
        byte[] post = params.getBytes( StandardCharsets.ISO_8859_1);
        URL url = new URL(URLEncoder.encode("https://api.myapi.com.br/api/oauth/token?", "ISO-8859-1"));
        HttpsURLConnection http = (HttpURLConnection) url.openConnection();
        http.setDoOutput(true);
        http.setDoInput(true);
        http.setFollowRedirects(false);
        http.setRequestMethod("POST");
        http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
        http.setRequestProperty("charset", "ISO-8859-1");
        http.setUseCaches(false);

        try(DataOutputStream output = new DataOutputStream(http.getOutputStream())){
            output.write(post);
        }
  • Sometimes you need output.close() to send it.

  • And your Oauth server can be waiting for a "GET", you change from "POST" to "GET".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.