gpt4 book ai didi

java - {"error":"unsupported_grant_type","error_description":"grant type not supported"} 销售队伍 Java

转载 作者:行者123 更新时间:2023-12-02 09:35:01 30 4
gpt4 key购买 nike

无法弄清楚我在这里做错了什么。尝试在 selenium 测试(java)中通过 SF 的 API 获取访问 token ,并不断收到相同的错误。在 cypress (javascript) 中进行了完全相同的调用,它工作得很好,但是,由于其他原因我不能走这条路(SF 讨厌 UI 测试)。我认为这可能与它必须是表单数据这一事实有关,而java只是对此感到奇怪?我不知道,请帮忙,我很害怕。

已经尝试将请求正文设置为一个长字符串,而不是将所有内容添加到 JSONObject 中,但这仍然不起作用

        URL obj = new URL("https://login.salesforce.com/services/oauth2/token");
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

JSONObject body = new JSONObject();
body.put("grant_type", "password");
body.put("client_id", "***");
body.put("client_secret", "***");
body.put("username", "tyler+prod@pactsafe.com");
body.put("password", "***");
// String jsonInputString = "{ grant_type: 'password', client_id: '***', client_secret: '***', password: '***', username: 'tyler+prod@pactsafe.com'";

System.out.println(body.toString());

OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(body.toString());
wr.flush();

int httpResult = con.getResponseCode();
InputStream inputStream;
if (200 <= httpResult && httpResult <= 299) {
inputStream = con.getInputStream();
} else {
inputStream = con.getErrorStream();
}

BufferedReader in = new BufferedReader(
new InputStreamReader(inputStream));

StringBuilder response = new StringBuilder();
String currentLine;

while ((currentLine = in.readLine()) != null)
response.append(currentLine);

in.close();


System.out.println(response.toString());
System.out.println(httpResult);

driver.get("https://na85.lightning.force.com/lightning/setup/SetupOneHome/home");

我明白了:

{"password":"***","grant_type":"password","client_secret":"***","client_id":"***","username":"tyler+prod@pactsafe.com"}
{"error":"unsupported_grant_type","error_description":"grant type not supported"}
400

最佳答案

没有正确编码我的表单值。通过执行类似的操作来修复:

StringBuilder sb = new StringBuilder("grant_type=password");
sb.append("&client_id=").append(URLEncoder.encode("client_id__shhhh, "UTF-8"));
sb.append("&client_secret=").append(URLEncoder.encode("secret shhhhh", "UTF-8"));

关于java - {"error":"unsupported_grant_type","error_description":"grant type not supported"} 销售队伍 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57594014/

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