Google Drive API with ColdFusion - 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

Related

Can't get Google photos album contents using google photos api (and ColdFusion cfhttp)

I'll keep this short. I have OAuth working with the Google Photos API. I am able to return albums and media items but not album specific media items.
Below I've done three requests. The first two work while the third does not.
Note: In the third request I've taken the albumID from the results returned in the first request.
<!--- This works --->
<cfhttp method="GET" charset="utf-8" url="https://photoslibrary.googleapis.com/v1/albums" result="res">
<cfhttpparam name="Authorization" type="header" value="OAuth #application.oauth2.GetAccess_token()#" />
</cfhttp>
<cfdump var="#deserializeJSON(res.filecontent)#">
<!--- This works --->
<cfhttp method="GET" charset="utf-8" url="https://photoslibrary.googleapis.com/v1/mediaItems" result="res">
<cfhttpparam name="Authorization" type="header" value="OAuth #application.oauth2.GetAccess_token()#" />
</cfhttp>
<cfdump var="#deserializeJSON(res.filecontent)#">
<!--- This doesn't. I get a 404 for this --->
<cfhttp method="POST" charset="utf-8" url="https://photoslibrary.googleapis.com/v1/mediaItems:search" result="res">
<cfhttpparam name="Authorization" type="header" value="OAuth #application.oauth2.GetAccess_token()#" />
<cfhttpparam name="albumId" type="formfield" value="AHeArAbj3EfLJqs23FHD9SWuReCaaPA0VBZxw6HNwL752bJr5X882_ZJnitk7kjZJwtzXvrw6hp2" />
</cfhttp>
<cfdump var="#res#">
This is the result I get for the last request;
What is wrong with the last request I am making? I am trying to use this API call: https://developers.google.com/photos/library/reference/rest/v1/mediaItems/search

ColdFusion Dropbox - Error in call to API function

I am trying to used Dropbox API from ColdFusion and I am running some simple tests to understand how it works. I am trying to list all the folders from my Dropbox account and I am using this piece of code:
<cfset jsonCall = StructNew()>
<cfset jsonCall.path = "Shared">
<cfhttp url="https://api.dropboxapi.com/2/files/list_folder" method="post">
<cfhttpparam type="header" name="Authorization" value="Bearer LXldZbIAwawAAAAAAAAYUHqCbBQjBPfoWybyRrhivVViX7qq2x8oPK45QZ5WZkGr"/>
<cfhttpparam type="header" name="Content-Type" value="application/json">
<cfhttpparam type="body" value="#serializeJSON(jsonCall)#">
</cfhttp>
Of course, the TOKEN is not real.
And I am getting the following error from Dropbox:
Error in call to API function "files/list_folder": request body:
unknown field 'PATH'
Can anyone help me with this error?

Load ColdFusion values from Form scope when name includes hyphen/dash

We're upgrading to Google's reCaptcha and it adds a field with name "g-recaptcha-response" to the form during submit. We them need to verify the response to Google with code like this:
<cfhttp url="https://www.google.com/recaptcha/api/siteverify" method="post" result="captchaResult">
<cfhttpparam type="formfield" name="secret" value="SECRET">
<cfhttpparam type="formfield" name="response" value="#Form.g-recaptcha-response#">
<cfhttpparam type="formfield" name="remoteip" value="#CGI.REMOTE_ADDR#">
</cfhttp>
However, the #Form.g-recaptcha-response# gives Element G is undefined in FORM.
Is there another way to reference Form scope to allow hyphens?
That will give you an error if the form variable doesn't exist. Try this instead:
<CFSET Form.Response = "">
<CFIF StructKeyExists(Form, "g-recaptcha-response")>
<CFSET Form.Response = form["g-recaptcha-response"]>
</CFIF>
I wrote a reCAPTCHA v2 UDF yesterday. I wanted to support sToken so I could use the same API key on multiple websites.
http://gist.github.com/JamoCA/c4e83ca402bd6175a1d7
Just found a solution:
<cfhttpparam type="formfield" name="response" value="#Form["g-recaptcha-response"]#">

Google oauth token giving 405 error

I am trying to post using below Code. I expect it to return token but its returning error 405 Method Not Allowed.
<cfhttp method="POST" url="http://accounts.google.com/o/oauth2/token" >
<cfhttpparam type="Formfield" name="code" value="#url.CODE#">
<cfhttpparam type="Formfield" name="client_id" value="458381219741.apps.googleusercontent.com">
<cfhttpparam type="Formfield" name="client_secret" value="XXXXXXX">
<cfhttpparam type="Formfield" name="redirect_uri" value="http://console.mbwebportal.com/oauth2callback">
<cfhttpparam type="Formfield" name="grant_type" value="authorization_code">
</cfhttp>
The above code is on http://console.mbwebportal.com/oauth2callback and it gets the Code in url after user allows access to the application.
Please help!!
I found the answer: key was to use cfhttpparam type 'body'.
As per livedocs "body: specifies the body of the HTTP request. ColdFusion does not automatically set a content-type header or URL encode the body contents. To specify the content-type, use a separate cfhttp tag with type=header. "
Below code is returning me access token now :)
<cfset client_id = "458381219741.apps.googleusercontent.com">
<cfset client_secret = "**********">
<cfset callback = "http://console.mbwebportal.com/oauth2callback">
<cfset postBody = "code=" & UrlEncodedFormat(url.code) & "&">
<cfset postBody = postBody & "client_id=" & UrlEncodedFormat(client_id) & "&">
<cfset postBody = postBody & "client_secret=" & UrlEncodedFormat(client_secret) & "&">
<cfset postBody = postBody & "redirect_uri=" & UrlEncodedFormat(callback) & "&">
<cfset postBody = postBody & "grant_type=authorization_code">
<cfhttp method="post" url="https://accounts.google.com/o/oauth2/token">
<cfhttpparam name="Content-Type" type="header" value="application/x-www-form-urlencoded">
<cfhttpparam type="body" value="#postBody#">
</cfhttp>
Found a similar post here Google OAuth 2 authorization - swapping code for token. The answer for them was to url encode the client secret key and redirect uri. In ColdFusion you can use the URLEncodedFormat() function to do that for you.
<cfhttp method="POST" url="http://accounts.google.com/o/oauth2/token" >
<cfhttpparam type="Formfield" name="code" value="#url.CODE#">
<cfhttpparam type="Formfield" name="client_id" value="458381219741.apps.googleusercontent.com">
<cfhttpparam type="Formfield" name="client_secret" value="#URLEncodedFormat(XXXXXXX)#">
<cfhttpparam type="Formfield" name="redirect_uri" value="#URLEncodedFormat("http://console.mbwebportal.com/oauth2callback")#">
<cfhttpparam type="Formfield" name="grant_type" value="authorization_code">
</cfhttp>
And please validate your url.CODE value before using it as anything can be passed in the URL.

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.