我正在尝试编写一个简单的应用程序,正在与Allegro.pl连接,但我被授权所困。我已经花了很多时间想办法弄清楚(邮递员帮不上忙),如果你能帮忙的话,我想知道吗?
根据文档,所有数据都应该使用Base64进行编码--我也试图将其编码为字符串,如byte[],但授权始终失败,出现错误:
{“错误”:“未经授权”,“error_description”:“未授权”}
在文档中有一个卷曲代码(请忽略不同的web地址,因为我在沙箱中工作)
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());
}发布于 2021-01-09 20:10:14
我会尽我最大努力的,呵呵。看起来您的基本身份验证是错误的,请尝试如下:
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();如果这对你有帮助,请告诉我。
https://stackoverflow.com/questions/65646998
复制相似问题