2013-08-21 27 views
1

大家下午好!PayPal REST API java sdk - 自定义配置文件

我使用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属性文件。

请教如何正确设置这些设置!

回答

1

我们在集成步骤上对PayPal Java SDK进行了一些很好的改进。我们删除了对sdk_config.properties文件的需求,因为它们不适用,特别是对于多配置设置。

现在,您只需创建一个APIContext实例,其中包括clientId,clientSecretmode。您从那里传递该上下文对象以进行任何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

4

因此,这里是我找到了解决办法:

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

    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); 
        } 
    } 
    
+0

ConfigManager.load已被弃用。但我也没有看到这个问题。 –

0

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