Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在spring JPA应用程序中集成paypal rest api。错误代码400_Java_Spring_Jpa_Paypal Sandbox_Paypal Rest Sdk - Fatal编程技术网

Java 如何在spring JPA应用程序中集成paypal rest api。错误代码400

Java 如何在spring JPA应用程序中集成paypal rest api。错误代码400,java,spring,jpa,paypal-sandbox,paypal-rest-sdk,Java,Spring,Jpa,Paypal Sandbox,Paypal Rest Sdk,根据提供的JavaSDK,我创建了一个新项目,并尝试集成代码以进行PayPalAPI调用。但当我运行应用程序时,它会给我以下错误:- Error code : 401 with response : Server returned HTTP response code: 401 for URL: https://api.sandbox.paypal.com/v1/payments/payment 这是我的控制器类 package com.main.controller; public cla

根据提供的JavaSDK,我创建了一个新项目,并尝试集成代码以进行PayPalAPI调用。但当我运行应用程序时,它会给我以下错误:-

Error code : 401 with response : Server returned HTTP response code: 401 for URL: https://api.sandbox.paypal.com/v1/payments/payment
这是我的控制器类

package com.main.controller;

public class PaymentController{ 

@RequestMapping(method = RequestMethod.POST, produces = "application/JSON")

public Payment getString(){

    InputStream is = PaymentController.class
            .getResourceAsStream("/sdk_config.properties");
    try {
        PayPalResource.initConfig(is);
        System.out.println("initiialization done");
    } catch (PayPalRESTException e) {
        System.out.println("Paypal Rest Exception : " + e.getMessage());
    }

    Map<String, String > map = new HashMap<String, String>();
    map.put("mode", "sandbox");
    String clientID = "AYDNebhrsuqiUKPU_ab-tCvGGVkzaxw2y4bIJFIl4rMuCW..........................";
    String clientSecret="ENgjkFRgy1yGhal0aobwdF8kLNglkDaDeDItLN-lgQJZV4W1FpNQ27g3FC...............";

    try {
        accessToken = new OAuthTokenCredential(clientID, clientSecret,map).getAccessToken();
    } catch (PayPalRESTException e) {
        // TODO Auto-generated catch block
        System.out.println("Cannot make the OAuthentication :" + e.getMessage());
    }

    Payment payment = createPayment();

    return payment;
}

public Payment createPayment(){

    Address billingAddress = new Address();
    billingAddress.setCity("Johnstown");
    billingAddress.setCountryCode("US");
    billingAddress.setLine1("52 N Main ST");
    billingAddress.setPostalCode("43210");
    billingAddress.setState("OH");

    CreditCard creditCard = new CreditCard();        
    creditCard.setBillingAddress(billingAddress);
    creditCard.setCvv2(111);
    creditCard.setExpireMonth(11);
    creditCard.setExpireYear(2018);
    creditCard.setFirstName("Joe");
    creditCard.setLastName("Shopper");
    creditCard.setNumber("5500005555555559");
    creditCard.setType("mastercard");

    Details details = new Details();
    details.setShipping("1");
    details.setSubtotal("5");
    details.setTax("1");

    Amount amount = new Amount();
    amount.setCurrency("USD");
    amount.setTotal("7");
    amount.setDetails(details);

    Transaction transaction = new Transaction();
    transaction.setAmount(amount);
    transaction
            .setDescription("This is the payment transaction description.");


    List<Transaction> transactions = new ArrayList<Transaction>();
    transactions.add(transaction);

    FundingInstrument fundingInstrument = new FundingInstrument();
    fundingInstrument.setCreditCard(creditCard);  


    List<FundingInstrument> fundingInstrumentList = new ArrayList<FundingInstrument>();
    fundingInstrumentList.add(fundingInstrument);

    Payer payer = new Payer();
    payer.setFundingInstruments(fundingInstrumentList);
    payer.setPaymentMethod("credit_card");

    Payment payment = new Payment();
    payment.setIntent("sale");
    payment.setPayer(payer);
    payment.setTransactions(transactions);
    Payment createdPayment = null;
    try {
        String accessToken = GenerateAccessToken.getAccessToken();

        String realAccessToken = "A101.kPIsO7eGXhg420XIjnZmPboCS27CeDF6TZjVfGR31f6ja1IotK3e6U-E_k9MwOO5.....";


        /*
         * String requestId = Long.toString(System.nanoTime(); APIContext
         * apiContext = new APIContext(accessToken, requestId ));
         */

        createdPayment = payment.create(apiContext);

        System.out.println("Created payment with id = " + createdPayment.getId()
        + " and status = " + createdPayment.getState());
    } catch (PayPalRESTException e) {
        System.out.println("Cannot make the payment from here: " + e.getMessage());
    }
    return createdPayment;
}
package com.main.controller;
公共类PaymentController{
@RequestMapping(method=RequestMethod.POST,products=“application/JSON”)
公共支付getString(){
InputStream is=PaymentController.class
.getResourceAsStream(“/sdk_config.properties”);
试一试{
PayPalResource.initConfig(is);
System.out.println(“初始化完成”);
}捕获(Paypalreste例外){
System.out.println(“贝宝Rest异常:+e.getMessage());
}
Map Map=newhashmap();
地图放置(“模式”、“沙箱”);
字符串clientID=“AYDNebhrsuqiUKPU_ab-tCvGGVkzaxw2y4bIJFIl4rMuCW”;
字符串clientSecret=“ENgjkFRgy1yGhal0aobwdF8kLNglkDaDeDItLN-lgQJZV4W1FpNQ27g3FC…………”;
试一试{
accessToken=新的OAuthTokenCredential(clientID、clientSecret、map)。getAccessToken();
}捕获(Paypalreste例外){
//TODO自动生成的捕捉块
System.out.println(“无法进行OAuthentication:+e.getMessage());
}
Payment Payment=createPayment();
退还款项;
}
公共支付{
地址账单地址=新地址();
setCity的billingAddress(“约翰斯顿”);
billingAddress.setCountryCode(“美国”);
billingAddress.setLine1(“北街52号”);
billingAddress.setPostalCode(“43210”);
billingAddress.setState(“OH”);
信用卡=新信用卡();
信用卡.收银地址(账单地址);
信用卡setCvv2(111);
信用卡。设定到期日(11);
信用卡。setExpireYear(2018年);
信用卡。setFirstName(“乔”);
信用卡。setLastName(“购物者”);
信用卡设置号码(“5500005559”);
信用卡。setType(“万事达卡”);
详细信息=新的详细信息();
详细信息。设置装运(“1”);
详细信息。设置分项(“5”);
详细信息。设定税收(“1”);
金额=新金额();
金额。设定货币(“美元”);
总额(“7”);
金额。设置详细信息(详细信息);
事务=新事务();
交易。设置金额(金额);
交易
.setDescription(“这是付款交易描述”);
列表事务=新建ArrayList();
交易。添加(交易);
FundingInstrument FundingInstrument=新的FundingInstrument();
资金工具。设置信用卡(信用卡);
List fundingInstrumentList=新建ArrayList();
基金仪器列表。添加(基金仪器);
付款人付款人=新付款人();
付款人设置资金工具(资金工具清单);
付款人设置付款方式(“信用卡”);
付款=新付款();
支付意图(“出售”);
付款。设置付款人(付款人);
支付。设置交易(交易);
Payment createdPayment=null;
试一试{
String accessToken=GenerateAccessToken.getAccessToken();
String realAccessToken=“A101.KPISO7EGXHG420XIJNZMPBOCS27CEDF6TZJVFGR31F6JA1OTK3E6U-E_k9MwOO5…”;
/*
*字符串requestId=Long.toString(System.nanoTime();APIContext
*apiContext=新的apiContext(accessToken,requestId));
*/
createdPayment=payment.create(apiContext);
System.out.println(“使用id=“+createdPayment.getId()创建付款”)
+“和状态=“+createdPayment.getState());
}捕获(Paypalreste例外){
System.out.println(“无法在此付款:+e.getMessage());
}
返回createdPayment;
}
}


更新:-我已经添加了
客户端id
机密
身份验证凭据,现在我得到的是
400
错误,这是
验证错误
401未经授权。因此,首先您必须授权并创建授权令牌。这是如何创建第一个呼叫的示例

使用应用程序的OAuth密钥作为基本身份验证值(这些密钥是您的客户端id和密码的值)进行/token调用。在请求正文中,将grant_type设置为client_凭据。当您运行该命令时,PayPal将生成并返回一个新的访问令牌

将您的令牌添加到您要创建的付款中

请看此示例中的这一行:

所以使用沙箱和类似的东西:

Map<String, String> configurationMap = new HashMap<String, String>();
            configurationMap.put("mode", "sandbox");
            APIContext apiContext = new APIContext();
            apiContext.setConfigurationMap(configurationMap);
            tokeninfo = Tokeninfo.createFromAuthorizationCodeForFpp(apiContext, params);
            tokeninfo.setAccessToken(tokeninfo.getTokenType() + " " + tokeninfo.getAccessToken());
Map configurationMap=newhashmap();
configurationMap.put(“模式”、“沙盒”);
APIContext APIContext=新的APIContext();
setConfigurationMap(configurationMap);
tokeninfo=tokeninfo.createFromAuthorizationCodeForFpp(apiContext,参数);
tokeninfo.setAccessToken(tokeninfo.getTokenType()+“”+tokeninfo.getAccessToken());

之后,您应该能够调用tokeninfo.getAccessToken()来创建支付。

401是未经授权的。因此,首先您必须授权并创建授权令牌。这是如何创建第一个呼叫的示例

使用应用程序的OAuth密钥作为基本身份验证值(这些密钥是您的客户端id和密码的值)进行/token调用。在请求正文中,将grant_type设置为client_凭据。当您运行该命令时,PayPal将生成并返回一个新的访问令牌

将您的令牌添加到您要创建的付款中

请看此示例中的这一行:

所以使用沙箱和类似的东西:

Map<String, String> configurationMap = new HashMap<String, String>();
            configurationMap.put("mode", "sandbox");
            APIContext apiContext = new APIContext();
            apiContext.setConfigurationMap(configurationMap);
            tokeninfo = Tokeninfo.createFromAuthorizationCodeForFpp(apiContext, params);
            tokeninfo.setAccessToken(tokeninfo.getTokenType() + " " + tokeninfo.getAccessToken());
Map configurationMap=newhashmap();
configurationMap.put(“模式”、“沙盒”);
APIContext APIContext=新的APIContext();
setConfigurationMap(configurationMap);
tokeninfo=tokeninfo.createFromAuthorizationCodeForFpp(apiContext,参数);
tokeninfo.setAccessToken(tokeninfo.getTokenType()+“”+tokeninfo.getAccessToken());