Talking to an API
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Talking to an API
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?
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?
-
- Livecode Opensource Backer
- Posts: 328
- Joined: Mon Dec 05, 2011 5:34 pm
- Location: Thailand
- Contact:
Re: Talking to an API
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.
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
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
Enjoy,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.
Visit http://electronic-apps.info for released App information.
Re: Talking to an API
Thanks so much Dave.
That was just the start I was looking for!!!!
Really appreciate it
That was just the start I was looking for!!!!
Really appreciate it
Re: Talking to an API
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9802
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Talking to an API
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Talking to an API
The API needs HTTPS, although I did try HTTP and it just redirected me to a 404
Re: Talking to an API
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..
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
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..
API Doco here
http://www.getharvest.com/api/time-tracking#get-entries
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
http://www.getharvest.com/api/time-tracking#get-entries
Re: Talking to an API
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!!!
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?
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!!!
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=""e&"integer""e&">2620538</project_id>"&cr&"<task_id type=""e&"integer""e&">1173391</task_id?>"&cr&"<spent_at type=""e&"date""e&">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
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.
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
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: Talking to an API
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.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Talking to an API
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?
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?
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: Talking to an API
Sure. From the documentation
so you just need to add the authorization string to that.
Note: it's not clear to me from the documentation whether or not the username:password needs to be in parentheses.
And you've already got the first two:Accept: application/xml
Content-Type: application/xml
Authorization: Basic (insert your authentication string here)
Code: Select all
set the httpHeaders to "Content-Type:application/xml"&cr&"Accept:application/xml"
Code: Select all
set the httpHeaders to "Content-Type:application/xml"&cr&"Accept:application/xml"&cr&"Authorization:Basic"&&base64encode(username&":"&userPass)
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Talking to an API
Thanks again.
Noting your previous post how do you suggest I structure the POST?
I was doing it like this...
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")