1

I am an oAuth newbie and struggling to implement a simple oAuth consumer in ColdFusion against a PHP site which uses the Pantheon oAuth library. The following curl script works perfectly and returns the JSON I need.

curl -X POST -i -H "Content-type: application/json" -c cookies.txt -X POST https://example.org/service/user/login?mykeyhere -d '{"username":"myuser","password":"mypassword"}'

My question is how do i implement this in ColdFusion, returning the responding JSON into a variable that I can parse?

Thanks for your help!

Ben


UPDATE 5/3/13

Ok, I tried to translate the cURL line and am getting closer - here's what I did:

<cfset mydata = serializejson('{"username":"myuser","password":"mypass"}')>

<cfhttp url="https://example.org/service/user/login" method="post" >
    <cfhttpparam type="header" name="Content-type" value="application/json" > 
    <cfhttpparam type="header" name="oauth_consumer_key"  value="mykey" > 
    <cfhttpparam type="body" value='#mydata#' >
    <cfhttpparam name="cookies.txt" type="cookie" value="" > 
</cfhttp>

However, I am still getting a negative response - but it seems it is because I am not passing the values that are in the "-d" clause from the cURL script correctly. Any ideas?

Thanks again!


Latest update: Tried both of these to no avail - i also removed the cookie line:

<cfset mydata = '{"username":"myuser","password":"mypass"}'>
<cfset mydata = serializejson('{"username=myuser","password=mypass"}')>

3:20 ET

Sorry - made the correction as per your comment to below - but still no luck:

<cfset myData = serializeJSON({username="user",password="pass"})>
wp78de
  • 18,207
  • 7
  • 43
  • 71
Ben
  • 33
  • 5
  • 3
    It looks like a simple POST request, so the equivalent is [cfhttp](http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7ffc.html) with a few [headers/parameters](http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7d85.html) thrown in ie `Content-Type`, `body`. Start by taking a look at the [cURL manual](http://curl.haxx.se/docs/manpage.html). Translate the options ie `-X, -i, -H, -d` and see how far you get. Post back with your code if you run into problems. – Leigh May 02 '13 at 22:41
  • Thanks Leigh - any thoughts on the approach I tool? – Ben May 03 '13 at 15:58
  • The value string looks off. The input string you are using is already in json format. So wrapping it in `serializeJSON` will mangle it. I suspect you meant to use CF's implicit structure notation instead. Note the use of `=` instead of `:`. ie `` – Leigh May 03 '13 at 18:44
  • Also, I do not think there is a straight translation of the "cookies" stuff in cfhttp. So I would omit that parameter for now. – Leigh May 03 '13 at 18:51
  • Ok - i removed the cookie line and tried both of these to no avail - any other ideas? Im really stuck - and thanks..... : – Ben May 03 '13 at 19:04
  • Your JSON code is still wrong. Take another look at the `cfset` in my comment above. It should look like this: `` – Leigh May 03 '13 at 19:12
  • Ok - i see it - still no luck - I put the change as per above and a negative. But again, the cURL works fine? – Ben May 03 '13 at 19:21
  • I am not familiar with `cURL`, but it seems to have debugging options. I would dump the request (headers and content) and compare it to what [cfhttp is sending](http://stackoverflow.com/questions/3515452/view-cfhttp-request). Look for differences. That is about all I can suggest for now. Is there a public URL people can test? And/or an online API? – Leigh May 03 '13 at 19:33

1 Answers1

2

Great idea on the debugging of the cURL post - I figured it out. It was just about the headers - the data WAS being passed correctly.

Once the JSON is created, here's the CFHTTP call:

<cfhttp url="https://mydomain.org/service/user/login?oauth_consumer_key=myKeyHere" method="post" >
    <cfhttpparam type="header" name="Content-type" value="application/json" > 
    <cfhttpparam type="header" name="Accept" value="*/*" > 
    <cfhttpparam type="body" value="#mydata#" >
</cfhttp>

Works great - thanks to Leigh for helping me think this through!

Ben
  • 33
  • 5