Talking to an API

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Talking to an API

Post by Nakia » Fri Feb 01, 2013 1:09 am

Hi All,

I am still relatively new to LC and Programming full stop but I am now looking at wanting to intergrate an iOS app I currently have with the following API.

http://www.getharvest.com/api/authentication-http-basic

Just wondering if there are any lessons I could do to get me started in this if it is at all possible?

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Location: Thailand
Contact:

Re: Talking to an API

Post by dave_probertGA6e24 » Fri Feb 01, 2013 4:46 am

Hi Nakia,

Can't help with all of it, but here's something to get you started. Put this code on a button, modify to suit your username, password and any special url text.

Code: Select all

on mouseUp
   put "xxxx" into userName
   put "yyyy" into userPass
   put "subdomain" into theSubDomain
   
   set the httpHeaders to "Content-Type:application/xml"&cr&"Accept:application/xml"
   get URL ("https://" & userName & ":" & URLEncode(userPass) &"@"&theSubDomain&".harvestapp.com/account/who_am_i")
   put the result into xout
   answer xout
end mouseUp
The variable xout above should contain the resultant xml text from the site. You can then parse this via the various xml commands in LC, or just brute force access the part you are interested in. The XML lesson at http://lessons.runrev.com/s/lessons/m/4 ... n-xml-file is ok to get you started and further information can be found via a search of the forums (or asking specific questions)

Enjoy,

Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Talking to an API

Post by Nakia » Fri Feb 01, 2013 6:51 am

Thanks so much Dave.

That was just the start I was looking for!!!!
Really appreciate it :)

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Talking to an API

Post by Nakia » Fri Feb 01, 2013 7:11 am

Hmmm

wonder why I get this return with valid credentials?

error -Error with certificate at depth: 0 issuer = /C=US/O=Equifax/OU=Equifax Secure Certificate Authority subject = /serialNumber=4-F4BJmWN-U-hWTfQnuIdTEN-QBf4jr4/C=US/O=*.harvestapp.com/OU=GT71452989/OU=See www.rapidssl.com/resources/cps (c)10/OU=Domain Control Validated - RapidSSL(R)/CN=*.harvestapp.com err 20:unable to get local issuer certificate

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Talking to an API

Post by FourthWorld » Fri Feb 01, 2013 3:39 pm

Are you using "http://" or "https://"?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Talking to an API

Post by Nakia » Fri Feb 01, 2013 9:36 pm

The API needs HTTPS, although I did try HTTP and it just redirected me to a 404

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Talking to an API

Post by Nakia » Fri Feb 01, 2013 10:08 pm

Doing a bit of research I found LibURLSetSSLVerification false/true which seems to toggle the use of SSL Certificate security but it seems the API I am talking to needs it to return any valid data.

If I set LibURLSetSSLVerification false and run the handler I get an empty result.

However, when I run the handler in the iPhone simulator (minus the above SSLVerification stuff) I am at least getting a 400 bad request..

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Talking to an API

Post by Nakia » Tue Feb 26, 2013 1:53 am

Okay,

So long story short I got the first part of this working (authenticating etc) but becuase I have never done this I am not sure how to structure any subsequent resuests.
The API Doco says i now just do a "Get /daily" to fetch what I need but where do I place this in a HTTPS Request?

below is what I used to authenticate successfully..

Code: Select all

   put empty into fld "ResultFld"
   put "xxxxx" into userName
   put "xxxxx" into userPass
   put "xxxxxxx" into theSubDomain
   
   set the httpHeaders to "Content-Type:application/xml"&cr&"Accept:application/xml"
   get URL ("https://" & userName & ":" & URLEncode(userPass) &"@"&theSubDomain&".harvestapp.com/account/who_am_i")
   put it into xout
   put xout

API Doco here
http://www.getharvest.com/api/time-tracking#get-entries

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Talking to an API

Post by Nakia » Wed Feb 27, 2013 4:28 am

Afternoon all,

So I have made some progress here and I am now able to successfully query the API, and place the result into a XML Tree and
use that parts I need!!! :D

So, the next step is to be able to post data to the API and I have started on this today.
Following the API doco posted above I have constructed the below handler but unfortunately I keep getting an error "Parse error; Fatal error: expected '>' at :5."

can anyone see the error in my ways?

Code: Select all

 put "<request>"&cr&"<notes>nakia testing API</notes>"&cr&"<hours>1</hours>"&cr&"<project_id type="&quote&"integer"&quote&">2620538</project_id>"&cr&"<task_id type="&quote&"integer"&quote&">1173391</task_id?>"&cr&"<spent_at type="&quote&"date"&quote&">Wed, 27 Feb 2013</spent_at>"&cr&"</request>" into tData
   --
   put "xxxxxx" into userName
   put "xxxxxxx" into userPass
   put "xxxx" into theSubDomain
   set the httpHeaders to "Content-Type:application/xml"&cr&"Accept:application/xml"
   POST tData to URL ("https://" & userName & ":" & URLEncode(userPass) &"@"&theSubDomain&".harvestapp.com/daily/add")
   put it

Bernard
Posts: 351
Joined: Sat Apr 08, 2006 10:14 pm
Location: London, England

Re: Talking to an API

Post by Bernard » Thu Feb 28, 2013 9:55 pm

I am not sure from your description if your "parse error" is coming from LiveCode or from the server.

However, let me help you clarify your code with a technique I find useful. The Merge() function. http://docs.runrev.com/Function/merge

I will show you how to use it on your POST line then you can apply it to your xml construction. To my mind it makes testing a lot easier than all those quote&cr things :) (They have their uses, but in this kind of construction I find them confusing).

POST tData to URL ("https://" & userName & ":" & URLEncode(userPass) &"@"&theSubDomain&".harvestapp.com/daily/add")
becomes
put URLEncode(userPass) into userPass
POST tData to URL ("https://[[userName ]]:[[userPass]]@[[theSubDomain]].harvestapp.com/daily/add")

Now the template structure is clear, and you can check that all the variables have the right values if you have a breakpoint before your POST statement. There should be nothing but the literal string, plus variables and square brackets in your template.

I would usually start with a working literal string e.g. from an example of the API. Then replace the essential variables with [[tUserNameHere]], etc.

I think you will find it easier to spot an error in your XML if you use this approach too. However, there is also a better way to produce XML, from a LiveCode array structure translated into an XML structure automatically. http://runrev.com/newsletter/july/issue ... etter1.php It may not be necessary to bother with that in this case; merge() might just be enough to make it all clearer.

Hope that helps.

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Talking to an API

Post by Nakia » Thu Feb 28, 2013 10:13 pm

Hi Bernd,
Thanks for the reply, I will sure take a look at that function.

From my testing so far I can successfully query the API via GET so I am sure it's the
XML content I am sending when I do a POST which is producing the error (FYI the error is coming from the server)

After doing some reading I know the API is expecting the data in UTF8 but I am led to believe LC defaults XML to UTF16
So maybe I need to Change the encoding before sending.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Talking to an API

Post by mwieder » Tue Mar 05, 2013 12:29 am

My reading of the Basic Authorization docs says you need to add the Authorization attribute to the headers in addition to the Accept and Content-Type attributes. Maybe the whoami function allows you to bypass this authorization, but I'm pretty sure you will need it for posting data.

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Talking to an API

Post by Nakia » Wed Mar 06, 2013 1:18 am

Thanks for your reply.

Being that I am new to this it would be a lie if I said I understood exactly what you meant.
Could you please clarify a little further or maybe provide an example?

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Talking to an API

Post by mwieder » Wed Mar 06, 2013 8:15 pm

Sure. From the documentation
Accept: application/xml
Content-Type: application/xml
Authorization: Basic (insert your authentication string here)
And you've already got the first two:

Code: Select all

set the httpHeaders to "Content-Type:application/xml"&cr&"Accept:application/xml"
so you just need to add the authorization string to that.

Code: Select all

set the httpHeaders to "Content-Type:application/xml"&cr&"Accept:application/xml"&cr&"Authorization:Basic"&&base64encode(username&":"&userPass)
Note: it's not clear to me from the documentation whether or not the username:password needs to be in parentheses.

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Talking to an API

Post by Nakia » Wed Mar 06, 2013 11:57 pm

Thanks again.

Noting your previous post how do you suggest I structure the POST?

I was doing it like this...

Code: Select all

POST tData to URL ("https://" & userName & ":" & URLEncode(userPass) &"@"&theSubDomain&".harvestapp.com/daily/add")

Post Reply

Return to “Talking LiveCode”