PayPal REST API - Sandbox returning 401 for API requests but successful access token -
i using paypal rest api in java make request testing (sandbox) servers access token, succeeds, , sending access token same sandbox servers payment fails 401 not authorized.
i followed paypal rest api instructions found here: https://developer.paypal.com/webapps/developer/docs/api/#authentication--headers , here https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/
i tried using oauthtokencredential
object provide clientid
, clientsecret
returns me access token, not appid
provided paypal nor expiresin
time in minutes. in addition, complains missing sdk_conf.properties
file in classpath.
i created own class make call using apache's httpclient full response , works:
custom paypalapi interface resteasy
@path("v1/oauth2/token") public interface paypaloauthapi { @post @produces(mediatype.application_json) @consumes(mediatype.application_form_urlencoded) public oauthinfo requestaccesstoken( @headerparam("authorization") string basicencodedtoken, string requestbody ); }
custom oauth token request
private oauthinfo getoauthinfo() { oauthinfo info = null; paypaloauthapi client = httpclientfactory.createresteasyclient(80, 443, 120, 2, api_endpoint, paypaloauthapi.class, true); info = client.requestaccesstoken("basic "+ generatebase64string(pp_api_user, pp_api_secret), "grant_type=client_credentials"); if (info == null) { throw new runtimeexception(apiresultcodes.remote_unauthorized, "could not authenticate paypal."); } return info; }
this works correctly , returns me 200 ok status expected json response including access token.
i take token , provide payment
object uses paypal's code communicate servers (under hood believe it's basic java.lang.net.httpconnection code) , returns 401 unauthorized.
payment call using paypal's rest api
// new token string accesstoken = getoauthinfo().getaccesstoken(); // configprops properties object pre-populated sdk_conf.properties // values except service.endpoint, clientid, , clientsecret configprops.setproperty("service.endpoint", api_endpoint); // ... transaction objects omitted try { payment payment = new payment(); payment.initconfig(configprops); payment.setintent(intent.sale.tostring()); payment.setpayer(payer); payment.settransactions(transactions); payment createdpayment = payment.create(accesstoken); } catch (paypalrestexception ex) { log.error("failed submit donation", ex); }
log output
(including httpclient request/response ids redacted)
2013-08-19 16:34:11,077 [qtp1005310362-44] debug:org.apache.http.impl.conn.defaultclientconnection sending request: post /v1/oauth2/token http/1.1 2013-08-19 16:34:11,077 [qtp1005310362-44] debug:org.apache.http.wire >> "post /v1/oauth2/token http/1.1[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] debug:org.apache.http.wire >> "content-type: application/x-www-form-urlencoded[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] debug:org.apache.http.wire >> "accept: application/json[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] debug:org.apache.http.wire >> "authorization: basic qwm1umnoq3g3dhb1bwzszw9rcur3mw41bhrxaktqu0xrodiywujwwxpedxpzugjuc0j0edzywglqx1prokvjvnq5eermm2jnqmw1og5kylz2vmtsr3jcavzvn1biwgtsv01mqjvqb3nxrtnkbwxccf9tv05bdu91ea==[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] debug:org.apache.http.wire >> "accept-encoding: gzip, deflate[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] debug:org.apache.http.wire >> "content-length: 29[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] debug:org.apache.http.wire >> "host: api.sandbox.paypal.com[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] debug:org.apache.http.wire >> "connection: keep-alive[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] debug:org.apache.http.wire >> "user-agent: apache-httpclient/4.2 (java 1.5)[\r][\n]" 2013-08-19 16:34:11,078 [qtp1005310362-44] debug:org.apache.http.wire >> "[\r][\n]" 2013-08-19 16:34:11,078 [qtp1005310362-44] debug:org.apache.http.wire >> "grant_type=client_credentials" 2013-08-19 16:34:12,690 [qtp1005310362-44] debug:org.apache.http.wire << "http/1.1 200 ok[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] debug:org.apache.http.wire << "server: apache-coyote/1.1[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] debug:org.apache.http.wire << "date: mon, 19 aug 2013 20:34:11 gmt[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] debug:org.apache.http.wire << "paypal-debug-id: d29c41eb8625a[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] debug:org.apache.http.wire << "content-type: application/json[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] debug:org.apache.http.wire << "content-length: 282[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] debug:org.apache.http.wire << "[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] 2013-08-19 16:34:12,691 [qtp1005310362-44] debug:org.apache.http.impl.client.defaulthttpclient connection can kept alive indefinitely 2013-08-19 16:34:12,691 [qtp1005310362-44] debug:org.apache.http.wire << "{"scope":"https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/vault/credit-card/.* openid","access_token":"rvatuloqb0wfx kesdzqw4znyfidupcf2j7smhcfspwo","token_type":"bearer","app_id":"app-80w284285q519543t","expires_in":28800}" 2013-08-19 16:34:12,693 [qtp1005310362-44] debug:org.apache.http.impl.conn.poolingclientconnectionmanager connection [id: 5][route: {s}->https://api.sandbox.paypal.com] can kept alive indefinitely 2013-08-19 16:34:12,693 [qtp1005310362-44] debug:org.apache.http.impl.conn.poolingclientconnectionmanager connection released: [id: 5][route: {s}->https://api.sandbox.paypal.com][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 2] 2013-08-19 16:34:15,197 [qtp1005310362-44] error:com.example.gateways.paypal.payments.paypalpaymentsprorest failed submit donation com.paypal.core.rest.paypalrestexception: error code : 401 response : server returned http response code: 401 url: https://api.sandbox.paypal.com/v1/payments/payment
i noticed scope , url api sent me 2 different things. perhaps i'm missing configuration sent api calls sandbox url?
in code above can see i'm setting service.endpoint
api_endpoint
set https://api.sandbox.paypal.com
in tests. not providing clientid
or clientsecret
in properties object, providing them gives me 401.
what else missing?
came across question , had similar problem when trying make payments third party paypal account.
not sure if you're running same thing was, trying create payments third party paypal account had not requested permission. turns out needed use permissions api request proper permissions.
Comments
Post a Comment