I am trying to write a simple app that is conecting with Allegro.pl, but I am stuck on authorization. I have spent way too many hours already trying to figure this out (postman was not helpful), and I wounder if you can have a look and help, please?
As per documentation, all data should be encoded with Base64 - I tried to encode as String as well, as byte[], but authorization fails all the time with an error:
{"error":"Unauthorized","error_description":"Unauthorized"}
in documentation there is a curl code (please disregard different web address, as I am working in a sandbox)
curl -X POST
‘https://allegro.pl/auth/oauth/device'
-H ‘Authorization: Basic {base64(client_id:client_secret)}’
-H ‘Content-Type: application/x-www-form-urlencoded’
-d ‘client_id={client_id}’
Please see below for my code
package AllegroAPI;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import org.apache.commons.codec.binary.Base64;
public class App
{
public static void main( String[] args )
{
String clientId = "myClientId";
String clientSecret = "myClientsSecret";
String authString = clientId + ":" + clientSecret;
// String codedAuthString = Base64.encodeBase64String(authString.getBytes());
byte[] codedAuthString = Base64.encodeBase64(authString.getBytes());
byte[] codedClientId = Base64.encodeBase64(clientId.getBytes());
HttpResponse response = Unirest.post("https://allegro.pl.allegrosandbox.pl/auth/oauth/device")
.header("Authorization", "Basic {base64("+codedAuthString+")}")
.header("Content-Type", "application/x-www-form-urlencoded")
.body("client_id={" + clientId +"}")
.asString();
System.out.println(response.getBody());
}
question from:
https://stackoverflow.com/questions/65646998/java-rest-api-authorization-issue 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…