I have been given the following directions to pull a JSON list of data from a webservice.
GET /criminal_api/1.0/service/requests
HTTP Header: Authorization: Bearer 6EDC52118E164AE659EA2C772F3B9804
The following values in the header Bearer 6EDC52118E164AE659EA2C772F3B9804 are dynamic and will be set using the following content variable
<cfset content = deserializeJSON( {
"access_token": "84F224956C6AB5287038C0209EBAC5AB",
"token_type": "bearer",
"refresh_token": "E48BB9C164FE2125D3BE2CD602E4A692",
"expires_in": 7199,
"scope": "read write"
})>
So I am tried the following:
<cfhttp method="get" url="https://test.mywebsite.com/criminal_api//1.0/service/requests" result="orderList">
<cfhttpparam type="HEADER" name="Authorization" value="#content.token_type# #content.access_token#">
</cfhttp>
But when I check the filecontent instead of getting a JSON list I get: Connection Failure
I have a feeling it is how I am setting the header value I am just not sure what I am doing wrong.
EDIT:
When I added a ":" in between the token type and access token I got a new error:
struct
error -1
error_description Invalid access token: : 82D773278FB69CFBCFB4CB8CEF8AC03D
Obviously it thinks the ":" is part of the access token so it is connecting I am just not sure how to have both values in the value= field so it is read correctly.
Have you tried:
<cfhttp method="get" url="https://test.mywebsite.com/criminal_api//1.0/service/requests" result="orderList" username="#content.token_type#" password="#content.access_token#">
This will produce an authorization header of "Basic Bearer:6EDC52118E164AE659EA2C772F3B9804"
Manually, that would be:
<cfhttp method="get" url="https://test.mywebsite.com/criminal_api//1.0/service/requests" result="orderList">
<cfhttpparam type="HEADER" name="Authorization" value="Basic #content.token_type#:#content.access_token#">
There's also the question of what that hex value contains. Take a look at Getting Basic Authentication to work with ColdFusion - maybe this more closely reflects your situation.
Related
There does not exist an official Vimeo API SDK for Coldfusion, so I wrote one based on the official PHP code. In the end, we're only interested in what JSON string Vimeo sees when it receives a request anyway, right?
I'm attempting to do a PULL approach, and I receive a video ID, link, status of "processing" etc when I run the script. The video appears in my account online as "Pending". This is the JSON content of my request:
Headers:
POST https://api.vimeo.com/me/videos
{
"Content_Type": "application/json",
"Authorization": "Bearer 7b8686f6d7cb....65990",
"Accept": "application/vnd.vimeo.*+json; version=3.4"
}
body:
{
"upload": {
"approach":"pull",
"size":30003213,
"link":"https://mysite.me/api/index.cfm/video?PK=Na6z6ZZMQ&SI=45rtt4423"},
"name":"Employee1.mp4"
}
}
The response I receive back from Vimeo includes the following data (obviously this is not the entire response):
{
"Statuscode": "201 Created",
"Filecontent": {
"uri":"/videos/3...393",
"name":"Untitled",
"description":null,
"link":"https://vimeo.com/3...393"
},
"app":{
"name":"My Vimeo App Name",
"uri":"/apps/14...6"
},
"status":"uploading",
"resource_key":"0b83....d49dc",
"upload":{
"status":"in_progress",
"complete_uri":null,
"approach":"post",
"size":null,
"redirect_url":null,
"link":null
},
"transcode":{"status":"in_progress"}
}
I can't seem to get Vimeo to recognize this as a "pull" approach, nor recognize the file name, size, etc. It appears that the request is successful, but the video in "My Videos" on Vimeo never completes uploading or transcoding, has no name, doesn't honor my settings for privacy or other options, and seems to be some sort of processing error.
I'll be glad to share some ColdFusion code with any experienced with it, but I feel like the issue probably lay with the compiled JSON rather than ColdFusion.
Solution:
when sending the requestes via ColdFusion, instead of sending the JSON content like this:
<cfhttpparam type="body" value="{"upload":{"approach":"pull","size":30003213,"link":"https://example.com/api/index.cfm/video?PK=Na6z6Zp4ca&CK=4EP56DM566&SI=6868"},"name":"EmployeeProfile.mp4"}" />
the parameters should be sent like this:
<cfhttpparam type="formField" encoded="false" name="upload.approach" value="pull" />
<cfhttpparam type="formField" encoded="false" name="upload.size" value="30003213" />
<cfhttpparam type="formField" encoded="false" name="upload.link" value="https://example.com/api/index.cfm/video?PK=Na6z6Zp4ca&CK=4EP56DM566&SI=6868" />
Not sure why, but when you send a POST request to Vimeo from ColdFusion, Vimeo does not recognize any of the JSON body of the cfhttpparam. You must use type="formField".
I want to sent push messages with Firebase Cloud Messaging. Everything is working, except for one thing. I want to save the response (see below) from Firebase to update a user profile in the database. So let's say the response gives back a failure, I want to sent that response back to my database.
To sent a push message I use this script:
var key = 'my-key';
var to = 'to-key';
var notification = {
'title': 'Portugal vs. Denmark',
'body': '5 to 1',
'icon': 'firebase-logo.png',
'click_action': 'http://localhost:8081'
};
fetch('https://fcm.googleapis.com/fcm/send', {
'method': 'POST',
'headers': {
'Authorization': 'key=' + key,
'Content-Type': 'application/json'
},
'body': JSON.stringify({
'notification': notification,
'to': to
})
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.error(error);
})
The response I get back from the Firebase is:
My question is how can I save (or sent) that response to my coldfusion server. I was thinking of re-writing the script to coldfusion like:
<cfscript>
objResponse = {
'message':{
'to':'SOME_TOKEN',
'notification':{
'title': 'Portugal vs. Denmark',
'body': '5 to 1',
'icon': 'firebase-logo.png',
'click_action': 'localhost:8081'
}
}
}
</cfscript>
<Cfdump var="#objResponse#" >
<cfoutput >#SerializeJSON(objResponse)#</cfoutput>
<cfhttp url="https://fcm.googleapis.com/fcm/send" method="post" result="objGet">
<cfhttpparam type="header" name="Accept" value="application/json" />
<cfhttpparam type="header" name="Authorization" value="key=MY_KEY">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value='#SerializeJSON(objResponse)#'/>
</cfhttp>
But that is giving me a 400 bad request:
On https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream I found
Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields (for instance, passing a string where a number was expected). The exact failure reason is described in the response and the problem should be addressed before the request can be retried.
So I understand it has something to do with the JSON request i'm sending, but I can't figure out what the problem is.
Since you are making this request from javascript in your browser, you would need to add some code in the .then() callback that would make an ajax post request to your ColdFusion server, sending the data you want to save.
Not sure what your flow is here, but you could also make the http request from the ColdFusion server itself.
I am calling an API and need to send it a JSON string with credentials. We are currently transitioning from CF9 to CF2016. In DEVL I have both versions. In Test and Prod I currently have CF9 only. Originally I wrote the code and tested on CF2016 and it worked fine. When I pushed it up to Test, it did not work. I retried in DEVL, on CF9, and it also errors. The code is:
<cfset logininfo = {"username": "eistech", "password": "#sat_pw#"}>
<cfset fromdate=dateformat(DateAdd('d', -1, dat), "yyyy-MM-dd") & 'T00:00:00-0500'>
<!--- Get token info--->
<cfhttp url="https://scoresdownload.collegeboard.org/pascoredwnld/files/list?fromDate=#fromdate#" method="post" result="finfo">
<cfhttpparam name="Content-Type" type="HEADER" value="application/json">
<cfhttpparam name="Accept" type="HEADER" value="application/json">
<cfhttpparam type="body" value="#serializeJSON(logininfo)#">
</cfhttp>
When running it in CF9, I get:
Invalid CFML construct found on line 5 at column 20. ColdFusion was
looking at the following text:
{ (Line 20 is <cfset logininfo =
{"username": "eistech", "password": "#sat_pw#"}>
I tried enclosing it in single quotes, but this fails in both instances. How can I get this to work in both CF2016 and CF9?
CF9 does not understand : as used in the JSON string in the question.
Use =!
<cfset logininfo = {"username"= "eistech", "password"= "#sat_pw#"}>
I'm attempting to call the smartsheet.com api and read the JSON data from a list sheet request. Im new to API's so I'm certain I'm missing much with my code.
Here is what I have so far:
<cfscript>
apiURL = "https://api.smartsheet.com/2.0/sheets";
apiToken = "xxxxxxxxxxxxxxxxxxxxxxxxx";
</cfscript>
<cfhttp url="#apiURL#" method="GET" result="httpResp" timeout="120" charset="utf-8">
<cfhttpparam type="header" name="Authorization" value="Bearer #apiToken#" />
</cfhttp>
However I do not receive the desired response:
I was attempting to use the sample provided to retrieve the data:
SmartSheet API 2.0
Example Request:
curl https://api.smartsheet.com/2.0/sheets -H "Authorization: Bearer ACCESS_TOKEN"
Example Response:
{
"pageNumber":1,
"pageSize":100,
"totalPages":1,
"totalCount":2,
"data":[
{
"accessLevel":"OWNER",
"id":4583173393803140,
"name":"sheet 1",
"createdAt":"2015-06-05T20:05:29Z",
"modifiedAt":"2015-06-05T20:05:43Z"
},
{
"accessLevel":"OWNER",
"id":2331373580117892,
"name":"sheet 2",
"createdAt":"2015-06-05T20:05:29Z",
"modifiedAt":"2015-06-05T20:05:43Z"
}
]
}
What version of CF? Looks similar to this issue: ColdFusion 9.0.1 - 3574332 CHTTP returns filecontent as java.io.ByteArrayOutputStream when mimetype is application/json. The workaround is to either:
Set the CFHTTP attribute getasbinary="never" OR
Convert the returned fileContent object into a string using:
<cfset rawJSONString = httpResp.fileContent.toString()>
I'm having some problems using the API OpenPay.mx (payments Mexican Banks & other payments services). This API returns JSON. I'm try to access the API with CFHTTP, but it is returning an HTTP 400 Incorrect request error.
Complete error:
https://sandbox-api.openpay.mx/v1/maidzkihk7utcvzhucwk/charges
{"category":"request","description":"Unexpected character ('m' (code
109)): expected a valid value (number, String, array, object, 'true',
'false' or
'null')","http_code":400,"error_code":1001,"request_id":"79a19194-61a2-49e5-8cbc-c83f5c93ce69"}
In the error list from the API help about errors 1001-400, this is the explanation:
The request format is JSON, the fields do not have the correct format,
or the request does not have fields that are required.
ColdFusion Code:
<cfset request_id = "sk_722a9645ea0040899ccd1f0a53dfcf53">
<cfset method="store">
<cfset amount=100>
<cfset description="Cargo con tienda">
<cfset customer="Gabriel Villafuerte">
<cfhttp url="https://sandbox-api.openpay.mx/v1/maidzkihk7utcvzhucwk/charges"
method="post" charset="utf-8" username="#request_id#" password=""
throwonerror="no">
<cfhttpparam type="header" name="Content-Type" value="application/json"/>
<cfhttpparam name="method" type="formfield" value="#method#">
<cfhttpparam name="amount" type="FormField" value="#amount#">
<cfhttpparam name="description" type="FormField" value="#description#">
<cfhttpparam name="customer" type="FormField" value="#customer#">
</cfhttp>
<CFDUMP var="#cfhttp#">
<!---display results--->
<cfoutput>
HTTP Response = #cfhttp.statusCode# <br>
<textarea cols=80 rows=10>
https://sandbox-api.openpay.mx/v1/maidzkihk7utcvzhucwk/charges
#cfhttp.fileContent#
</textarea>
</cfoutput>
These are the rules for the access OpenPay.mx API:
Does anyone have a clue about how I can give the correct format of the fields?
I am not familiar with the API, but looking over the documentation and examples, I suspect the error message means exactly what it says (emphasis mine).
The format of the request is not JSON, the fields do not have the
correct format, OR the request does not have fields that are required.
The request values must be submitted as JSON, not separate fields, and obviously must contain all of the required values. Instead of using "formfield", put the values in a structure. Convert it into a JSON using serializeJSON(). Then pass the JSON to the API using parameter type "body".
You will need to review the API examples, for whichever method you are invoking, to figure out which parameters are required. However, the Charges via Store Example worked with some slight modifications:
The "due_date" cannot be a past date
The "order_id" must be a value not already processed. (I just increased the sample number by an arbitrary amount until I hit a valid id.)
Charges via Store Example
<!--- sample request from API --->
<!--- note: increased "order_id" value by arbitrary amount --->
<cfset timeNow = now()>
<cfset requestData = {
"method" : "store",
"amount" : 100,
"description" : "Cargo con tienda",
"order_id" : "oid-00100",
"due_date" : dateFormat(timeNow, "yyyy-mm-dd")&"T"&timeFormat(timeNow, "HH:nn:ss")
}>
<cfhttp url="https://sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/customers/ag4nktpdzebjiye1tlze/charges"
method="post"
charset="utf-8"
username="sk_e568c42a6c384b7ab02cd47d2e407cab:"
password=""
throwonerror="no">
<cfhttpparam type="header" name="Content-Type" value="application/json"/>
<cfhttpparam type="body" value="#serializeJSON(requestData)#">
</cfhttp>
<cfdump var="#cfhttp#">
Result: