Page 1 of 3

Talking to an API

Posted: Fri Feb 01, 2013 1:09 am
by Nakia
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?

Re: Talking to an API

Posted: Fri Feb 01, 2013 4:46 am
by dave_probertGA6e24
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

Re: Talking to an API

Posted: Fri Feb 01, 2013 6:51 am
by Nakia
Thanks so much Dave.

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

Re: Talking to an API

Posted: Fri Feb 01, 2013 7:11 am
by Nakia
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

Re: Talking to an API

Posted: Fri Feb 01, 2013 3:39 pm
by FourthWorld
Are you using "http://" or "https://"?

Re: Talking to an API

Posted: Fri Feb 01, 2013 9:36 pm
by Nakia
The API needs HTTPS, although I did try HTTP and it just redirected me to a 404

Re: Talking to an API

Posted: Fri Feb 01, 2013 10:08 pm
by Nakia
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..

Re: Talking to an API

Posted: Tue Feb 26, 2013 1:53 am
by Nakia
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

Re: Talking to an API

Posted: Wed Feb 27, 2013 4:28 am
by Nakia
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

Re: Talking to an API

Posted: Thu Feb 28, 2013 9:55 pm
by Bernard
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.

Re: Talking to an API

Posted: Thu Feb 28, 2013 10:13 pm
by Nakia
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.

Re: Talking to an API

Posted: Tue Mar 05, 2013 12:29 am
by mwieder
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.

Re: Talking to an API

Posted: Wed Mar 06, 2013 1:18 am
by Nakia
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?

Re: Talking to an API

Posted: Wed Mar 06, 2013 8:15 pm
by mwieder
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.

Re: Talking to an API

Posted: Wed Mar 06, 2013 11:57 pm
by Nakia
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")