我正在尝试设置 Paypal API。但是 getAccessToken() 方法返回 null。在查看 API 时,它提到使用 Payment.initConfig 或创建 OAuthTokenCredentials。 accessToken 的两种方式都返回为 null。
public static void createPayment() {
String clientID = "xxxxxx"
String clientSecret = "xxxxx";
String paymentID = null;
OAuthTokenCredential credentials;
String accessToken;
Properties prop = new Properties();
try {
prop.setProperty("service.Endpoint", "http://api.sandbox.paypal.com");
prop.setProperty("clientID", clientID);
prop.setProperty("clientSecret", clientSecret);
prop.setProperty("http.GoogleAppEngine", "false");
prop.setProperty("http.UseProxy", "false");
prop.setProperty("http.ConnectionTimeOut", "5000");
prop.setProperty("http.Retry", "1");
prop.setProperty("http.ReadTimeOut", "30000");
prop.setProperty("http.MaxConnection", "100");
//tried this way.
credentials = Payment.initConfig(prop);
//also tried this way.
//accessToken = new OAuthTokenCredential(clientID, clientSecret).getAccessToken();
accessToken = credentials.getAccessToken();
Payment.get(accessToken, paymentID);
APIContext apiContext = new APIContext(accessToken);
Payment payment = new Payment();
payment.setIntent("sale");
payment.create(apiContext);
} catch (PayPalRESTException ex) {
Logger.getLogger(PayPal.class.getName()).log(Level.SEVERE, null, ex);
}
}
最佳答案
在调用“getAccessToken”之前,您必须配置端点:
// Sandbox (for testing) : https://api.sandbox.paypal.com
// Live (production) : https://api.paypal.com
Properties properties = new Properties();
properties.setProperty("service.EndPoint", "https://api.paypal.com");
ConfigManager.getInstance().load(properties);
OAuthTokenCredential tokenCredential = new OAuthTokenCredential(clientId, clientSecret);
String accessToken = tokenCredential.getAccessToken();
更多信息请访问:PayPal-Java-SDK / GitHub
关于api - Java 中的 Paypal REST API - getAccessToken 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34236402/