|
<cfcomponent output="false" displayname="Facebook Graph API Component" hint="Interact with the Facebook Graph API using ColdFusion"> |
|
|
|
<!--- use this to request an auth token ---> |
|
<cffunction name="getAuthToken" returnType="struct" access="public" output="false" displayname="Request an authorisation token"> |
|
|
|
<!--- arguments ---> |
|
<cfargument name="client_id" type="string" required="false" default="#application.stFacebook.client_id#" /> |
|
<cfargument name="app_secret" type="string" required="false" default="#application.stFacebook.app_secret#" /> |
|
<cfargument name="redirect_uri" type="string" required="false" default="#application.stFacebook.redirect_uri#" /> |
|
<cfargument name="code" type="string" required="true" /> |
|
|
|
<!--- local variables ---> |
|
<cfset var stGraph = StructNew() /> |
|
<cfset var stReturn = StructNew() /> |
|
|
|
<cftry> |
|
<cfhttp url="https://graph.facebook.com/oauth/access_token" method="get" timeout="30" result="stGraph"> |
|
<cfhttpparam type="url" name="client_id" value="#arguments.client_id#" /> |
|
<cfhttpparam type="url" name="client_secret" value="#arguments.app_secret#" /> |
|
<cfhttpparam type="url" name="redirect_uri" value="#trim(arguments.redirect_uri)#" /> |
|
<cfhttpparam type="url" name="code" value="#trim(arguments.code)#" /> |
|
</cfhttp> |
|
<cfcatch type="any"> |
|
<cfset stReturn = cfcatch /> |
|
</cfcatch> |
|
</cftry> |
|
|
|
<cfif structcount(stGraph)> |
|
|
|
<cfset stReturn.access_token = rereplace(stGraph.filecontent, 'access_token=([a-zA-Z0-9]+).+', "\1") /> |
|
<cfset stReturn.token_expires = rereplace(stGraph.filecontent, '.+?expires=([0-9]+)', "\1") /> |
|
|
|
</cfif> |
|
|
|
<cfreturn stReturn /> |
|
|
|
</cffunction> |
|
|
|
<!--- Use this to get information about the current user ---> |
|
<cffunction name="getCurrentUser" returntype="struct" access="public" output="false" displayname="Get Current User" hint="Used in the authentication and app approval workflow, to get information about the user who just authorised an application"> |
|
|
|
<!--- arguments ---> |
|
<cfargument name="access_token" type="string" required="true" /> |
|
|
|
<!--- local variables ---> |
|
<cfset var stGraphRequest = StructNew() /> |
|
<cfset var stReturn = StructNew() /> |
|
|
|
<!--- attempt to post against the Graph API, catch coldfusion errors and return them if they occur ---> |
|
<cftry> |
|
<cfhttp url="https://graph.facebook.com/me" method="get" timeout="30" result="stGraphRequest"> |
|
<cfhttpparam type="url" name="access_token" value="#arguments.access_token#" /> |
|
</cfhttp> |
|
<cfcatch type="any"> |
|
<cfset stReturn.error = StructNew() /> |
|
<cfset stReturn.error.type = 'ColdFusion' /> |
|
<cfset stReturn.error.message = 'There was an error trying to retrieve the information from Facebook.' /> |
|
</cfcatch> |
|
</cftry> |
|
|
|
<!--- if we have some file content, let's deserialize the JSON and return it ---> |
|
<cfif structKeyExists(stGraphRequest, 'filecontent')> |
|
<cfset stReturn = DeserializeJSON(stGraphRequest.filecontent) /> |
|
</cfif> |
|
|
|
<cfreturn stReturn /> |
|
|
|
</cffunction> |
|
|
|
<!--- Use this to post against the graph API ---> |
|
<cffunction name="graphCreate" returntype="struct" access="public" output="false" displayname="Create an entry in the Graph" hint="Use the following to create an entry in the Facebook graph"> |
|
|
|
<!--- arguments ---> |
|
<cfargument name="object" type="string" required="true" /> |
|
<cfargument name="graphId" type="string" required="true" /> |
|
<cfargument name="graphArguments" type="struct" required="false" default="#StructNew()#" /> |
|
|
|
<!--- local variables ---> |
|
<cfset var stReturn = StructNew() /> |
|
|
|
<!--- attempt to post against the Graph API, catch errors and return them if they occur ---> |
|
<cftry> |
|
<cfhttp url="https://graph.facebook.com/#arguments.graphId#/#arguments.object#" method="post" timeout="30" result="stReturn"> |
|
<cfloop collection="#arguments.graphArguments#" item="item"> |
|
<cfhttpparam type="formField" name="#item#" value="#arguments.graphArguments[item]#"> |
|
</cfloop> |
|
</cfhttp> |
|
<cfcatch type="any"> |
|
<cfset stReturn = cfcatch /> |
|
</cfcatch> |
|
</cftry> |
|
|
|
<cfreturn stReturn /> |
|
|
|
</cffunction> |
|
|
|
</cfcomponent> |
Hi - attempting to use your code, which looks great. Hope you can help.
I have entered the 'client_id' (called app id on the app config page), the app secret, a redirect uri (starting with http://localhost) and the requested permissions into the struct in Application.cfc.
When running index.cfm i get this error message:
Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains.
Why?
Can it be because I am using localhost in the site url and redirect url, or that I haven’t entered anything in the field for App domain on the app config. page?