javarestapi授权问题

kg7wmglp  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(312)

我试图写一个简单的应用程序,是连接allegro.pl,但我卡在授权。我已经花了太多的时间试图弄清楚这一点( Postman 是没有帮助的),我想如果你能看看和帮助,请?
根据文档,所有数据都应使用base64编码-我尝试将其编码为字符串和byte[],但授权始终失败,并出现错误:
{“error”:“未授权”,“error\u description”:“未授权”}
在文档中有一个curl代码(请忽略不同的网址,因为我在沙盒中工作)

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}’

请看下面我的代码

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());

    }
iovurdzv

iovurdzv1#

我会尽力的呵呵。看起来你的基本身份验证错误,请尝试以下操作:

String codedAuthString = clientId + ":" + clientSecret;
String authValueBase64 = new String(Base64.encodeBase64(
                    codedAuthString.getBytes()));
HttpResponse response = Unirest.post("https://allegro.pl.allegrosandbox.pl/auth/oauth/device")
.header("Authorization", "Basic " + authValueBase64 )
.header("Content-Type", "application/x-www-form-urlencoded")
.body("client_id={" + clientId +"}")
.asString();

如果这对你有帮助,请告诉我

相关问题