How to download a pdf file from Dropbox API response - coldfusion

I am getting a response from Coldbox API in ColdFusion but I am unable to convert this response to PDF in Coldfusion. I have attached an image of the response below.
Here is my code:
<cfhttp result="get" method="POST" url="https://content.dropboxapi.com/2/files/download" getAsBinary="yes" >
<cfhttpparam type="header"name="Authorization" value="#token#">
<cfhttpparam type="header" name="Dropbox-API-Arg" value="#serializeJSON(pathData)#">
</cfhttp>
<cfheader name="content-disposition" value="inline; filename=test.pdf" />
<cfheader name="content-transfer-encoding" value="binary" />
<cfcontent type="application/pdf" variable="#get.Filecontent#" reset="true" />
I want to Hit API and download the file in next tab.
Let me know If anyone can help me to fix that issue.
Thanks.

Related

convertAPI version 2 docx to PDF

I successfully made a connection to version 1 using the below code:
<cfhttp method="post"
url="http://do.convertapi.com/Word2Pdf"
result="convertAttempt"
path="#arguments.path#"
file="#arguments.fileToDestination#"
>
<cfhttpparam type="formfield" name="ApiKey" value="xxxxxxx" >
<cfhttpparam type="file" file="#arguments.path#/#arguments.fileToConvert#" name="File" >
</cfhttp>
Below is the code I am trying to use for version 2. It writes a file to the correct folder, but it's not a readable PDF. I think it has something to do with base64, but not sure. Anyway, hoping there's another ColdFusion user out there to help me out. Then, we hopefully get code samples on the convertAPI site to help others.
<cfhttp method="post"
url="http://v2.convertapi.com/docx/to/pdf?Secret=mysecret"
result="convertAttempt"
path="#arguments.path#"
file="#arguments.fileToDestination#"
>
<cfhttpparam type="file" file="#arguments.path##arguments.fileToConvert#" name="File" >
</cfhttp>
By default ConvertAPI version 2 returns JSON. You need to decode the file using a Base64 decoder.
To save response time and bandwidth, better add the accept=application/octet-stream header to the request, to get an instant binary response without any decoding.
Using the suggestions in the comments and Tomas's answer, here is my final code. It first deserializes the response from JSON. Then decodes the pdf from base64 into binary. Finally, saves the binary pdf file to disk.
<cfhttp method="post" url="http://v2.convertapi.com/docx/to/pdf?Secret=your-secret" result="convertAttempt">
<cfhttpparam type="file" file="#arguments.path##arguments.fileToConvert#" name="File" >
</cfhttp>
<cfset FileResult = deserializeJSON(convertAttempt.FileContent) />
<cfif isDefined("fileResult.Code")>
<!--- Failed --->
<cfelse>
<cfset FileWrite("#arguments.path##arguments.fileToDestination#", BinaryDecode(FileResult.Files[1].FileData, "base64"))>
</cfif>

Attempting to test Docusign REST API in ColdFusion

I found this example and am using it to test the DocuSign REST API in my ColdFusion app. It is the whole of it. I am not not very familiar with the ColdFusion language and am pretty sure I am not handling a response from the server in any way through this request. I do not receive an error and I think that is why and the document/email are not sent.
Can anyone point out where my errors are and help me build in code that will return the error so that I can further troubleshoot the issue? Thanks!
<cffile action="READBINARY" file="875487865.pdf" variable="docData">
<cfset docData = BinaryEncode(docData,"Base64")>
<cfset envelope = "<envelopeDefinition xmlns=""http://www.docusign.com/restapi"">
<status>Sent</status>
<emailSubject>eSignature request</emailSubject>
<emailBlurb>Please sign the document</emailBlurb>
<recipients>
<signers>
<signer>
<recipientId>1</recipientId>
<name>John Doe>john#doe.com</email>
<tabs>
<signHereTabs>
<signHere>
<anchorString>1SI</anchorString>
<documentId>1</documentId>
</signHere>
</signHereTabs>
</tabs>
</signer>
</signers>
</recipients>
<documents>
<document>
<name>875487865.pdf</name>
<documentId>1</documentId>
<fileExtension>pdf</fileExtension>
<documentBase64>#docData#</documentBase64>
</document>
</documents>
</envelopeDefinition>">
<cfhttp url="https://demo.docusign.net/restapi/v2/login_information/envelopes" method="POST" resolveurl="Yes" throwonerror="No">
<cfhttpparam name="X-DocuSign-Authentication" type="HEADER" value=" <DocuSignCredentials><Username>MY_USER_NAME</Username><Password>MY_PASSWORD</Password><IntegratorKey>MY_INT_KEY</IntegratorKey></DocuSignCredentials>">
<cfhttpparam name="Content-Type" type="HEADER" value="application/xml">
<cfhttpparam name="Accept" type="HEADER" value="application/xml">
<cfhttpparam name="Content-Length" type="HEADER" value="#Len(envelope)#">
<cfhttpparam name="request_body" type="BODY" value="#envelope#">
</cfhttp>
Just as Leigh suspected, you are not hitting a valid DocuSign endpoint. I see your POST URL set to this:
https://demo.docusign.net/restapi/v2/login_information/envelopes
Where did you get this URL from as it's not valid? You need to go through the "Making your First API call" section of the DocuSign Developer Center to learn how to properly login (authenticate) and create an envelope / signature request.
https://www.docusign.com/developer-center/quick-start/first-api-call

Google Drive API with ColdFusion

I started to work on Google Drive API with ColdFusion and I am stuck to upload the file using ColdFusion. I have done with the registration of new project, getting client and client secret and I am successfully able to get the accessToken but somehow I am not able to upload the file on the google drive.
Here is the code to get the code and accesstoken
<cfoutput>
<cfset request.oauthSettings = {
scope = "https://www.googleapis.com/auth/drive", client_id = "clientid",
client_secret = "clientsecret",
redirect_uri = "link"}
/>
<!--- create login url --->
<cfset loginURL = "https://accounts.google.com/o/oauth2/auth?scope="
& request.oauthSettings["scope"]
& "&redirect_uri=" & request.oauthSettings["redirect_uri"]
& "&response_type=code&client_id=" & request.oauthSettings["client_id"]
& "&access_type=offline"
/>
Login with Google account that has access to analytics
<cfif isDefined("URL.code") AND URL.code NEQ "access_denied">
<cfhttp url="#arguments.gaOauthUrl#" method="post">
<cfhttpparam name="code" type="formField" value="#arguments.code#">
<cfhttpparam name="client_id" type="formField" value="clientid">
<cfhttpparam name="client_secret" type="formField" value="clientsecret">
<cfhttpparam name="redirect_uri" type="formField" value="link">
<cfhttpparam name="grant_type" type="formField" value="authorization_code">
</cfhttp>
</cfif>
</cfoutput>
I am using the following code to upload the file, I know I have to pass some more parameters to make it correct but I don't know what are that parameters.
<cfhttp url="https://www.googleapis.com/upload/drive/v2/files?uploadType=media" method="post">
<cfhttpparam name="Content-Type" type="formField" value="text/plain">
<cfhttpparam name="Authorization" type="formField" value="#session.ga_accessToken#">
</cfhttp>
I am trying to find out in the google docs but no luck; there is no documentation for ColdFusion. Please let me know the other parameters if someone has some clue about this area.
You aren't setting the Authorization header correctly. It should be
Authorization: Bearer ya29.AHES6ZRosLBEnyGGH9EysIrAB7Z

Consuming Salesforce WSDL via Coldfusion Issue

Ive been given a class file from another group of SFDC developers that work in a separate instance that generates a SOAP based WSDL. I have imported that class file and generated the WSDL in our instance. The webservice is very basic, it just returns a set of values (in XML of course) and doesn't require any arguments to retrieve those values.
Just to test that the WSDL is working Ive downloaded the WSDL from our instance to my local PC and put it into SOAPUI and successfully returned the set of values.
NOW ONTO THE ISSUE - CONSUMING VIA THE URL
Im using Coldfusion's CFHTTP, so I have two CFHTTP calls, the first is to the login of our instance which returns a valid session ID. This session ID is used in my second call which is below:
<!--- token from from first cfhttp --->
<cfset variables.access_token_node = xmlSearch(XMLContent, "//*[name()='sessionId']") />
<cfset variables.access_token = variables.access_token_node[1].xmlText>
<cfset variables.wsdl_url2 = "https://cs12.salesforce.com/services/wsdl/class/WS_FAKE_WSDL">
<cfset packet = CreateObject("java", "java.lang.StringBuffer") />
<cfset packet.append('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://soap.sforce.com/schemas/class/WS_FAKE_WSDL">') />
<cfset packet.append('<soapenv:Header>') />
<cfset packet.append('<ws:SessionHeader>') />
<cfset packet.append('<ws:sessionId>#variables.access_token#</ws:sessionId>') />
<cfset packet.append('</ws:SessionHeader>') />
<cfset packet.append('</soapenv:Header>') />
<cfset packet.append('<soapenv:Body>') />
<cfset packet.append('<ws:DescribesObjectFields/>') />
<cfset packet.append('</soapenv:Body>') />
<cfset packet.append('</soapenv:Envelope>') />
<cfhttp method="post" url="#variables.wsdl_url2#" result="findResponse2">
<cfhttpparam type="HEADER" name="Accept" value="application/soap+xml, application/xml, multipart/related, text/*">
<cfhttpparam type="HEADER" name="ACCEPT-ENCODING" value="application/soap+xml">
<cfhttpparam type="HEADER" name="CONNECTION" value="Keep-Alive">
<cfhttpparam type="HEADER" name="SOAPAction" value="dummy">
<cfhttpparam type="HEADER" name="Content-Type" value="text/xml; charset=utf-8">
<cfhttpparam type="HEADER" name="Must-Understand" value="1">
<cfhttpparam type="Header" name="Content-Length" value="#len(trim(packet.ToString()))#">
<cfhttpparam type="body" value="#packet.ToString()#" encoded="yes">
</cfhttp>
Below is what I am receiving from the WSDL..that code just takes me back to the login screen. With a Session ID I would assume I wouldn't need to log back in, correct?
All advice is appreciated.
You appear to be sending your APi call to the URL of the WSDL file, this is not the correct URL, you should be sending it to the service address that's detailed within the WSDL file, see the soap:address element in the service element at the bottom of the WSDL.

Upload google docs with coldfusion

I've built a forum-like app in ColdFusion and I want to add a feature with which the users can upload files to Google Docs using their Google accounts and then other users can edit those files.
I've been using this CFC: http://cfgoogle.riaforge.org/ to retrieve Google Docs, but the upload function is currently missing. I need the upload function for the first upload of the file and then for the second part of editing it. I hope that it makes sense.
That's what I'm asking help for. I'm not that experienced with cffunctions and I was wondering if someone could give me a hand with it.
This is what I have so far:
<cffunction name="upload" access="public" returnType="any" hint="I upload the document." output="false">
<cfargument name="myFile" type="string" required="true" hint="file to upload.">
<cfset var result = "">
<cfset var service = variables.docservice>
<cfset theUrl = "https://docs.google.com/feeds/documents/private/full HTTP/1.1">
<cfhttp url="#theURL#" method="post" result="result">
<cfhttpparam type="header" name="Authorization" value="GoogleLogin auth=#getAuth(service)#">
<cfhttpparam type="header" name="Content-Length" value="81047">
<cfhttpparam type="header" name="Content-Type" value="application/msword">
<cfhttpparam type="header" name="Slug" value="#myFile#">
</cfhttp>
<cfreturn result.filecontent>
</cffunction>
But I get the following error when I output the result:
'Invalid request URI'
If someone could help me out with this one (even just show me the way), it would be mostly appreciated.
I would try to expand cfgoogle CFC with upload method using Protocol Guide for uploading.