cfhttp and Stripe APi - coldfusion

I'm trying to get the stripe api working with 'cfhttp' but not having much luck.
<cfhttp
url="https://api.stripe.com/Stripe/Stripe::setApiKey('MyLiveKeyHere');"
authType="Basic" name="StripeAPI" method="Post">
>
<cfoutput>#StripeAPI.statusCode#</cfoutput>
This gives me an error
Message
Invalid CSV line size, expected 1 columns but found 2 instead
Detail
[message: You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. 'Authorization: Bearer YOUR_SECRET_KEY').
I cant find anything online that is helping me - anyone got any ideas?
Thanks

Found out how to do it - through trial and error...
<cfhttp url="https://api.stripe.com/v1/customers/*CustID*" method="Get">
<cfhttpparam type="header" name="Authorization" value="Bearer *Live Key Here*">
</cfhttp>

To authenticate with the Stripe API, you need to use either basic auth or a bearer token rather than placing the API key in the url. For example, to list charges you could do something like this:
<cfhttp url="https://api.stripe.com/v1/charges" method="get" username="sk_test_xxxyyyz">
You'd then want to de-serialize the JSON response.

Related

Cfhttp and mailgun api

I’ve been trying to configure Mailgun API with Coldfusion's Cfhttp and can’t figure out the format to pass the variables. I configured it in about 5 minutes with Python, and have to roll it to a Coldfusion front end. I’ve used the same basic structure as I did in Python, and it all works great in Python.
I’ve been able to make a connection, but I have not been able to send mail. I know it has to do with the structure, and I can’t find a working example anywhere.
How do you build the cfhttp to pass the required data? Is the data=data I would use in the request in Python the same as a cfhttpparam in ColdFusion? And is it a header, or a body?
My Coldfusion code is below, i've replaced secret information with domain.com
<cfscript>
mailgun_variables = '{
"company_id": 1008,
"company_contact": "Joe Smith",
"random_key": "91A303C8-91FB-AA1D-DE07F18782594721"
}';
data= '{
"from": "email#domain.com",
"to": "email#domain.com",
"subject": "Subject line",
"template": "welcome",
"o:tag": ["welcome", "welcome-countdown", "welcome-alt", "coldfusion"],
"h:X-Mailgun-Variables": #mailgun_variables#}';
</cfscript>
<cfhttp url="https://api.mailgun.net/v3/domain.com/messages"
method="POST"
username="api"
password="APIKEY"
result="response" >
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="header" name="data" value="#serializeJSON(data)#">
</cfhttp>
My error message is a 400 Bad request, {"message":"from parameter is missing"}.
Any guidance or links to examples would be much appreciated. Or maybe you can obviously spot what I am doing wrong?
I ended up having to do a lot of trial and error, but have working code if anyone else ever runs into this issue. Mailgun support was no help.
The answer ended up being that everything had to be passed as cfhttpparam formfields. I also ran into a lot of challenges with tagging and using template variables, but got it all worked out.
I added it to Github for anyone to use, and will continue to improve it there as I get more in-depth with it.
https://github.com/convurt/Mailgun-Coldfusion-Sendmail
cfhttp(method="POST", url="https://api.mailgun.net/v3/DOMAIN.COM/messages", result="response", username="api", password="MAILGUN API KEY") {
cfhttpparam(name="from", type="formfield", value="#email_from#");
cfhttpparam(name="h:Reply-To", type="formfield", value="#reply_to#");
cfhttpparam(name="to", type="formfield", value="#email_to#");
cfhttpparam(name="subject", type="formfield", value="#email_subject#");
// loop over tag list, I could not get it to work correctly as a list
for (t in tags) {
cfhttpparam(name="o:tag", type="formfield", value="#t#");
}
cfhttpparam(name="template", type="formfield", value="#template#");
// custom paramamter for template
cfhttpparam(name="t:variables", type="formfield", value="#t_variables#");

Difference between header and cookie for cfhttpparam type

I am working on a ColdFusion app that authenticates to SharePoint Online and pulls some files using SharePoint's REST API as described in http://paulryan.com.au/2014/spo-remote-authentication-rest/
When I try to obtain the FormDigestValue by posting to _api/contextinfo if I set the cfhttpparam type to cookie I get a 403 forbidden, but if I pass the cookies as a header everything works but I don't understand why.
<cfhttpparam
type="header"
name="cookie"
value="rtFa=#rtFa#;FedAuth=#FedAuth#"
/>
Works but
<cfhttpparam
type="cookie"
name="rtFa"
value="#rtFa#"
/>
<cfhttpparam
type="cookie"
name="FedAuth"
value="#FedAuth#"
/>
Fails
The best way to see what is happening would be to inspect the traffic and see what is happening. Other than that, I know that the difference between using the header type and the cookie type for the cfhttpparam tag is URL encoding.
When you use the header type the value is not URL encoded.
When you use the cookie type the value is URL encoded.
So my guess would be that their API does not like when the value is URL encoded.
Document reference for cfhttpparam attributes.

Error 400: Bad request while fetching json data from instagram api via coldfusion

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.. :)

POST JSON ignoring content-type

Using ColdFusion, I am trying to POST JSON to an API. Here is the code I have so far -
<cfhttp url="#url#" method="post" result="httpResp" timeout="60">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#serializeJSON(jsonStr)#">
</cfhttp>
An example of the JSON is here -
{
"booking":{
"username" : "#username#",
"password" : "#password#",
"customerEmail" : "#customer_email_address#",
"firstName" : "#customer_firstname#",
"lastName" : "#customer_surname#",
"telephoneNumber" : "#customer_mobile_number#",
"guestNumber" : #url.guests#,
"unitNumber" : #url.location#,
"eventDate" : "#LSDateFormat(url.when,'dd/mm/yyyy')#"
}
}
When I pass this JSON to the API URL with POSTMAN client in Chrome, everything is good! however when I process this in CF, I simply get a bad request error from the API. I realise that message is no use that is simple whats being set in the API.
If I remove the content-type from POSTMAN client in Chrome, I get the same message. So I am "assuming" That the content-type is not being sent or over written somehow in CF.
Can anyone point me in the right direction?
Thanks
Issue was with the API not accepting what I thought it required.

How to create a new CouchDB User without Futon or Curl?

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).