【问题标题】:PayPal REST API java sdk - custom config filePayPal REST API java sdk - 自定义配置文件
【发布时间】:2013-08-23 22:58:40
【问题描述】:

大家下午好!

我使用 PayPal REST API java sdk,我想为我的应用程序的不同环境提供不同的配置。以下是我尝试这样做的方式:

private static boolean IS_PRODUCTION = false;

private static String PAYPAL_ACCESS_TOKEN;

private static void initPayPal() {
    InputStream is = null;
    try {
        is = ApplicationConfig.class.getResourceAsStream(
                IS_PRODUCTION? "/my_paypal_sdk_config.properties" : "/my_paypal_sdk_config_test.properties");
        PayPalResource.initConfig(is);
        String clientID = ConfigManager.getInstance().getConfigurationMap().get("clientID");
        String clientSecret = ConfigManager.getInstance().getConfigurationMap().get("clientSecret");
        PAYPAL_ACCESS_TOKEN = new OAuthTokenCredential(clientID, clientSecret).getAccessToken();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

在尝试获取我拥有的 clientID 时

java.io.IOException: Resource 'sdk_config.properties' could not be found

奇怪的行为 - 我以为我刚刚将 sdk 配置为使用我自己的属性文件。

请告知我如何正确设置这些设置!

【问题讨论】:

    标签: java paypal


    【解决方案1】:

    所以这是我找到的解决方案:

    1. 在默认位置创建一个空的 sdk_config.properties 文件
    2. 加载您自己的属性:

      private static void initPayPal() {
          InputStream is = null;
          try {
              is = ApplicationConfig.class.getResourceAsStream(
          IS_PRODUCTION ? "/my_paypal_sdk_config.properties" : "/my_paypal_sdk_config_test.properties");
              Properties props = new Properties();
              props.load(is);
              PayPalResource.initConfig(props);
              ConfigManager.getInstance().load(props);
              String clientID = ConfigManager.getInstance().getConfigurationMap().get("clientID");
              String clientSecret = ConfigManager.getInstance().getConfigurationMap().get("clientSecret");
              PAYPAL_ACCESS_TOKEN = new OAuthTokenCredential(clientID, clientSecret).getAccessToken();
          } catch (Exception e) {
              throw new RuntimeException(e);
          } finally {
              IOUtils.closeQuietly(is);
          }
      }
      

    【讨论】:

    • ConfigManager.load 已弃用。但我也认为没有其他方法可以解决这个问题。
    【解决方案2】:

    我们在集成步骤上对 PayPal Java SDK 做了一些很好的改进。我们不再需要 sdk_config.properties 文件,因为它们不能很好地工作,特别是对于多配置设置。

    现在,您只需使用clientId、clientSecret 和mode 创建一个APIContext 实例。从那里为任何 API 操作传递该上下文对象。

    下面是代码在不同配置下的样子:

    APIContext defaultContext = new APIContext(clientId1, clientSecret1, "sandbox");
    APIContext sandboxContext = new APIContext(clientId2, clientSecret2, "sandbox");
    APIContext someOtherContext = new APIContext(clientId3, clientSecret3, "live");
    APIContext liveContext = new APIContext(clientId, clientSecret, "live");
    
    // Now pass any of the above context in these calls, and it would use those configurations.
    Payment payment = new Payment();
    // Fill in all the details.
    payment.create(defaultContext);
    // Replace that defaultContext with any of those other contexts.
    

    这是解释该内容的 wiki 页面:https://github.com/paypal/PayPal-Java-SDK/wiki/Making-First-Call

    【讨论】:

      【解决方案3】:

      我在使用 SDK 0.11 版本时遇到了同样的错误。我使用自己的属性文件,但代码仍在寻找“sdk_config.properties”。我将它放入我的 CLASSPATH 的根目录中,但仍然出现相同的错误。然后我提出了明显而可怕的解决方案:将空的“sdk_config.properties”放入“rest-api-sdk-0.11.0.jar”库中。这个街头魔术解决了我的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-04
        • 2016-09-05
        • 2015-12-20
        • 2013-06-01
        • 2016-02-10
        • 2015-02-25
        • 2014-12-12
        • 2020-09-01
        相关资源
        最近更新 更多