Windows Push Notification Services Token request via ColdFusion - coldfusion

I am trying to get the "access token" from WNS via a ColdFusion request but I get a "Bad Request" response. I believe I have everything set up correctly following the instructions here. My devices are registering the URI's to my backend.
Here is a simple code snippet;
(I've hidden the secret key of course)
<cfhttp url="https://login.live.com/accesstoken.srf" method="post" result="httpResp">
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded" />
<cfhttpparam type="URL" name="grant_type" value="client_credentials" />
<cfhttpparam type="URL" name="client_id" value="ms%2Dapp%3A%2F%2Fs%2D1%2D15%2D2%2D1197233413%2D3602308102%2D1084427847%2D2188608249%2D1036687727%2D3580410356%2D2392468796" />
<cfhttpparam type="URL" name="client_secret" value="************************" />
<cfhttpparam type="URL" name="scope" value="notify.windows.com" />
I am also getting failed response when I test the URL directly via the browser. Appreciate your help.
Thank you,
Ian.

I think your cfhttpparam fields now set to "URL" should actually be set to "FORMFIELD" based on the example on MSDN.
<cfhttpparam type="FORMFIELD" name="grant_type" value="client_credentials" />
<cfhttpparam type="FORMFIELD" name="client_id" value="..." />
<cfhttpparam type="FORMFIELD" name="client_secret" value="************************" />
<cfhttpparam type="FORMFIELD" name="scope" value="notify.windows.com" />
The example appears to put thes in the content area (showing a raw post) - so these are form fields of a post, not URL fields right?

Related

How do I create a new folder in Google Drive using the API via ColdFusion?

I've been trying to follow the documentation at https://developers.google.com/drive/v2/reference/files/insert and https://developers.google.com/drive/web/folder. However, I'm not clear on exactly what information I need to be passing during the http call. When I run the following code it simply creates a file that Drive says it doesn't know how to open:
<cfhttp method="post" url="https://www.googleapis.com/upload/drive/v2/files">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="URL" name="key" value="#myKey#">
<cfhttpparam type="URL" name="access_token" value="#myAccessToken#">
<cfhttpparam type="URL" name="corpus" value="DEFAULT">
<cfhttpparam type="URL" name="uploadType" value="multipart">
<cfhttpparam type="URL" name="Content-Type" value="application/json; charset=UTF-8 {""title"": ""Test 001"", ""parents"": [{""id"":""#validParentID#""}]}">
<cfhttpparam type="URL" name="Content-Type" value="application/vnd.google-apps.folder">
</cfhttp>
This code was derived from my call that successfully creates an empty file (although I've not yet gotten the title to work).
UPDATE
I updated the code per Jedihomer Townend's suggestion:
<cfhttp method="post" url="https://www.googleapis.com/upload/drive/v2/files">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="URL" name="key" value="#myKey#">
<cfhttpparam type="URL" name="access_token" value="#myAccessToken#">
<cfhttpparam type="URL" name="corpus" value="DEFAULT">
<cfhttpparam type="URL" name="uploadType" value="multipart">
<cfhttpparam type="URL" name="Content-Type" value="application/json; charset=UTF-8 {""mimeType"": ""application/vnd.google-apps.folder"" ""title"": ""Test 001"", ""parents"": [{""id"":""#validParentID#""}]}">
</cfhttp>
This had the same result as my original code.
BOUNTY
I'm starting a bounty on this now. In addition to resolving this particular problem, I'd also appreciate insights on how to apply Google's documentation to direct http calls. Despite REST being a technology designed around http requests, Google's docs (pun intended) seem to cover only an OOP approach to using it.

Google OAuth Code Contains Hashtag

In my attempts to get the necessary code so I can generate my refresh token, I ran this URL:
https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/drive.file&redirect_uri=--mywebsite--&response_type=code&client_id=--myclientid--
And my received code contains a trailing hash tag, which throws an (expected) error when I try to execute:
<cfhttp url="https://accounts.google.com/o/oauth2/token" method="post">
<cfhttpparam name="code" value="4/UXF5F5TlIuFsXrav-DvIrebMR8NST9WK-EPmThmx7l0#" type="formfield"> <!-- Sample code value -->
<cfhttpparam name="client_id" value="--myclientid--" type="formfield">
<cfhttpparam name="client_secret" value="[client secret]" type="formfield">
<cfhttpparam name="redirect_uri" value="--mywebsite--">
<cfhttpparam name="grant_type" value="authorization_code" type="formfield">
</cfhttp>
ColdFusion was looking at the following text:formfieldThe CFML compiler was processing:An expression that began on line 2, column 86.The expression might be missing an ending #, for example, #expr instead of #expr#.The tag attribute value, on line 2, column 34.A cfhttpparam tag beginning on line 2, column 10.
I tried adding a second hashtag to make it literal but I receive { "error" : "invalid_grant", "error_description" : "Invalid code." } response.
Am I missing something painfully obvious here? The tutorial I was following is at http://www.brandiandjohn.com/post.cfm/oauth-2-google-and-cfml-without-cfoauth
In order to continue with the existing code, you need to escape #. You can do that, by adding an extra # at the end. For eg
<!---
<cfset value="4/UXF5F5TlIuFsXrav-DvIrebMR8NST9WK-EPmThmx7l0#">
<cfoutput>#value#</cfoutput>
Error
--->
<cfset value="4/UXF5F5TlIuFsXrav-DvIrebMR8NST9WK-EPmThmx7l0##">
<cfoutput>#value#</cfoutput>
Output: 4/UXF5F5TlIuFsXrav-DvIrebMR8NST9WK-EPmThmx7l0#
So, you can try the below code:-
<cfhttp url="https://accounts.google.com/o/oauth2/token" method="post">
<cfhttpparam name="code" value="4/UXF5F5TlIuFsXrav-DvIrebMR8NST9WK-EPmThmx7l0##" type="formfield"> <!-- Sample code value -->
<cfhttpparam name="client_id" value="--myclientid--" type="formfield">
<cfhttpparam name="client_secret" value="[client secret]" type="formfield">
<cfhttpparam name="redirect_uri" value="--mywebsite--">
<cfhttpparam name="grant_type" value="authorization_code" type="formfield">
</cfhttp>
The hashtag error was a red herring. I got around this by making the call to get the code and then the access token as a single action. By passing in url.code I received the necessary credentials.
<cfhttp url="https://accounts.google.com/o/oauth2/token" method="post">
<cfhttpparam name="code" value="#url.code#" type="formfield">
<cfhttpparam name="client_id" value="--myclientid--" type="formfield">
<cfhttpparam name="client_secret" value="[client secret]" type="formfield">
<cfhttpparam name="redirect_uri" value="--mywebsite--">
<cfhttpparam name="grant_type" value="authorization_code" type="formfield">
</cfhttp>
<cfdump var="#foo.filecontent#">
Manually pasting the code into the cfhttpparam tag, even without the hashtag, would throw a 400 error. This way, it does not.
The user agent that sent you the code should have stripped the "#" character, see: Google OAuth service redirects to URL with a # sign at the end. Apparently it did not (and as such that user agent is broken) but you can strip it manually in your code before sending if off.

Twilio Rest API Filtering using Coldfusion11

I am trying to use the following code to access the Twilio Rest API to retrieve a list of incoming calls to my twilio number for a given date range.
https://www.twilio.com/docs/api/rest/call
<cfhttp url="https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxxxxxx/Calls" method="get" resolveurl="no" username="xxxxxx" password="xxxxx1">
<cfhttpparam name="To" type="url" value="myphone">
<cfhttpparam name="StartTime>=" type="url" value="2015-05-01">
<cfhttpparam name="StartTime<" type="url" value="2015-06-01">
</cfhttp>
When I try to connect with the above, I get a "Connection" error in coldfusion. It doesn't like the ">". I have also tried to put the StartTime> into a variable perform urlencodedformat() on it, but it didn't give the desired result.
When I query an individual day it works fine.
<cfhttp url="https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxxxxxx/Calls" method="get" resolveurl="no" username="xxxxxx" password="xxxxx1">
<cfhttpparam name="To" type="url" value="myphone">
<cfhttpparam name="StartTime" type="url" value="2015-05-01">
</cfhttp>
Based off Twilio code examples (I was referencing PHP examples) it appears the the variable name can be StartTime>, StartTime>=, StartTime<, StartTime<=.
In you code, you are calling the "StartTime"
<cfhttp url="https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxxxxxx/Calls" method="get" resolveurl="no" username="xxxxxx" password="xxxxx1">
<cfhttpparam name="To" type="url" value="myphone">
<cfhttpparam name="StartTime>=" type="url" value="2015-05-01">
<cfhttpparam name="StartTime<" type="url" value="2015-06-01">
</cfhttp>
As per https://www.twilio.com/docs/api/rest/call, you should use StartTime & EndTime. I am able to dump the httpResponse with the below code:-
<cfhttp url="https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxxxxxx/Calls" method="get" resolveurl="no" username="xxxxxx" password="xxxxx1" result="httpResponse">
<cfhttpparam name="To" type="url" value="myphone">
<cfhttpparam name="StartTime" type="url" value="2015-05-01">
<cfhttpparam name="EndTime" type="url" value="2015-06-01">
</cfhttp>
<cfdump var="#httpResponse#" label="httpResponse">
Also, https://www.twilio.com requires two SSL certificates. You need to import these to the keystore.

Mailchimp API listsubscribe() doesn't add subscriber to my list

I am implementing the Mailchimp API to my Coldfusion Project. I have performed all the steps that are required to add email to list.When I run the code it returns the message of true but when I look at my list there is no subscriber I found there? Below is my code
<cfhttp url="https://us5.api.mailchimp.com/1.3/" method="post" >
<cfhttpparam name="output" value="json" type="url">
<cfhttpparam name="method" value="listSubscribe" type="URL">
<cfhttpparam name="apikey" value="3xxxxxxxx1e7a-us5" type="url">
<cfhttpparam name="id" value="ccxxxx2b" type="url">
<cfhttpparam name="email_address" value="test#hotmail.com" type="url">
<cfhttpparam name="merge_vars[fname]" value="test" type="url">
</cfhttp>
<cfset result = JSStringFormat(cfhttp.filecontent)>
<cfdump var="#result#">
What is wrong in my code?
The answer to this question was already posted, but unfortunately with no attention, so I am re-posting it here to give you the solution.
After reading the answer by #alexleonard I figured out the problem. The solution to your question is just add an extra <cfhttpparam after the param fname:
<cfhttpparam name="double_optin" value="FALSE" type="url">
Now check it works or not?

ColdFusion CFHTTP GET strips cookie

I'm running into issues trying to pass a cookie in my CFHTTP GET Statement.
Has anyone else experienced this? Pointers would be appreciated. BTW - I am using the x-http-method-override, because I saw this on a few other sites that were discussing this topic.
<cfhttp url="#this.apiServer#/api/v#this.version#/user.json"
method="POST" result="jorn">
<cfhttpparam type="COOKIE" name="ms_user" value="#arguments.patAppSession#" />
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="header" name="x-http-method-override" value="GET" />
<cfhttpparam type="body" value='#variables.dataFields#' />
</cfhttp>
<cfdump var = "#jorn#">