I'm trying to download a .csv file on a remote server location using ColdFusion (version 2016). I used the cfhttp tag to perform this operation but I keep getting the following error:
401 UNAUTHORIZED
I checked with the server admin of the remote server to verify that I have the right domain name, userid and password and the admin confirmed it. I couldn't find anything similar on SO, hence posting this question. Hoping someone can help me out.
PS: I don't have access to the ColdFusion Administrator since it is hosted by a separate team.
Below is my code (actual values replaced with dummy data for security):
<cfhttp url="https://xxx.yyy.com/abcd/xyz/myfolder/myFile.csv">
<cfhttpparam type="header" name="Authorization"
value="Basic #ToBase64("MydomainName\Myuserid:Mypassword")#" />
</cfhttp>
<cfdump var="#cfhttp.filecontent#" label="CSV file content">
<cfabort>
It might be that your auth string is wrong. To avoid confusion and while debugging, I would suggest something like this:
<cfset myDomainName = "MydomainName" />
<cfset myUserId = "Myuserid" />
<cfset myPassword = "Mypassword" />
<cfset authString = "Basic " & ToBase64('#myDomainName#' & '\' & '#myUserId#' & ':' & '#myPassword#') />
<cfhttp url="https://xxx.yyy.com/abcd/xyz/myfolder/myFile.csv">
<cfhttpparam type="header" name="Authorization"
value="#authString#" />
</cfhttp>
<cfdump var="#cfhttp.filecontent#" label="CSV file content">
<cfabort>
I hope this helps.
I'm having a terrible time...I am trying to get coldfusion to get a token from salesforce, but I am stuck on the following error
{"error":"invalid_grant","error_description":"authentication failure"}
ive tried cfscript
local.http = new Http(url='https://test.salesforce.com/services/oauth2/token',method='post');
local.http.addParam(type='header',name='content-type', value='application/x-www-form-urlencoded');
local.http.addParam(type='formField',name='grant_type', value='password');
local.http.addParam(type='formField',name='client_id', value='client_id');
local.http.addParam(type='formField',name='client_secret', value='client_password_string');
local.http.addParam(type='formField',name='username', value='user#email.com');
local.http.addParam(type='formField',name='password', value='userspassword');
local.http.addParam(type='formField',name='format', value='json');
local.httpSendResult = local.http.send();
rc.httpResult = httpSendResult.getPrefix();
ive tried cfhttp tags
<cfhttp url="https://test.salesforce.com/services/oauth2/token" method="POST">
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded" />
<cfhttpparam type="formField" name="grant_type" value="password" />
<cfhttpparam type="formField" name="client_id" value="client_id" />
<cfhttpparam type="formField" name="client_secret" value="client_password_string" />
<cfhttpparam type="formField" name="username" value="user#email.com" />
<cfhttpparam type="formField" name="password" value="userspassword" />
</cfhttp>
<cfset rc.result = cfhttp.fileContent />
however the same exact call executed by cUrl on my local machine works perfectly fine
curl -d "grant_type=password"
-d "client_id=client_id"
-d "client_secret=client_secret_string"
-d "username=user#email.com.dev"
-d "password=userpassword" https://test.salesforce.com/services/oauth2/token
ive made sure that my ip range is whitelisted, my ip relaxation is set to relax, allusers may self authorize, ive tried different username and password parameters, all i get is the same error about invalid grants
any help is very appreciated
I am not able to comment, so I have to answer :) But this is really just a suggestion. When you use <cfhttpparam type="formfield">, by default ColdFusion URL encodes that for you. ColdFusion will encode the characters ~, ., -, and _, but it should not actually do that according to the RFC 3986 spec (see https://en.wikipedia.org/wiki/Percent-encoding), since they are unreserved characters. If your formfields contain those characters (and I suspect they do since your example shows an email address), it is possible that those characters being encoded incorrectly is what is causing the authentication failure.
As a quick test you could add encoded="false" to your <cfhttpparam> tags and then url encode their values yourself using either encodeForUrl() or urlEncodedFormat() (depending on your version of ColdFusion), and then undo the incorrect encoding:
<cfhttpparam type="formField" encoded="false" name="username" value="#replacelist(urlEncodedFormat('user#email.com'), '%2D,%2E,%5F,%7E', '-,.,_,~')#">
Below is some CFScript code pulled out of a CFC I created for accessing salesforce. (won't run as is, but the logic is there - sorry it was too much code to copy the complete thing). Hoping it will help.
I'm using predominantly the same approach as you, but you might want to steal some of the specific settings (charset, content type, accept).
I've faked the values in the loginCredentials struct, but they are roughly the same kind of format (to compare with your own).
This runs fine for us on Railo. Maybe also check that it's not a SSL issue - do you need to add the SSL certs into Coldfusion so it can talk to Salesforce?
variables.sfAuthDomain = "https://login.salesforce.com";
variables.authServiceURL = variables.sfAuthDomain & "/services/oauth2/token";
variables.accessToken = "xxxxx" // The access token returned by SF, used on future logins
variables.loginCredentials = {
"grant_type": "password",
"client_id": "3MVG9Fkjshdkfjvshd ckjfhjkch.blkjlkjlkjkljl.wkjhgkjhkjhds.mVk84TRzhm_pXxK6_786786",
"client_secret": "3887687686868668727",
"username": "huge.duck#monkey.com.icom.icomqa",
"password": "Bungerloo!PPkjhj324ij45bQGyymmd"
};
/**
* MAKE SERVICE CALL
* Makes HTTP service call
**/
public Struct function makeServiceCall(String serviceUrl, String method="GET", Boolean sfAuth=true, Struct headers={}, Struct formFields) {
var httpService = new http(); // create new http service
var httpResponse = {};
var fieldName = "";
var bodyData = "";
/* set attributes using implicit setters */
httpService.setMethod(arguments.method);
httpService.setCharset("utf-8");
httpService.setUrl(Trim(arguments.serviceURL));
httpService.setTimeOut(variables.timeoutValue);
/* add httpparams using addParam() */
for(fieldName in arguments.headers) {
httpService.addParam(type="header", name="#fieldName#", value="#arguments.headers[fieldName]#");
}
if(arguments.sfAuth){
httpService.addParam(type="header", name="Authorization", value="OAuth #variables.accessToken#");
}
if(StructKeyExists(arguments, "formFields")) {
loop collection="#arguments.formFields#" item="fieldName" {
bodyData = ListAppend(bodyData, "#fieldName#=#formFields[fieldName]#", "&");
}
}
if(bodyData is not "") {
httpService.addParam(type="body", name="post", encoded="no", value="#bodyData#");
}
/* make the http call to the URL using send() */
httpResponse = httpService.send().getPrefix();
//dump(httpResponse, false, "modern", "", 500, "httpResponse");
return httpResponse;
}
httpResponse = makeServiceCall(
serviceUrl = serviceURL,
method = "POST",
sfAuth = false,
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"accept": "text/json"
},
formFields = loginCredentials
);
Ended up changing my coldfusion server to use TLS 1.2, as TLS 1.0 was deprecated for use by salesforce. Hope my answer and the helpful answers given on this post help someone else
Your password formfield value is contain only password, so that kind of error is shown:
({"error":"invalid_grant","error_description":"authentication failure"}).
That must be contain(password and security token).
<cfhttpparam type="formField" name="password" value="#arguments.password##arguments.Security_token#"/>
Afraid to ask this question as I'm not able to create a jsfiddle for it, but hope someone will be able help.
I'm trying to create a cfc in ColdFusion for an instagram login. That part is done. I'm using postman (google app) and by my credentials I can see the user's data in json, but when I'm converting this into ColdFusion it's giving an error. I tried to change the data-type, header and a lot of lines but am still getting the same error again and again.
My code (replaced ids with xxx for security)
<cftry>
<cfhttp url="https://api.instagram.com/oauth/access_token" method="post" resolveurl="yes">
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded" />
<cfhttpparam type="formfield" name="client_id" value="14faxxxxxdc5440f86x6cdd8xxxxf78" />
<cfhttpparam type="formField" name="client_secret" value="40xa78220cfb" />
<cfhttpparam type="formField" name="grant_type" value="authorization_code" />
<cfhttpparam type="formField" name="redirect_uri" value="#URLEncodedFormat('http://example.com/demo/instagramAPI/success.cfm')#" />
<cfhttpparam type="formField" name="code" value="#url.code#" />
</cfhttp>
<cfdump var="#cfhttp#"><cfabort>
<cfcatch type="any">
<cfdump var="#cfcatch#">
</cfcatch>
</cftry>
I'm following this code from this answer. For more info check this . You can see that I'm getting data by using the same login details, but when doing the same via a cfc I'm getting error.
Error which I'm getting after running the url :
I've read a lot of articles and blogs, but still haven't been able to resolve the error. Can anyone help me understand what I'm doing wrong? If you have any other suggestions, please do let me know.
If any additional information is required, just let me know.
Finally i got the answer of my question after days.. Thanx Miguel-F and Mark A Kruger however Mark your link wasn't good for me as that was out of my issue so..
What i did is to update my SSL certificate. I tried before but was not having much information like Organization unit etc but then i followed the steps given in this Link, provided by Miguel and tested then i got expiration code error.
After that i tried to refresh with ?reinit=1 as i made changes in my cfc but forget to reinitialize after updating the certificate, and then i got the result :)
SO final answer is Update your SSL certificate with proper authorization and cfc can fetch data from Instagram..
Link useful for me One, CFC demo. In cfc demo you can download the cfc for instagram which is also useful (even in case you don't have to update the SSL certificate).
If anyone having issue with Instagram cfc then do let me know.. I spent days on this and can help you.. :)
I have been hearing a lot about some new encryption needed for submitting transactions to Authorize.net but cannot find anything that explains it.
The AIM instructions for 2015 describe what I am already using except the post address has changed.
When I just submitted a transaction to the new address it was rejected.
Here is my code:
<cflock timeout="30" throwontimeout="No" name="12345">
<cfhttp url="https://secure.anetsgateway.net/gateway/transact.dll" method="post">
<cfoutput>
<cfhttpparam type="Formfield" name="x_login" value="myclient12345">
<cfhttpparam type="Formfield" name="x_tran_key" value="myxtrankey12345">
<cfhttpparam type="Formfield" name="x_version" value="3.1">
<cfhttpparam type="Formfield" name="x_test_request" value="TRUE">
The "x_tran_key" and "x_login" are hard-coded in my submission page.
Can someone explain where the issue is and examples of what special encryption I need.
It looks like you are using the wrong URL to talk to Authorize.Net. You should consult the documentation for the correct URL and proper method for integrating.
I would also like to add that placing your transaction key in the form is not a very good idea. It should be treated like a password.
I'm searching for a way to create a new CouchDB user without using Futon or Curl... just a straight http request.
One way I found (http://stackoverflow.com/questions/3456256/error-creating-user-in-couchdb-1-0) puts a JSON doc to "http://localhost:5984/_users/org.couchdb.user:username" to create a user.
I have attempted the following:
<cfhttp url="http://127.0.0.1/_users/org.couchdb.user:xyz_company" port="5984" method="PUT" username="#variables.couch_username#" password="#variables.couch_password#">
<cfhttpparam type="header" name="Content-Type" value="application/json">
<cfhttpparam type='body' name='org.couchdb.user:xyz_company' value='{"roles":[],"name":"xyz_company","salt":"3B33BF09-26B9-D60A-8F469D01286E9590","id":"org.couchdb.user:xyz_company","password_sha":"096EA41A5A81EA1507F2C6F7EDC364C0B82694AC","type":"user"}'>
I keep receiving the following back from Couch:
cfhttp.statuscode = 405 Method Not Allowed
cfhttp.filecontent = Method Not Allowed; The requested method PUT is not allowed for the URL /_users/org.couchdb.user:xyz_company
Any thoughts or suggestions?
UPDATE:
I edited my code based on Marcello's suggestions. I still receive the same 405 Method Not Allowed error. Here is the code now:
<cfhttp url="http://127.0.0.1/_users/org.couchdb.user:xyz_company" port="5984" method="PUT" username="#variables.couch_username#" password="#variables.couch_password#"><cfhttpparam type="header" name="Content-Type" value="application/json;charset=UTF-8"><cfhttpparam type='body' value='{"roles":[],"name":"xyz_company","salt":"3B33BF09-26B9-D60A-8F469D01286E9590","_id":"org.couchdb.user:xyz_company","password_sha":"096EA41A5A81EA1507F2C6F7EDC364C0B82694AC","type":"user"}'></cfhttp>
Any more suggestions? Thank you!
curl is a straight http request. There are other ways to create such requests: you can craft them with your browser; you can use a different program (e.g. wget); or even write your own (e.g. in Python or in JavaScript with V8 or Rhino).