When use PayPal Rest API , i get AUTHENTICATION_FAILURE from response.

woniu
New Community Member

 This is my code : 

String scope = "https://api.paypal.com/v1/payments/.* https://uri.paypal.com/services/payments/refund https://uri.paypal.com/services/applications/webhooks https://uri.paypal.com/services/payments/payment/authcapture https://uri.paypal.com/payments/payouts https://api.paypal.com/v1/vault/credit-card/.* https://uri.paypal.com/services/reporting/search/read https://uri.paypal.com/services/disputes/read-seller https://uri.paypal.com/services/disputes/read-buyer https://api.paypal.com/v1/vault/credit-card openid https://uri.paypal.com/services/disputes/update-seller https://uri.paypal.com/services/payments/realtimepayment";

        Tokeninfo tokeninfo = new Tokeninfo();
        String pClientID = "xxx";
        String pClientSecret = "xxx";

        CreateFromRefreshTokenParameters param = new CreateFromRefreshTokenParameters();
        param.setClientID(pClientID);
        param.setClientSecret(pClientSecret);
        tokeninfo.setRefreshToken("xxxx");
        tokeninfo.setTokenType("Bearer");
        tokeninfo.setScope(scope);
        APIContext apiContext = new APIContext();
        apiContext.setMode("live");
        try {
            Tokeninfo token = tokeninfo.createFromRefreshToken(apiContext,param);
            LOG.info(token.getAccessToken());
        } catch (PayPalRESTException e) {
            e.printStackTrace();
        }

Then , i get accessToken and use it call RufundAPI , code : 

 

       Amount amount = new Amount();
        amount.setTotal(totalAmount);
        amount.setCurrency(currencyCode.toString());

        RefundRequest refundRequest = new RefundRequest();
        refundRequest.setAmount(amount);

        DetailedRefund detailedRefund = null;

        boolean isHaveException = false;
        String exceptionMsg = null;
        try{

            APIContext apiContext = new APIContext(accessToken);
            apiContext.setMode("live");
            Sale sale = new Sale();
            sale.setId(transactionId);
            detailedRefund = sale.refund(apiContext,refundRequest);
        } catch (PayPalRESTException e){
            isHaveException = true;
            exceptionMsg = e.getMessage();
            LOG.error(exceptionMsg);
        }

        PayPalRefundResponse refundResponse = new PayPalRefundResponse();
        if (isHaveException){
            refundResponse.setStatus(ERROR);
            refundResponse.setMessage(exceptionMsg);
        }else{

            if (detailedRefund == null){
                refundResponse.setStatus(ERROR);
                refundResponse.setMessage("detailedRefund == null");
                return refundResponse;
            }

            if ("failed".equalsIgnoreCase(detailedRefund.getState())){
                refundResponse.setStatus(ERROR);
                refundResponse.setMessage("refund failed");
                return refundResponse;
            }

            if ("pending".equalsIgnoreCase(detailedRefund.getState())){
                refundResponse.setStatus(ERROR);
                refundResponse.setMessage("refund pending");
                refundResponse.setRefundTransactionID(detailedRefund.getId());
                return refundResponse;
            }

            if ("completed".equalsIgnoreCase(detailedRefund.getState())){
                refundResponse.setStatus(SUCCESS);
                refundResponse.setRefundTransactionID(detailedRefund.getId());
                refundResponse.setCurrency(detailedRefund.getAmount().getCurrency());
                refundResponse.setAmount(detailedRefund.getAmount().getTotal());
                refundResponse.setRefundFromTransactionFee(detailedRefund.getRefundFromTransactionFee().getValue());
                refundResponse.setRefundFromReceivedAmount(detailedRefund.getRefundFromReceivedAmount().getValue());
                refundResponse.setTotalRefundedAmount(detailedRefund.getTotalRefundedAmount().getValue());
                return refundResponse;
            }
        }

I get error message

Response code: 401 Error response: {"name":"AUTHENTICATION_FAILURE","message":"Authentication failed due to invalid authentication credentials or a missing Authorization header.","links":[{"href":"https://developer.paypal.com/docs/api/overview/#error","rel":"information_link"}]}

Why?

 

Could someone help me?

 

Login to Me Too
1 REPLY 1

woniu
New Community Member

Should i do like this : 

String scope = "https://api.paypal.com/v1/payments/.* https://uri.paypal.com/services/payments/refund https://uri.paypal.com/services/applications/webhooks https://uri.paypal.com/services/payments/payment/authcapture https://uri.paypal.com/payments/payouts https://api.paypal.com/v1/vault/credit-card/.* https://uri.paypal.com/services/reporting/search/read https://uri.paypal.com/services/disputes/read-seller https://uri.paypal.com/services/disputes/read-buyer https://api.paypal.com/v1/vault/credit-card openid https://uri.paypal.com/services/disputes/update-seller https://uri.paypal.com/services/payments/realtimepayment";

APIContext apiContext = new APIContext(pClientID,pClientSecret,"live");

Tokeninfo tokeninfo = Tokeninfo.createFromAuthorizationCode(apiContext,authorizationCode);
tokeninfo.setTokenType("Bearer");
tokeninfo.setScope(scope);
Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.