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.
– alxwca
The Auth Service is expecting a Json, or a form-enconde huc.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
– alxwca
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
– alxwca
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.
– Luís Felipe Dal Molin
Your code makes a request to the authorization server in the application/x-www-form-urlencoded format.
– alxwca
Exactly, I did as you had suggested, I put .setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
– Luís Felipe Dal Molin
huc.setRequestProperty( "Accept", "/" );
– alxwca
add the above comment to the code, debug. and put your request here.
– alxwca
If I understand correctly, follow below the requested.
– Luís Felipe Dal Molin
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
– Luís Felipe Dal Molin
huc.setDoOutput(true); huc.setDoInput(true); . But I think these two will solve.
– alxwca