ColdFusion 9 to SSL API connection- I/O Exception: peer not authenticated - api

I'm using ColdFusion on a Windows server.
I try to connect to an API, it works on http url, but not on https.
<cfhttp url="https://www.example.com/api/login" method="post" result="httpResp" timeout="120">
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded" />
<cfhttpparam type="formField" name="user_key" value="#user_key#" />
<cfhttpparam type="formField" name="email" value="#user_email#" />
<cfhttpparam type="formField" name="password" value="#user_password#" />
</cfhttp>
<cfdump var="#httpResp#">
I'm getting this response:
struct
Charset [empty string]
ErrorDetail I/O Exception: peer not authenticated
Filecontent Connection Failure
Header [empty string]
Mimetype Unable to determine MIME type of file.
Responseheader
struct [empty]
Statuscode Connection Failure. Status code unavailable.
Text YES
When opening the URL from the server, I'm getting this xml response:
<rsp stat="fail" version="1.0">
<err code="15">Login failed</err>
</rsp>
And in my code, I have another https post that works without issue:
<cfhttp url="https://www.example2.com" method="post" result="httpResp" timeout="120">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#Replace(myJsonStruct,"//","")#">
</cfhttp>
That means that the issue is not coming from SSL settings on the server.
Is there an error in my CF code?

Related

Issue in generating oauth_signature for twitter in coldfusion

I have started twitter API testing while creating an OAuth signature the response showing is
{"errors":[{"code":215,"message":"Bad Authentication data."}]}
Added all the params and headers mention in the doc, and not getting the correct result, please let me know if i am doing anything wrong.
<cfset status = "Hello Ladies + Gentlemen, a signed OAuth">
<cfset include_entities = true >
<cfset oauth_consumer_key = "x3OgJNjJ5TPGj7jyvLz9rUzfU">
<cfset oauth_nonce = createUUID()>
<cfset oauth_signature_method ="HMAC-SHA1" >
<cfset oauth_timestamp = GetTickCount()/1000>
<cfset oauth_token = "1196333045885702145-BPWGfPyKakezuqYoLaYXiIgQ5TWAkf">
<cfset oauth_version = "1.0">
<cfset parameterString = "include_entities=true&oauth_consumer_key=#oauth_consumer_key#&oauth_nonce=#oauth_nonce#&oauth_signature_method=#oauth_signature_method#&oauth_timestamp=#oauth_timestamp#&oauth_token=#oauth_token#&oauth_version=1.0&status=#urlEncodedFormat(status)#" >
<cfdump var="#parameterString#">
<cfhttp url="https://api.twitter.com/1.1/statuses/update.json" method="POST" >
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
<cfhttpparam type="header" name="mimetype" value="application/x-www-form-urlencoded" />
<cfhttpparam type="header" name="Accept" value="*/*" />
<cfhttpparam type="header" name="Connection" value="close" />
<cfhttpparam type="header" name="Host" value="api.twitter.com" />
<cfhttpparam type="header" name="User-Agent" value="OAuth gem v0.4.4" />
<cfhttpparam type="header" name="Content-Length" value="76">
<cfhttpparam type="header" name="include_entities" value="#include_entities#">
<cfhttpparam type="header" name="oauth_consumer_key" value="#oauth_consumer_key# ">
<cfhttpparam type="header" name="oauth_nonce" value="#oauth_nonce#">
<cfhttpparam type="header" name="oauth_signature_method" value="#oauth_signature_method#">
<cfhttpparam type="header" name="oauth_timestamp" value="#oauth_timestamp#">
<cfhttpparam type="header" name="oauth_token" value="#oauth_token#">
<cfhttpparam type="header" name="oauth_version" value="#oauth_version#">
<cfhttpparam type="header" name="status" value="#urlEncodedFormat(status)#">
<cfhttpparam type="body" value="#parameterString#" >
</cfhttp>
<cfdump var="#cfhttp#"><cfabort>
Refer this and Create your developer account on twitter,by your own key, ouath will work.refer this create secret key for oauth
use this method, it would work correctly
<cfset consumerKey = "xxxxxxxxxxxxxx">
<cfset consumerSecret = "xxxxxxxxxxxxxxxxxxx">
<cfset bearerToken = ToBase64(consumerKey & ":" & consumerSecret)>
<cfset authorization = "Basic " & bearerToken>
<cfhttp url="https://api.twitter.com/oauth2/token" method="post" charset="utf-8" >
<cfhttpparam type="header" name="Authorization" value="#authorization#">
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded;charset=UTF-8">
<cfhttpparam type="body" value="grant_type=client_credentials">
</cfhttp>

Converting LogMeIn Refresh Token Curl to Coldfusion

The LogMeIn API for GoToWebinar says to use this code to refresh an Access Token after it expire:
curl -X POST "https://api.getgo.com/oauth/v2/token" \
-H "Authorization: Basic {Base64 Encoded consumerKey and consumerSecret}" \
-H "Accept:application/json" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=refresh_token&refresh_token={refresh_token}"
Tried recreating it in ColdFusion like this:
<cfhttp url="https://api.getgo.com/oauth/v2/token" method="post" result="httpResp" timeout="60">
<cfhttpparam type="header" name="Accept" value="application/json" />
<cfhttpparam type="header" name="Authorization" value="Basic ...abc123..." />
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded" />
<cfhttpparam type="body" encoded="false" value="grant_type=refresh_token&refresh_token=#refreshToken#" />
</cfhttp>
The error we are getting is "error":"invalid_grant".
Before I delve further, is the ColdFusion translation correct (assuming the refresh_token and Authorization encoded keys are correct)?

Curl call to ColdFusion - Instagram

I'm not experienced with cURL at all, but from what I can gather, it's equivalent to cfhttp.
I want to work with the Instagram API and authenticate a user. Their example uses cURL.
curl \-F 'client_id=CLIENT-ID' \
-F 'client_secret=CLIENT-SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=YOUR-REDIRECT-URI' \
-F 'code=CODE' \https://api.instagram.com/oauth/access_token
Would I be correct in thinking the CF version would be:
<cfhttp url="https://api.instagram.com/oauth/access_token" method="post" resolveurl="true">
<cfhttpparam type="formField" name="client_id" value="CLIENT-ID" />
<cfhttpparam type="formField" name="client_secret" value="CLIENT-SECRET" />
<cfhttpparam type="formField" name="grant_type" value="authorization_code" />
<cfhttpparam type="formField" name="redirect_uri" value="YOUR-REDIRECT-URI" />
<cfhttpparam type="formField" name="code" value="code" />
</cfhttp>
I'm not able to try this yet as I won't be at my development machine until a lot later, so I'm just digging around right now looking at possibilities and making pseudo-code (not tested!).
Anyone with specific experience with the Instagram API and ColdFusion who could shed some insight would be greatly appreciated.
I'm using Railo.
According to how to use cURL the -F option is form data.
http://curl.haxx.se/docs/manpage.html
So you're going to want to send form data, not url data in your cfhttpparam tags.
<cfhttp url="https://api.instagram.com/oauth/access_token" method="post" resolveurl="true">
<cfhttpparam type="formField" name="client_id" value="CLIENT-ID" />
<cfhttpparam type="formField" name="client_secret" value="CLIENT-SECRET" />
<cfhttpparam type="formField" name="grant_type" value="authorization_code" />
<cfhttpparam type="formField" name="redirect_uri" value="YOUR-REDIRECT-URI" />
<cfhttpparam type="formField" name="code" value="code" />
</cfhttp>
I don't know anything about the Instagram API and only what I read in the above link about cURL so this may not be the complete solution but it looks like you're on the right track.

How can I force a download of a pdf in a url?

I have a URL that goes to a pdf file. In my coldfusion page, I want to allow the user to download the file (using the open/save dialog or however that particular browser handles it).
This is the code I have so far:
<cfset tempFile = getTempFile(getTempDirectory(), 'testfile') />
<cfhttp url="myUrl/myFile.pdf" method="get" file="#tempFile#"/>
<cfheader name="Content-Disposition" value="attachment; filename=myFile.pdf">
<cfcontent type="application/pdf" file="#tempFile#">
This seems to work... but when I try to open the file it tells me something's wrong with the file. What am I doing wrong?
file attribute: Do not specify the path to the directory in this attribute; use
the path attribute.
Try separating the file name and path:
<!--- hard coded for clarity --->
<cfhttp url="http://www.somesite.com/path/testFile.pdf"
method="get"
getAsBinary="yes"
path="c:/test/"
file="testFile.pdf"/>
<cfheader name="Content-Disposition" value="attachment; filename=myFile.pdf" />
<cfcontent type="application/pdf" file="c:/test/testFile.pdf" />
For smaller files you might skip the temp file and use <cfcontent variable..>
<cfhttp url="http://download.macromedia.com/pub/documentation/en/coldfusion/mx7/cfmx7_cfml_qref.pdf"
method="get"
getAsBinary="yes" />
<cfheader name="Content-Disposition" value="attachment; filename=myFile.pdf" />
<cfcontent type="application/pdf" variable="#cfhttp.fileContent#" />
Update: Dynamic example using a temp file
<cfset tempDir = getTempDirectory() />
<cfset tempFile = getFileFromPath(getTempFile(tempDir, "testfile")) />
<!--- uncomment to verify paths
<cfoutput>
tempDir = #tempDir#<br />
tempFile = #tempFile#<br />
</cfoutput>
<cfabort />
--->
<cfhttp url="http://download.macromedia.com/pub/documentation/en/coldfusion/mx7/cfmx7_cfml_qref.pdf"
method="get"
getAsBinary="yes"
path="#tempDir#"
file="#tempFile#" />
<cfheader name="Content-Disposition" value="attachment; filename=myFile.pdf" />
<cfcontent type="application/pdf" file="#tempDir#/#tempFile#" />
As far as I know, your coding is fine in Google Chrome. In IE, error message prompt. It's because of "file path" cannot support for URL path. Should use directory path instead of URL path.

Trouble creating calendar in Google API via ColdFusion

I am trying to create a caledar using the Google API, and it just returns the list of calendars in my account, just like I sent a GET request. Here is my code:
<cfxml variable="locals.xml">
<cfoutput>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:gCal="http://schemas.google.com/gCal/2005">
<title type="text">#arguments.argTitle#</title>
<summary type="text">#arguments.argSummary#</summary>
<cfif len(arguments.argTimezone)><gCal:timezone value="#arguments.argTimezone#"></gCal:timezone></cfif>
<gCal:hidden value="false"></gCal:hidden>
<gCal:accesslevel value="owner" />
<gCal:color value="#arguments.argColor#"></gCal:color>
<gd:where rel='' label='' valueString='Oakland'></gd:where>
</entry>
</cfoutput>
</cfxml>
<cfhttp url="#variables.baseURL#/default/owncalendars/full" method="post" redirect="false" multiparttype="related" charset="utf-8">
<cfhttpparam type="header" name="Authorization" value="GoogleLogin auth=#getAuth(variables.serviceName)#">
<cfhttpparam type="header" name="Content-Type" value="application/atom+xml">
<cfhttpparam type="header" name="GData-Version" value="2">
<cfhttpparam type="body" value="#trim(locals.xml)#">
</cfhttp>
Any help would be appreciated.
CFXML creates a ColdFusion XML object. That's an internal CFML construct and is not going to mean anything to the receiving API. I expect that you need to convert it into text.
Try wrapping locals.xml with a ToString(). Like so:
<cfhttp url="#variables.baseURL#/default/owncalendars/full" method="post"
redirect="false" multiparttype="related" charset="utf-8">
<cfhttpparam type="header" name="Authorization" value="GoogleLogin
auth=#getAuth(variables.serviceName)#">
<cfhttpparam type="header" name="Content-Type"
value="application/atom+xml">
<cfhttpparam type="header" name="GData-Version" value="2">
<cfhttpparam type="body" value="#trim(toString(locals.xml))#">
</cfhttp>
I would start by outputting the XML you're sending into a text-box and displaying it on-screen, to verify that it's in the correct format:
<textarea rows="30" cols="120">
<cfoutput>#trim(toString(locals.xml))#</cfoutput>
</textarea>
Another approach you might consider would be to build your XML as a string, not a native ColdFusion XML object that you later convert to a string: (notice that I'm using CFSaveContent instead of CFXML)
<cfsavecontent variable="locals.xml">
<cfoutput>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:gCal="http://schemas.google.com/gCal/2005">
<title type="text">#arguments.argTitle#</title>
<summary type="text">#arguments.argSummary#</summary>
<cfif len(arguments.argTimezone)><gCal:timezone value="#arguments.argTimezone#"></gCal:timezone></cfif>
<gCal:hidden value="false"></gCal:hidden>
<gCal:accesslevel value="owner" />
<gCal:color value="#arguments.argColor#"></gCal:color>
<gd:where rel='' label='' valueString='Oakland'></gd:where>
</entry>
</cfoutput>
</cfsavecontent>