Curl and Network.request

Have anyone worked with Rest API Call like Paypal? I have troubles interpreting CURL to Corona Lua.

I think Corona can work with rest api. Bellow is the CURL from Paypal Developer Portal.

curl https://api.sandbox.paypal.com/v1/oauth2/token \ -H "Accept: application/json" \ -u "EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM:EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM" \ -d "grant\_type=client\_credentials" 

Anyone Please? Really Appreciate!

I have found some clues on handling Basic HTTP authentication from this post http://forums.coronalabs.com/topic/41116-basic-http-authorization/?hl=%2Bcurl+%2Band+%2Bnetwork.request

Below is the code i tried as suggested by Gooner87.

-- b64 encoding local function \_b64enc( data ) -- character table string local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' return ( (data:gsub( '.', function( x ) local r,b='', x:byte() for i=8,1,-1 do r=r .. ( b % 2 ^ i - b % 2 ^ ( i - 1 ) \> 0 and '1' or '0' ) end return r; end ) ..'0000' ):gsub( '%d%d%d?%d?%d?%d?', function( x ) if ( #x \< 6 ) then return '' end local c = 0 for i = 1, 6 do c = c + ( x:sub( i, i ) == '1' and 2 ^ ( 6 - i ) or 0 ) end return b:sub( c+1, c+1 ) end) .. ( { '', '==', '=' } )[#data %3 + 1] ) end -- authentication header creation local function \_createBasicAuthHeader( username, password ) -- the header format is "Basic \<base64 encoded username:password\>" local header = "Basic " -- local header = "" local authDetails = \_b64enc( username .. ":" .. password ) header = header .. authDetails return header end local function getAccessToken() local function networkListener( event ) if ( event.isError ) then print( "Network error!") else print ( "RESPONSE: " .. event.response ) end end local authHeader = \_createBasicAuthHeader( "ASUAYxDIogu99hFdIP3NJ6W3W8cLz5gs-vzBdsfd-pCL8yME9Whr4J8JDK4C", "EPNeZhDzxsGWtB2iZ8J89-6hDBEkntI25oAx1Sq1mcrXv8GQdtLwkIgQKR9z" ) local headers = {} headers["Authorization"] = authHeader headers["Content-Type"]="application/json" headers["Accept"] = "application/json" print("authHeader",authHeader) local body = { ["grant\_type"] = "client\_credentials", } local params = {} params.headers = headers params.body = json.encode(body) -- Access Google over SSL: network.request( "https://api.sandbox.paypal.com/v1/oauth2/token", "POST", networkListener, params ) end getAccessToken()

I got Network Error in callback. is there something wrong here? Please help.

Make the authheader the string you have.  It’s already gone through the createBasicAuthHeader process.  What that’s doing is taking the username and password and base64 encoding them. 

I tied it by set 

local authHeader = "Basic ASUAYxDIogu99hFdIP3NJ6W3W8cLz5gs-vzBdsfd-pCL8yME9Whr4J8JDK4C:EPNeZhDzxsGWtB2iZ8J89-6hDBEkntI25oAx1Sq1mcrXv8GQdtLwkIgQKR9z"

,Or even this

local authHeader = "ASUAYxDIogu99hFdIP3NJ6W3W8cLz5gs-vzBdsfd-pCL8yME9Whr4J8JDK4C:EPNeZhDzxsGWtB2iZ8J89-6hDBEkntI25oAx1Sq1mcrXv8GQdtLwkIgQKR9z"

,but i still get Network error in networkListener.

PS: this link describes the above process. https://developer.paypal.com/docs/integration/direct/make-your-first-call/

You could try putting in the URL itself:

https://ASUAYxDIogu99hFdIP3NJ6W3W8cLz5gs-vzBdsfd-pCL8yME9Whr4J8JDK4C:EPNeZhDzxsGWtB2iZ8J89-6hDBEkntI25oAx1Sq1mcrXv8GQdtLwkIgQKR9z@api.sandbox.paypal.com/v1/oauth2/token

That’s how you normally pass a username and password with a URL.

Thanks Rob,

But it still returns network error. I think it’s related to Paypal mechanism on how to handle the Basic Authorization.

I checked around the Paypal Development Site. They explain in PHP Stuff which I’m not so familiar with. If you can take a look at this link https://devblog.paypal.com/authenticating-with-oauth-2/, you might have better understanding than i do.

If Corona support payment method like Paypal, that would be a major feature for business app.

I have found some clues on handling Basic HTTP authentication from this post http://forums.coronalabs.com/topic/41116-basic-http-authorization/?hl=%2Bcurl+%2Band+%2Bnetwork.request

Below is the code i tried as suggested by Gooner87.

-- b64 encoding local function \_b64enc( data ) -- character table string local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' return ( (data:gsub( '.', function( x ) local r,b='', x:byte() for i=8,1,-1 do r=r .. ( b % 2 ^ i - b % 2 ^ ( i - 1 ) \> 0 and '1' or '0' ) end return r; end ) ..'0000' ):gsub( '%d%d%d?%d?%d?%d?', function( x ) if ( #x \< 6 ) then return '' end local c = 0 for i = 1, 6 do c = c + ( x:sub( i, i ) == '1' and 2 ^ ( 6 - i ) or 0 ) end return b:sub( c+1, c+1 ) end) .. ( { '', '==', '=' } )[#data %3 + 1] ) end -- authentication header creation local function \_createBasicAuthHeader( username, password ) -- the header format is "Basic \<base64 encoded username:password\>" local header = "Basic " -- local header = "" local authDetails = \_b64enc( username .. ":" .. password ) header = header .. authDetails return header end local function getAccessToken() local function networkListener( event ) if ( event.isError ) then print( "Network error!") else print ( "RESPONSE: " .. event.response ) end end local authHeader = \_createBasicAuthHeader( "ASUAYxDIogu99hFdIP3NJ6W3W8cLz5gs-vzBdsfd-pCL8yME9Whr4J8JDK4C", "EPNeZhDzxsGWtB2iZ8J89-6hDBEkntI25oAx1Sq1mcrXv8GQdtLwkIgQKR9z" ) local headers = {} headers["Authorization"] = authHeader headers["Content-Type"]="application/json" headers["Accept"] = "application/json" print("authHeader",authHeader) local body = { ["grant\_type"] = "client\_credentials", } local params = {} params.headers = headers params.body = json.encode(body) -- Access Google over SSL: network.request( "https://api.sandbox.paypal.com/v1/oauth2/token", "POST", networkListener, params ) end getAccessToken()

I got Network Error in callback. is there something wrong here? Please help.

Make the authheader the string you have.  It’s already gone through the createBasicAuthHeader process.  What that’s doing is taking the username and password and base64 encoding them. 

I tied it by set 

local authHeader = "Basic ASUAYxDIogu99hFdIP3NJ6W3W8cLz5gs-vzBdsfd-pCL8yME9Whr4J8JDK4C:EPNeZhDzxsGWtB2iZ8J89-6hDBEkntI25oAx1Sq1mcrXv8GQdtLwkIgQKR9z"

,Or even this

local authHeader = "ASUAYxDIogu99hFdIP3NJ6W3W8cLz5gs-vzBdsfd-pCL8yME9Whr4J8JDK4C:EPNeZhDzxsGWtB2iZ8J89-6hDBEkntI25oAx1Sq1mcrXv8GQdtLwkIgQKR9z"

,but i still get Network error in networkListener.

PS: this link describes the above process. https://developer.paypal.com/docs/integration/direct/make-your-first-call/

You could try putting in the URL itself:

https://ASUAYxDIogu99hFdIP3NJ6W3W8cLz5gs-vzBdsfd-pCL8yME9Whr4J8JDK4C:EPNeZhDzxsGWtB2iZ8J89-6hDBEkntI25oAx1Sq1mcrXv8GQdtLwkIgQKR9z@api.sandbox.paypal.com/v1/oauth2/token

That’s how you normally pass a username and password with a URL.

Thanks Rob,

But it still returns network error. I think it’s related to Paypal mechanism on how to handle the Basic Authorization.

I checked around the Paypal Development Site. They explain in PHP Stuff which I’m not so familiar with. If you can take a look at this link https://devblog.paypal.com/authenticating-with-oauth-2/, you might have better understanding than i do.

If Corona support payment method like Paypal, that would be a major feature for business app.

HI,

Did you got the paypal rest api working? If you did please message me here or on my mail id: varun.raja.30@gmail.com . Hoping your response soon. I am in a need for this thing working on my project.

Regards

Varun

HI,

Did you got the paypal rest api working? If you did please message me here or on my mail id: varun.raja.30@gmail.com . Hoping your response soon. I am in a need for this thing working on my project.

Regards

Varun