PayPal REST Api

I am thinking about making a plugin for PayPal using their REST API.

Following their documentation:

https://developer.paypal.com/docs/integration/direct/make-your-first-call/

I made this function to get the access token:

function getAccessToken() local function networkListener( event ) if ( event.isError ) then print( "getAccessToken: Network error!" ) print ( "RESPONSE: " .. event.response ) else print ( "RESPONSE: " .. event.response ) end end local headers = {} headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Accept-Language"] = "en-US" headers["Accept"] = "application/json" headers["Authorization"] = "Basic QVZzQblablabla" local body = "grant\_type=client\_credentials" local params = {} params.headers = headers params.body = body network.request( "https://api.sandbox.paypal.com/v1/oauth2/token", "POST", networkListener, params ) end

I get the following in the response:

“Certificate required”

I tried the call with POSTMAN and it worked.

What am I doing wrong in this Lua code?

Bump on this, i would like to know too as im looking at linking to paypal and am getting the same issue. Postman works fine for me too.

Ive not had any issues linking to other REST api’s using https

This worked for me:

local json = require( "json" ) local mime = require( "mime" ) local accessToken = nil local payPalClientId = "insert your paypal app client id here" local payPalClientSecret = "insert your paypal app client secret here" local function payPalLoginListener( event ) if ( event.isError ) then print( "Network error!" ) else print ( "RESPONSE: " .. event.response ) print( json.prettify( event.response ) ) accessToken = event.response.access\_token end end local headers = {} headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Accept-Language"] = "en-US" headers["Accept"] = "application/json" headers["Authorization"] = "Basic " .. mime.b64( payPalClientId .. ":" .. payPalClientSecret ) local body = "grant\_type=client\_credentials" local params = {} params.headers = headers params.body = body network.request( "https://api.sandbox.paypal.com/v1/oauth2/token", "POST", payPalLoginListener, params )

Hi Rob, your code does not work for me, i get the same “certificate required” error. I added more logging to the error response event, this is what i get.

print( “Network error!” )

print( "Status: " … tostring(event.status))

print( "Response Header: " … tostring(event.responseHeaders))

print( "Response: " … tostring(event.response))

Network error!

Status: -1

Response Header: nil

Response: Certificate required

Like the other poster, i have no problems making the calls via PostMan and i have also been linking with another REST service using https the last few days without any problems. 

FYI my corona build (incase it matters in this case) is 2015.2731

I’m using 2830, but I don’t think we’ve made any changes to networking since 2731. Here is what I get:

Feb 27 08:38:19.111 Copyright (C) 2009-2016  C o r o n a   L a b s   I n c . Feb 27 08:38:19.111     Version: 3.0.0 Feb 27 08:38:19.112     Build: 2016.2830 Feb 27 08:38:19.159 Platform: iPad / x86\_64 / 10.10.5 / Intel HD Graphics 4000 OpenGL Engine / 2.1 INTEL-10.6.33 / 2016.2830 / en | US | en\_US | en Feb 27 08:38:19.453 Loading project from:   ~/tmp Feb 27 08:38:19.453 Project sandbox folder: ~/Library/Application Support/Corona Simulator/tmp-DA5DF2AE67CF50DBF6069B9ED09E7028 Feb 27 08:38:22.148 RESPONSE: {"scope":"https://uri.paypal.com/services/subscriptions https://uri.paypal.com/services/paypalhere https://api.paypal.com/v1/payments/.\* https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/payments/sale/.\*/refund https://uri.paypal.com/services/applications/webhooks openid https://uri.paypal.com/payments/payouts https://api.paypal.com/v1/vault/credit-card/.\* https://api.paypal.com/v1/payments/refund","nonce":"2016-02-28T01:38:21ZtQd9jKO37DMShZsPQT3ClQ\_rB6UQ021eyytKhkbHEdI","access\_token":"A101.L-nXpN-AFBxxQQO5RRF6fQshzuOOVGQPmFTbL79qJIFDn9q5Ip-U1lsSGTkNKO5i.Pphl1Ggwjbk50nbJxDxmRSfRMe4","token\_type":"Bearer","app\_id":"APP-80W284485P519543T","expires\_in":32400} Feb 27 08:38:22.180 {   "access\_token":"A101.L-nXpN-AFBxxQQO5RRF6fQshzuOOVGQPmFTbL79qJIFDn9q5Ip-U1lsSGTkNKO5i.Pphl1Ggwjbk50nbJxDxmRSfRMe4",   "app\_id":"APP-80W284485P519543T",   "expires\_in":32400,   "nonce":"2016-02-28T01:38:21ZtQd9jKO37DMShZsPQT3ClQ\_rB6UQ021eyytKhkbHEdI",   "scope":"https://uri.paypal.com/services/subscriptions https://uri.paypal.com/services/paypalhere https://api.paypal.com/v1/payments/.\* https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/payments/sale/.\*/refund https://uri.paypal.com/services/applications/webhooks openid https://uri.paypal.com/payments/payouts https://api.paypal.com/v1/vault/credit-card/.\* https://api.paypal.com/v1/payments/refund",   "token\_type":"Bearer" }

No certificate errors. It sounds to me like something might not be setup in your PayPal developer portal correctly. Even though there isn’t much to configure there. Are you on Windows or a Mac?

Rob

I’m on Windows 10. I’ve tried with both the default credentials and app specific credentials but same result with both. The interactive example thing on the paypal dev website and postman both work fine with the default credentials, i didnt try them with the app specific one. I only setup an app specific one as a test when your code didnt work for me either. 

Windows doesn’t support CURL (I don’t believe), so I don’t know if you can try that example or not. Let me try it from Windows 10 and see if I see your issue.

Rob

I confirmed Windows has a different behavior than OS X does. One of our engineers did a little research and it appears that PayPal is trying to use TLS1.2 on their sandbox servers and perhaps somewhere in the windows implementation, there is a limit of  TLS 1.1. The research suggested that in production, this likely won’t be a problem.

It’s the weekend and I’ll try to bring this back up with the engineers during the week.

Rob

Ok thanks, Rob.

Bump on this, i would like to know too as im looking at linking to paypal and am getting the same issue. Postman works fine for me too.

Ive not had any issues linking to other REST api’s using https

This worked for me:

local json = require( "json" ) local mime = require( "mime" ) local accessToken = nil local payPalClientId = "insert your paypal app client id here" local payPalClientSecret = "insert your paypal app client secret here" local function payPalLoginListener( event ) if ( event.isError ) then print( "Network error!" ) else print ( "RESPONSE: " .. event.response ) print( json.prettify( event.response ) ) accessToken = event.response.access\_token end end local headers = {} headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Accept-Language"] = "en-US" headers["Accept"] = "application/json" headers["Authorization"] = "Basic " .. mime.b64( payPalClientId .. ":" .. payPalClientSecret ) local body = "grant\_type=client\_credentials" local params = {} params.headers = headers params.body = body network.request( "https://api.sandbox.paypal.com/v1/oauth2/token", "POST", payPalLoginListener, params )

Hi Rob, your code does not work for me, i get the same “certificate required” error. I added more logging to the error response event, this is what i get.

print( “Network error!” )

print( "Status: " … tostring(event.status))

print( "Response Header: " … tostring(event.responseHeaders))

print( "Response: " … tostring(event.response))

Network error!

Status: -1

Response Header: nil

Response: Certificate required

Like the other poster, i have no problems making the calls via PostMan and i have also been linking with another REST service using https the last few days without any problems. 

FYI my corona build (incase it matters in this case) is 2015.2731

I’m using 2830, but I don’t think we’ve made any changes to networking since 2731. Here is what I get:

Feb 27 08:38:19.111 Copyright (C) 2009-2016  C o r o n a   L a b s   I n c . Feb 27 08:38:19.111     Version: 3.0.0 Feb 27 08:38:19.112     Build: 2016.2830 Feb 27 08:38:19.159 Platform: iPad / x86\_64 / 10.10.5 / Intel HD Graphics 4000 OpenGL Engine / 2.1 INTEL-10.6.33 / 2016.2830 / en | US | en\_US | en Feb 27 08:38:19.453 Loading project from:   ~/tmp Feb 27 08:38:19.453 Project sandbox folder: ~/Library/Application Support/Corona Simulator/tmp-DA5DF2AE67CF50DBF6069B9ED09E7028 Feb 27 08:38:22.148 RESPONSE: {"scope":"https://uri.paypal.com/services/subscriptions https://uri.paypal.com/services/paypalhere https://api.paypal.com/v1/payments/.\* https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/payments/sale/.\*/refund https://uri.paypal.com/services/applications/webhooks openid https://uri.paypal.com/payments/payouts https://api.paypal.com/v1/vault/credit-card/.\* https://api.paypal.com/v1/payments/refund","nonce":"2016-02-28T01:38:21ZtQd9jKO37DMShZsPQT3ClQ\_rB6UQ021eyytKhkbHEdI","access\_token":"A101.L-nXpN-AFBxxQQO5RRF6fQshzuOOVGQPmFTbL79qJIFDn9q5Ip-U1lsSGTkNKO5i.Pphl1Ggwjbk50nbJxDxmRSfRMe4","token\_type":"Bearer","app\_id":"APP-80W284485P519543T","expires\_in":32400} Feb 27 08:38:22.180 {   "access\_token":"A101.L-nXpN-AFBxxQQO5RRF6fQshzuOOVGQPmFTbL79qJIFDn9q5Ip-U1lsSGTkNKO5i.Pphl1Ggwjbk50nbJxDxmRSfRMe4",   "app\_id":"APP-80W284485P519543T",   "expires\_in":32400,   "nonce":"2016-02-28T01:38:21ZtQd9jKO37DMShZsPQT3ClQ\_rB6UQ021eyytKhkbHEdI",   "scope":"https://uri.paypal.com/services/subscriptions https://uri.paypal.com/services/paypalhere https://api.paypal.com/v1/payments/.\* https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/payments/sale/.\*/refund https://uri.paypal.com/services/applications/webhooks openid https://uri.paypal.com/payments/payouts https://api.paypal.com/v1/vault/credit-card/.\* https://api.paypal.com/v1/payments/refund",   "token\_type":"Bearer" }

No certificate errors. It sounds to me like something might not be setup in your PayPal developer portal correctly. Even though there isn’t much to configure there. Are you on Windows or a Mac?

Rob

I’m on Windows 10. I’ve tried with both the default credentials and app specific credentials but same result with both. The interactive example thing on the paypal dev website and postman both work fine with the default credentials, i didnt try them with the app specific one. I only setup an app specific one as a test when your code didnt work for me either. 

Windows doesn’t support CURL (I don’t believe), so I don’t know if you can try that example or not. Let me try it from Windows 10 and see if I see your issue.

Rob

I confirmed Windows has a different behavior than OS X does. One of our engineers did a little research and it appears that PayPal is trying to use TLS1.2 on their sandbox servers and perhaps somewhere in the windows implementation, there is a limit of  TLS 1.1. The research suggested that in production, this likely won’t be a problem.

It’s the weekend and I’ll try to bring this back up with the engineers during the week.

Rob

Ok thanks, Rob.