Search code examples
curlcoldfusiongoogle-fit-api

CURL Succeeds, but CFHTTP Gets 404 Accessing GoogleFit API Endpoint: /fitness/v1/users/me/dataset:aggregate


This is seriously driving me nuts!!

The exact same request succeeds when I format it as a curl statement, but gets a 404 when using cfhttp to make the request.

Coldfusion

<cfhttp method="POST" result="stepsResponse"
    url="https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate">
    <cfhttpparam type="header" name="Authorization" value="Bearer #ACCESS_TOKEN#">
    <cfhttpparam type="header" name="Content-Type" value="application/json;encoding=utf-8">
    <cfhttpparam type="body" value='{"bucketByTime":{"durationMillis":86400000},"endTimeMillis":1678950000000,"startTimeMillis":1678863600000,"aggregateBy":[{"dataSourceId":"derived:com.google.step_count.delta:com.google.android.gms:estimated_steps","dataTypeName":"com.google.step_count.delta"}]}'>
</cfhttp>

Curl

curl \
-X POST \
-H "Content-Type: application/json;encoding=utf-8" \
-H "Authorization: Bearer {ACCESS_TOKEN}" \
-d '{"bucketByTime":{"durationMillis":86400000},"endTimeMillis":1678950000000,"startTimeMillis":1678863600000,"aggregateBy":[{"dataSourceId":"derived:com.google.step_count.delta:com.google.android.gms:estimated_steps","dataTypeName":"com.google.step_count.delta"}]}' \
https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate

Why? Why? Why?

Strangely, when I call the endpoint directly in the browser I get a 404 as well. I'm fairly certain there is something (easy) that I'm overlooking, but I've been staring at this for hours over the past couple days and cannot see where I'm going wrong. I sincerely hope someone out there sees the problem. I'm grateful for your time!

Google Developer Guide — Read Daily Steps Total

Update: JavaScript Works TOO! Why won't CFHTTP?!

2023-03-18: I've tested this using JavaScript's fetch method as well and it works. No 404. I just don't understand why the same URL fails in CFHTTP?!

fetch('https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate', {
  method: 'POST',
  headers: {
    'Authorization': '#ACCESS_TOKEN#',
    'Content-Type': 'application/json;encoding=utf-8'
  },
  body: '{"bucketByTime":{"durationMillis":86400000},"endTimeMillis":1678950000000,"startTimeMillis":1678863600000,"aggregateBy":[{"dataSourceId":"derived:com.google.step_count.delta:com.google.android.gms:estimated_steps","dataTypeName":"com.google.step_count.delta"}]}'
})
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));

Update: stepsResponse Screenshot

2023-03-22: at the request of @CFMLBread and @TRose, here's a screenshot of the stepsResponse.

enter image description here


Solution

  • I was having this exact same issue with cfhttp today. Turns out the colon in the url part was automatically being encoded, which the google api didn't understand. If you're on Lucee, you can use the 'encodeurl' option - https://docs.lucee.org/reference/tags/http.html . If you're on some other version of coldfusion you could try updating or going around cfhttp entirely and using something like bolthttp https://github.com/foundeo/bolthttp