Twitter, oauth and coldfusion - coldfusion

I am trying to post to twitter. I have the app already authenticated and now want to post an update.
This is what my http post is at:
<cfhttp url="http://api.twitter.com/1/statuses/update.json" method="post">
<cfhttpparam type="header" name="status" value="#urlEncodedFormat('my test post')#" />
<cfhttpparam type="header" name="oauth_consumer_key" value="xxx" />
<cfhttpparam type="header" name="oauth_nonce" value="xxx" />
<cfhttpparam type="header" name="oauth_signature_method" value="#urlEncodedFormat('HMAC-SHA1')#" />
<cfhttpparam type="header" name="oauth_token" value="xxx" />
<cfhttpparam type="header" name="oauth_timestamp" value="#GetTickCount()#" />
<cfhttpparam type="header" name="oauth_version" value="1.0" />
</cfhttp>
Has anyone done this? Am I going down the right route?

have you read this?
http://dev.twitter.com/pages/auth#auth-request
you need to construct the "signature base string" and post as body (warning: untested code, for CF8+)
<cffunction name="makeSignatureBaseString" returntype="string" output="false">
<cfargument name="httpMethod" type="string" required="true">
<cfargument name="baseUri" type="string" required="true">
<cfargument name="values" type="struct" required="true">
<cfset var signatureBaseString = "#httpMethod#&#URLEncodedFormat(baseUri)#&">
<cfset var keys = StructKeyArray(values)>
<cfset var key = "">
<cfset ArraySort(keys, "textNoCase")>
<cfloop array="#keys#" index="key">
<cfset signatureBaseString &= URLEncodedFormat("&#key#=#values[key]#")>
</cfloop>
<cfreturn signatureBaseString>
</cffunction>
-
<!--- using values from http://dev.twitter.com/pages/auth#auth-request --->
<cfset params = {
oauth_consumer_key = "GDdmIQH6jhtmLUypg82gる",
oauth_nonce = "oElnnMTQIZvqvlfXM56aBLAf5noGD0AQR3Fmi7Q6Y",
oauth_signature_method = "HMAC-SHA1",
oauth_token = "819797-Jxq8aYUDRmykzVKrgoLhXSq67TEa5ruc4GJC2rWimw",
oauth_timestamp = "1272325550",
oauth_version = "1.0"
}>
<cfhttp url="http://api.twitter.com/1/statuses/update.json" method="POST">
<cfloop collection="#params#" item="key">
<cfheader type="header" name="#key#" value="#params[key]#">
</cfloop>
<!--- add status to the params for makeSignatureBaseString() --->
<cfset params.status = "setting up my twitter 私のさえずりを設定する">
<cfhttpparam type="body"
value="#makeSignatureBaseString('POST', 'http://api.twitter.com/1/statuses/update.json', params)#">
</cfhttp>

Related

cfschedule: Fire onError function if url cannot be found

We have ColdFusion tasks defined like this:
<cfschedule
action="update"
task="Test"
operation="HTTPRequest"
url="#path.getFileFolder()#tasks/test.cfm"
startDate="#now()#"
startTime="00:00"
interval="daily"
resolveURL="no"
publish="yes"
file="test.txt"
path="#path.getLogFolder#"
eventHandler="tasks.eventHandler"
onException="invokeHandler">
The onError function in the eventHandler looks like this:
<cffunction name="onError" returntype="void">
<cfargument name="context" type="struct" required="false" />
<cfscript>
var slackHookURL = 'urlToOurSlackErrorChannel';
var slackMessage = 'ourSlackMessage';
</cfscript>
<cftry>
<cfhttp url="#slackHookURL#" method="post" result="httpResp" timeout="60">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#slackMessage#" />
</cfhttp>
<cfcatch></cfcatch>
</cftry>
</cffunction>
We had the problem that after a server switch our config file missed a / in the file folder path. So the url referenced in all of our tasks pointed to something like https://ourPagetasks/test.cfm instead of https://ourPage/tasks/test.cfm. The onError function hasn't been fired. We just "accidentally" stumbled upon all our tasks not having been executed ever since.
In the test.txt log file however we found the message "Connection timeout". Shouldn't the onError function warn us if that happens? Or is there any workaround so I can check the text that's about to be written to the log file? The onTaskEnd function of the eventHandler is only allowed to have the parameter context which tells me nothing about what's going to be logged.
I hope I explained my problem somehow understandable. Thanks in advance!
I managed to implement a workaround. In our scheduledTasks.cfm I added the following lines at the end to check if any of the urls are invalid:
<!--- Check if the tasks are defined correctly --->
<cfschedule action="list" mode="server" result="tasks" />
<cfloop query="tasks">
<cfhttp method="head" url="#tasks.URL#" />
<cfif len(cfhttp.errorDetail)>
<cfscript>
slackHookURL = 'urlToOurSlackErrorChannel';
slackMessage = 'ourSlackMessage';
</cfscript>
<cftry>
<cfhttp url="#slackHookURL#" method="post" result="httpResp" timeout="60">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#slackMessage#" />
</cfhttp>
<cfcatch></cfcatch>
</cftry>
</cfif>
</cfloop>

Coldfusion How to save PDF Response Stream to File

We have a REST API that returns a stream of content-type application/pdf. I just want to save it to a file on the server.
<cfhttp url="#ApiPath#" method="post" result="res">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#payload#" />
</cfhttp>
<cffile action = "write" file = "#FileName#" output = "#res.fileContent#">
I've producing a blank PDF, any ideas? ( ive tried various combinations of cfdocument/cfpdf with no luck)
here's a dump of the REST response:
I think I've got it:
Solution 1:
<cfhttp url="#url#" method="post" result="res">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#payload#" />
</cfhttp>
<cfset bytes = res.FileContent.toByteArray()>
<cfscript>
fos = createObject("java", "java.io.FileOutputStream").init("myfile.pdf");
fos.write(bytes);
fos.flush();
fos.close();
</cfscript>
EDIT: Based on SOS's solution this also worked:
Solution 2:
<cfhttp url="#url#" method="post" result="res" getAsBinary="Auto">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#payload#" />
</cfhttp>
<cfset fileName = listlast(res["responseHeader"]["content-disposition"],";=")>
<cffile action="write" file="#path#\#fileName#" output="#res.FileContent#">

Properly tagged but facing error: The start tag must have a matching end tag

Sending http call to stripe API for getting Auth-token, but following code throws error: The start tag must have a matching end tag. An explicit end tag can be provided by adding </cffunction>. If the body of the tag is empty, you can use the shortcut <cffunction .../>.....
<cffunction name="getAuthToken" access="private" output="false">
<cfhttp url="#variables.server#/v1/tokens" method="post" password="#variables.password#">
<cfargument name="req" required="true" type="any">
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
<cfhttpparam type="header" name="Accept-Language" value="en_US">
<cfhttpparam type="body" value="#req#">
</cfhttp>
<cfset response = deserializeJSON(cfhttp.FileContent)>
<cfreturn response>
</cffunction>
It looks to me like you have that <cfargument ... tag in the wrong place. The compiler is getting confused by that. Move it up under the <cffunction ... tag instead.
Like this:
<cffunction name="getAuthToken" access="private" output="false">
<cfargument name="req" required="true" type="any">
<cfhttp url="#variables.server#/v1/tokens" method="post" password="#variables.password#">
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
<cfhttpparam type="header" name="Accept-Language" value="en_US">
<cfhttpparam type="body" value="#req#">
</cfhttp>
<cfset response = deserializeJSON(cfhttp.FileContent)>
<cfreturn response>
</cffunction>
Unless it's intended to be another <cfhttpparam ... in which case change it to that.

Looping over CFHTTP cookie and issues setting up cookie

I am trying to loop over a cookie coming from cfhttp but it is not displaying correct results.
Below is my code
<cfhttp url="#address#" method="get" throwOnError="Yes" resolveurl="false">
<cfset cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfloop collection="#cookies#" item="k">
<cfset temp = REReplace(k, ";.*", "")>
<cfset cookieName = listfirst(temp,'=')>
<cfset cookievalue = listlast(temp,'=')>
<cfhttpparam type="cookie" name="#cookieName#" value="#cookievalue#">
</cfloop>
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
Second attempt:
From one call, I am getting cookies and I am putting them in a structure as follows:
<cfset cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfset cookieStruct=StructNew()>
<cfloop collection="#cookies#" item="key">
<cfset cookieKeyAndValue = REReplace(key, ";.*", "")>
<cfset cookieKey = listfirst(cookieKeyAndValue,'=')>
<cfset cookieValue = listlast(cookieKeyAndValue,'=')>
<cfset StructInsert(cookieStruct,cookieKey,cookieValue)>
</cfloop>
<cfdump var="#cookieStruct#" abort>
<cfhttp url="#addr#" method="get" throwOnError="Yes" resolveurl="false" result="objAddress">
<cfloop collection="#cookieStruct#" item="key">
<cfhttpparam type="cookie" name="#key#" value="#cookieStruct[key]#">
</cfloop>
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
This is giving me an error:
Invalid collection ASPJGASGHSG=KBHFPN; path=/. Must be a valid structure or COM object. Loop error.
CFHTTP GET returns a result into the CFHTTP data structure. Having the loop inside the CFHTTP open/close tag results in trying to loop over something that does not exist yet.
<cfhttp url="#address#" method="get" throwOnError="Yes" resolveurl="false" >
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
<cfset cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfloop collection="#cookies#" item="k">
<cfset temp = REReplace(k, ";.*", "")>
<cfset cookieName = listfirst(temp,'=')>
<cfset cookievalue = listlast(temp,'=')>
</cfloop>
I'm not sure what you are attempting to do here but if there is more than one cookie you're going to need different code in the loop.

Upload Files To Google Drive Using ColdFusion

*NEW UPDATED FOR BETTER SECOND PART - NOW GETS TO "308 Resume Incomplete", even though file should be just one upload!
I am using the foundation of cfgoogle from Ray Camden. But Google has deprecated the code for document uploads. The new standard is Resumable Media Uploads.
I have this part working (up to and including the "Initiating a resumable upload request") in the above referenced Google document.
Calling Page:
<cfset application.cfc.Google = createObject('component','#path_cf_cfc#Google') />
<cfset application.cfc.GoogleDocs = createObject('component','#path_cf_cfc#GoogleDocs') />
<cfset gtoken = application.cfc.GoogleDocs.authenticate(emailaddress,password)>
<CFSET testdoc = "a\filepath\documentname.doc">
<CFSET FileType = "application/msword">
<CFSET FileTitle = "test_001">
<cfset temp = application.cfc.GoogleDocs.upload_auth("#Application.Map.DocStorage##tv.testdoc#",FileType,FileTitle)>
<CFSET uploadpath = Listgetat(Listgetat(temp.header,ListContains(temp.header,"https://docs.google.com","#chr(10)#"),"#chr(10)#"),2," ") >
<cfset temp2 = application.cfc.GoogleDocs.upload_file("#Application.Map.DocStorage##tv.testdoc#",FileType,FileTitle,uploadpath)>
The code works up to and including the cfset temp line (getting the unique upload URI)
Here is the code for upload_auth:
<cffunction name="upload_auth" access="public" returnType="any" hint="I get a uniqu URI from Google API." output="false">
<cfargument name="myFile" type="string" required="true" hint="filepath to upload.">
<cfargument name="myType" type="string" required="true" hint="application/msword">
<cfargument name="myTitle" type="string" required="true" hint="name of doc">
<cfset GoogleUrl = "https://docs.google.com/feeds/upload/create-session/default/private/full">
<cfset GoogleVersion = 3>
<cfset FileSize = createObject("java","java.io.File").init(myFile).length()>
<cfhttp url="#GoogleUrl#" method="post" result="diditwork" resolveurl="no">
<cfhttpparam type="header" name="Authorization" value="GoogleLogin auth=#getAuth(variables.docservice)#">
<cfhttpparam type="header" name="GData-Version" value="#GoogleVersion#">
<cfhttpparam type="header" name="Content-Length" value="0">
<cfhttpparam type="header" name="X-Upload-Content-Type" value="#myType#">
<cfhttpparam type="header" name="X-Upload-Content-Length" value="#FileSize#">
<cfhttpparam type="header" name="Slug" value="#myTitle#">
</cfhttp>
<cfreturn diditwork>
</cffunction>
OK - So Far So Good.
But here is where it breaks down:
Running upload_file returns "308 Resume Incomplete" (A lest it's not a 400!) from Google. Arrgh!!
Here is the upload_file -
<cffunction name="upload_file" access="public" returnType="any" hint="I upload the document." output="false">
<cfargument name="myFile" type="string" required="true" hint="filepath to upload.">
<cfargument name="myType" type="string" required="true" hint="like application/msword">
<cfargument name="myTitle" type="string" required="true" hint="name of doc">
<cfargument name="myAuthPath" type="string" required="true" hint="call auth">
<cfset FileSize = GetFileInfo(myFile).size >
<CFSET tv.tostartwithzero = FileSize - 1>
<CFFILE action="read" file="#myfile#" variable="FileText">
<cfhttp url="#myAuthPath#" method="put" result="diditwork" resolveurl="no" multipart="yes" charset="utf-8" >
<cfhttpparam type="header" name="Authorization" value="GoogleLogin auth=#getAuth(variables.docservice)#">
<cfhttpparam type="header" name="GData-Version" value="#variables.GoogleVersion#">
<cfhttpparam type="header" name="Content-Length" value="#FileSize#">
<cfhttpparam type="header" name="Content-Range" value="bytes 0-#tv.tostartwithzero#/#FileSize#">
<cfhttpparam type="header" name="Content-Type" value="#myType#">
<cfhttpparam type="body" value="#trim(FileText)#">
</cfhttp>
<cfreturn diditwork>
</cffunction>
So, there we have it - where I am stuck.
I can get the unique URI, but (maybe because it is late at night) I'm brain dead on what I am doing wrong otherwise to complete the file upload.
All help is appreciated.
I strongly recommend taking a different approach here. There are two big issues with the path you're taking:
It appears the code is using the deprecated client login auth mechanism and is using the username/password instead of OAuth.
Using the deprecated documents list API instead of the newer Drive API.
Since you can call java, I'd suggest writing the upload code in plain Java then invoking it from your CF pages as needed.