gpt4 book ai didi

Java如何在请求中正确发送正文

转载 作者:行者123 更新时间:2023-12-02 01:56:58 24 4
gpt4 key购买 nike

我尝试从用 Java 编写的控制台程序连接 API,但每次都会收到 400 bad-request。

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}'
import java.io.*;
import java.net.*;
import java.util.Base64;

public class AccessToken {

void getAccessToken() throws IOException {
String authUrl = "https://allegro.pl.allegrosandbox.pl/auth/oauth/device";
String userCredentials = "bdc22d4054c04090ae687d4e0e75a7b4:uNHKzbWhwWQYneraAU7yVWHIdLSIw7MmCHkliZOyk7QNeYeRANdQuApJqFNkADcy";
String basicAuth = Base64.getEncoder().encodeToString(userCredentials.getBytes());

HttpURLConnection myURL = (HttpURLConnection) new URL(authUrl).openConnection();

myURL.setRequestProperty("Request Method", "POST");
myURL.setRequestProperty("Authorization", "Basic " + basicAuth);
myURL.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
myURL.setDoOutput(true);

OutputStream outStream = myURL.getOutputStream();
OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8");
outStreamWriter.write("client_id={bdc22d4054c04090ae687d4e0e75a7b4}");
outStreamWriter.flush();
outStreamWriter.close();
outStream.close();

int status = myURL.getResponseCode(); // 200 = HTTP_OK
System.out.println("Response (Code):" + status);
System.out.println("Response (Message):" + myURL.getResponseMessage());
}

}

最佳答案

使用JaxB/JaxRS

/**
* OAuth2 / OIDC token retrieval response.
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class OAuthServerResponse
{
@XmlElement(name = "access_token")
private String access_token;

@XmlElement(name = "refresh_token")
private String refresh_token;

@XmlElement(name = "token_type")
private String token_type;

@XmlElement(name = "scope")
private String scope;

@XmlElement(name = "tenant")
private String tenant;

@XmlElement(name = "id_token")
private String id_token;

// empty constructor, getters, setters
}

private com.sun.jersey.api.client.WebResource webResource;

public void init()
{
webResource = Client.create().resource(authorizationServerRoot + "/" + tokenEndpoint);
}

/**
* retrieve an access token against the current authorization server
*
* @param payload the request payload as String
* @return an access token
*/
public OAuthServerResponse getAccessToken(String payload)
{
OAuthServerResponse result = null;
final ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED)
.accept(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, payload);
if (response.getStatus() == HttpStatus.SC_OK)
{
result = response.getEntity(OAuthServerResponse.class);
}
else
{
LOG.error(String.format("%d invalid oauth2 request", response.getStatus()));
}
return result;
}

有效负载如下所示:“grant_type=client_credentials&client_id=foo&client_secret=bar”

关于Java如何在请求中正确发送正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57394646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com