0

我正在尝试编写一个与 Allegro.pl 连接的简单应用程序,但我被困在授权上。我已经花了太多时间试图解决这个问题(邮递员没有帮助),如果你能看一下并提供帮助,我会很伤心,好吗?

根据文档,所有数据都应使用 Base64 进行编码-我也尝试将其编码为 String 字节 [],但授权始终失败并出现错误:

{"error":"Unauthorized","error_description":"Unauthorized"}

在文档中有一个 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());

    }
4

1 回答 1

2

我会在这里尽力而为嘿嘿。看起来你的基本身份验证是错误的,试试这个:

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

让我知道这是否对您有帮助

于 2021-01-09T20:10:14.353 回答