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

标签 api paypal

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

相关文章:

ruby-on-rails - 使用 Rails 和 HTTParty 将 JSON 发布到 API

git - 是否有 TFS Git API 用于获取文件夹中文件的上次修改日期?

php - Paypal Payments Pro Sandbox 错误 很抱歉,我们现在无法完成您的付款。请稍后再试

asp.net - Paypal支付安全

PayPal - 付款人可以与订户不同吗?

api - 在 AzureDevOps API 的 Invoke-RestMethod 上传递凭据以检索用户

api - CakePHP 3 REST API 身份验证,同时仍使用现有 Controller

api - 如何在 Google API Linechart 中格式化数字?

api - PayPal + RESTful API + WebHooks + 自签名证书

Paypal : Billing Plan + Agreement - basic Qs