Google oauth token giving 405 error - coldfusion

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.

Related

Square API and ColdFusion

I am trying to take an existing ColdFusion website that currently uses Authorize to process cards. I would like to switch over the payment gateway and use Square. Does anyone have any sample ColdFusion code on how to use the Square API as a payment gateway?
Here is a gist another developer has used
<cfset IDKey = CreateUUID()>
<cfset request.params.card_nonce = form.nonce>
<cfset request.params.amount_money.amount = 100>
<cfset request.params.amount_money.currency = 'USD'>
<cfset request.params.idempotency_key = IDKey>
<cfset jsonString = serializejson(request.params)>
<cfset requestPath = "https://connect.squareup.com/v2/locations/<replace_locationid>/transactions">
<cfhttp url="#requestPath#" method="post" result="response">
<cfhttpparam type="HEADER" name="Accept" value="application/json">
<cfhttpparam type="HEADER" name="Content-Type" value="application/json">
<cfhttpparam type="HEADER" name="Authorization" value="Bearer <replace_access_token>">
<cfhttpparam type="body" name="params" value="#jsonString#">
</cfhttp>

cfhttp to the stats.ezhostingserver website not working

I have the following Updated code where I am trying to connect to the website using the username/password and siteid combination, i am passing all values using get method and trying to fetch cookie to authenticate and go ahead but somehow i am again getting redirected to the login screen
My Code
<cfhttp method="get" url="https://stats.ezhostingserver.com/Login.aspx" resolveurl="true" redirect="false">
<cfhttpparam type="URL" name="ctl00$MPH$txtUserName" value="********">
<cfhttpparam type="URL" name="ctl00$MPH$txtPassword" value="********">
<cfhttpparam type="URL" name="ctl00$MPH$txtSiteId" value="*****">
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
<cfset stm_cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfset stm_temp = REReplace(stm_cookies, ";.*", "")>
<cfset stm_cookieName = listfirst(stm_temp,'=')>
<cfset stm_cookievalue = listlast(stm_temp,'=')>
<cfhttp method="get" url="https://stats.ezhostingserver.com/default.aspx" charset="utf-8" result="results" redirect="no">
<cfhttpparam type="cookie" name="#stm_cookieName#" value="#stm_cookievalue#">
</cfhttp>
<cfoutput>#results.filecontent#</cfoutput>
But it is saying me object moved error

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.

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

Push value to DucksBoard in ColdFusion

I going to implement the DucksBoard API. I made my custom counter on Ducksboard now I want to PUSH value. I read the tutorial I just found this line on DUCKSBOARD to push data
curl -v -u YOUR_API_KEY:unused -d '{"value": 10}' https://push.ducksboard.com/values/235
SO i try something like this in my Coldfusion code
<cfset var1 = '{"value":5}'>
<cfhttp url="https://push.ducksboard.com/values/xxxx" method="post" result="httpResp" timeout="60">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#serializeJSON(var1)#">
</cfhttp>
There is less information in the site of Duckboard.Can someone has implemented this.If yes then kindly tell me how to push value and where to use my API key?
I've not used Duckboard but it looks like what you have is correct. The CFHTTP tag takes 2 more parameters, username and password which you can fill in with your username and your API password like so:
<cfset var1 = '{"value":5}'>
<cfhttp url="https://push.ducksboard.com/values/xxxx" method="post"
result="httpResp" timeout="60" username="myusername" password="mypassword"
>
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#serializeJSON(var1)#">
</cfhttp>
I study the API and found the solution You can use this code and change with yours this will work.And don't use serializeJSON in body tag.
<cfset var1 = '{"value":5}'>
<cfhttp url="https://push.ducksboard.com/v/xxxx" method="post" username="API-key" password="x" result="httpResp" >
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value='#var1#'>
</cfhttp>