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.