Basic HTTP authorization

I’m trying to log on to a web service using a simple HTTP authorization using network.request() and i’m not having any luck. And I can’t seem to find a solution. The web service is a web based POS: http://docs.vendhq.com/intro.html and they use cURL as an example (Which I don’t know so it’s not helpful). But it does seem rather straight forward.

I’m editing the sample code to just see if I can get this part working:

local myText = display.newText("(Waiting for response)", 0, 0, native.systemFont, 16) myText.x = display.contentCenterX myText.y = 120 local function networkListener( event ) if ( event.isError ) then myText.text = "Network error!" else myText.text = "See Corona Terminal for response" print ( "RESPONSE: " .. event.response ) end end local headers = {} headers["Authorization"] = "username:password" headers["Accept"] = "application/json" local params = {} params.headers = headers -- Access Google over SSL: network.request( "https://companyName.vendhq.com/api/products", "GET", networkListener, params )

But it just gives me in the console: error: “API Auth required”

So i’m obviously not posting my log in credentials properly/ I’m I doing this wrong?

I think you should try this

local authHeader = \_createBasicAuthHeader( username, password ) local headers = {} -- set the auth header headers["Authorization"] = authHeader

-- authentication header creation local function \_createBasicAuthHeader( username, password ) -- the header format is "Basic \<base64 encoded username:password\>" local header = "Basic " local authDetails = \_b64enc( username .. ":" .. password ) header = header .. authDetails return header end

-- 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

That worked! Thanks you very much!

I think you should try this

local authHeader = \_createBasicAuthHeader( username, password ) local headers = {} -- set the auth header headers["Authorization"] = authHeader

-- authentication header creation local function \_createBasicAuthHeader( username, password ) -- the header format is "Basic \<base64 encoded username:password\>" local header = "Basic " local authDetails = \_b64enc( username .. ":" .. password ) header = header .. authDetails return header end

-- 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

That worked! Thanks you very much!

Wow this was super timely, thanks very much for the help and question both of you!!! :slight_smile:

-Mario

Wow this was super timely, thanks very much for the help and question both of you!!! :slight_smile:

-Mario

Thanks for this great answer Gooner87, it really helped me!

FYI - there is a b64 encoder in the mime package:

local mime = require( "mime" ) . . . auxdata.headers["Authorization"] = "Basic "..mime.b64( "username:password" ) network.request( remote\_host, httpverb, oncompleteFunc, auxdata )

Thanks for this great answer Gooner87, it really helped me!

FYI - there is a b64 encoder in the mime package:

local mime = require( "mime" ) . . . auxdata.headers["Authorization"] = "Basic "..mime.b64( "username:password" ) network.request( remote\_host, httpverb, oncompleteFunc, auxdata )