首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java授权问题

Java授权问题
EN

Stack Overflow用户
提问于 2021-01-09 19:55:41
回答 1查看 132关注 0票数 0

我正在尝试编写一个简单的应用程序,正在与Allegro.pl连接,但我被授权所困。我已经花了很多时间想办法弄清楚(邮递员帮不上忙),如果你能帮忙的话,我想知道吗?

根据文档,所有数据都应该使用Base64进行编码--我也试图将其编码为字符串,如byte[],但授权始终失败,出现错误:

{“错误”:“未经授权”,“error_description”:“未授权”}

在文档中有一个卷曲代码(请忽略不同的web地址,因为我在沙箱中工作)

代码语言:javascript
复制
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}’

请看下面我的代码

代码语言:javascript
复制
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());

    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-09 20:10:14

我会尽我最大努力的,呵呵。看起来您的基本身份验证是错误的,请尝试如下:

代码语言:javascript
复制
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();

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65646998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档