2016-06-14 25 views
1

根據java SDK提供的here,我創建了一個新項目,並嘗試將代碼集成到PayPal API調用中。但是,當我運行應用程序,它給我的錯誤是: -如何在Spring JPA應用程序中集成paypal rest api。錯誤代碼400

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(""); 
    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; 
} 

}

更新: - 我已經加入client_idsecret認證證書和現在我得到的是400錯誤是Validation Error

回答

0

401是未經授權的。所以,首先你必須授權並創建授權令牌。 Here是如何創建你的第一個電話。

使用應用程序的OAuth密鑰爲基本身份驗證值(密鑰是您的client_id和secret的值)進行/ token調用。在請求正文中,將grant_type設置爲client_credentials。當您運行該命令時,PayPal會生成並返回一個新的訪問令牌。

將您的代幣添加到您要創建的付款中。

看這句話在這個示例:https://github.com/paypal/PayPal-Java-SDK/blob/master/rest-api-sample/src/main/java/com/paypal/api/sample/FuturePaymentSample.java#L62

因此,使用沙盒和類似的東西:

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()); 

之後,你應該能夠調用tokeninfo.getAccessToken(),用於創建付款。

+0

我從https://developer.paypal.com/docs/rest-errors/發現了該錯誤。但後來我提供了一個有效的標記,如下所示: - 'String realAccessToken =「A101.kPIsO7eGXhg420XIjnZmPboCS27CeDF6TZjVfGR31f6ja1IotK3e6U-E_k9MwOO5 .....」;' – viper

+1

如果你正在關注他們的例子https://github.com/paypal/ PayPal-Java-SDK#爲make-api調用,它仍然不起作用?我的意思是OAuthTokenCredential從它獲取令牌。 – Hrabosch

+0

我剛剛在帖子中使用了上面提到的代碼。沒有什麼更多。是的'sdk_config.properties'當然 – viper

相關問題