在尝试使用Java发布https请求以执行paypal计费协议时,我收到了401响应。我在下面的请求中做了什么错误。
String url = "https://api.sandbox.paypal.com/v1/payments/billing-agreements/" +request.getParameter("token") + "/agreement-execute";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Accept-Charset", "UTF-8");
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Execute Token : " + request.getParameter("token"));
System.out.println("Response Code : " + responseCode);
答案 0 :(得分:0)
This guide应该引导您完成必要的步骤
身份验证错误
HTTP状态代码:401身份验证错误通常是由与访问令牌相关的问题引起的:
- 确保包含并正确访问令牌。
- 确保访问令牌没有过期。
答案 1 :(得分:0)
好的,
Paypal提供的API用于执行结算协议,而不是直接发布网址。
提供的API为Agreement.execute(apiContext, token);
用
替换上面的代码 String token = request.getParameter("token");
String accessToken;
try {
accessToken = GenerateAccessToken.getAccessToken();
APIContext apiContext = new APIContext(accessToken);
Agreement.execute(apiContext, token);
System.out.println(Agreement.getLastRequest());
System.out.println(Agreement.getLastResponse());
} catch (PayPalRESTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}