我提出此请求
https://www.amazon.com/ap/oa?client_id=amzn1.application-oa2-
clientxxxxxxxxxxxxxxxxxxxxxxxxx&scope=clouddrive%3Aread%20clouddrive%3
Awrite&response_type=code&redirect_uri=https://roscasend.go.ro:8443/
我已成功重定向到此页面
https://roscasend.go.ro:8443/?code=cccccccoooooodeeeeeee&scope=
clouddrive%3Aread+clouddrive%3Awrite
但是在更新以下请求中的字段时
POST https://api.amazon.com/auth/o2/token HTTP/1.1
Host: api.amazon.com
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
//those are the post's request arguments
grant_type=authorization_code&code=cccccccoooooodeeeeeee
&client_id=amzn1.application-oa2-clientxxxxxxxxxxxxxxxxxxxxxxxxx&
client_secret=secret______________________&redirect_uri=
http%3A%2F%2Froscasend.go.ro%3A8443
凭我的凭据我遇到400错误。
您是否会就如何更新上述请求给我解决方案?
请注意,第一个请求是在浏览器中,第二个请求是通过java。
答案 0 :(得分:1)
BasicCookieStore cookieStore = new BasicCookieStore();
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.build();
HttpUriRequest login = null;
try {
login = RequestBuilder.post()
.setUri(new URI("https://api.amazon.com/auth/o2/token"))
.addParameter("grant_type", "authorization_code")
.addParameter("code", "cccccccoooooodeeeeeee")
.addParameter("client_id", "amzn1.application-oa2-clientxxxxxxxxxxxxxxxxxxxxxxxxx")
.addParameter("client_secret", "secret______________________")
.addParameter("redirect_uri", "https://roscasend.go.ro:8443/")
.build();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//login.addHeader("Content-Type", "application/x-www-form-urlencoded");
//login.addHeader("Host", "api.amazon.com");
login.addHeader("Cache-Control", "no-cache");
CloseableHttpResponse response2 = null;
try {
response2 = httpclient.execute(login);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpEntity entity = response2.getEntity();
System.out.println("Login form get: " + response2.getStatusLine());
InputStream in = entity.getContent();
InputStreamReader is = new InputStreamReader(in);
StringBuilder sb=new StringBuilder();
BufferedReader br = new BufferedReader(is);
String read = br.readLine();
while(read != null) {
//System.out.println(read);
sb.append(read);
read =br.readLine();
}
EntityUtils.consume(entity);
System.out.println(sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
response2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
httpclient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}