Wednesday, October 27, 2010

CFHTTP "Could Not Determine MIME Type of File" Error with FacebookGraph API

I'm finishing up the Facebook and Twitter integration for the OpenCF Summit application we'll be launching soon, and I was running into an error when calling the Facebook Graph API via CFHTTP.

If you've worked with Facebook you're probably aware that once a user logs into Facebook via your application, a cookie gets set that contains an access token you can use to grab additional details about the Facebook user via the Graph API. You make a simple HTTP call to http://graph.facebook.com/me?access_token and you get JSON back containing the details about the user that they have authorized you to retrieve.

The first issue I ran into is if the access token contains a pipe you may get a "Failed to set URL:Invalid query" error (and thanks to Andy Wu for pointing out to me that was the issue!). Simple enough to fix by putting the access token into a CFHTTPPARAM tag:

<cfhttp url="https://graph.facebook.com/me">
    <cfhttpparam type="url" name="access_token" value="cookie_value_here" />
</cfhttp>



That fixed the invalid query error, but then the response I was getting back had a filecontent of "Connection failed." It worked fine in my browser so I wasn't sure what was going on, but the mimetype of the response was "Unable to determine MIME type of file" so that gave me a bit of a clue.

CFHTTPPARAM to the rescue again. Setting a MIMETYPE header with a value of text/javscript did the trick:

<cfhttp url="https://graph.facebook.com/me">
    <cfhttpparam type="url" name="access_token" value="cookie_value_here" />
    <cfhttpparam type="header" name="mimetype" value="text/javascript" />
</cfhttp>


Hope that saves someone else a bit of time if they run into a similar issue.

2 comments:

jeffglad said...

Matt, FYI there is a sporadically updated open source project on RiaForge: http://facebookgraph.riaforge.org/ that you might find helpful

Matthew Woodward said...

Thanks--I'll check that out. I like digging into stuff myself to in order to learn it ;-), but I'll take a look!