gpt4 book ai didi

api - Java 中的 Paypal REST API - getAccessToken 返回 null

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:30 28 4
gpt4 key购买 nike

我正在尝试设置 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/

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