Reputation: 847
I am writing a ColdFusion (CF) app that needs to access its own Google Analytics (GA) data (not another user's). Some research indicated I want to use a Simple API Key, but then I encountered warnings that this has been deprecated and I should use Oauth 2 with a Service Account.
I found a working CF/Oauth2 demo here, but it's not designed to use a Service Account. Then I found a post on StackOverflow with instructions for Service Account authentication, but the code is in PHP.
I've created a Service Account in GA, registered the application, downloaded the private key, etc. But I don't know how to make it all work with CF. I can find examples online of Service Account authentication, or CF Oauth 2 authentication, but not both.
Can someone provide a working example of ColdFusion authenticating to Google Analytics using Oauth 2 and a Service Account?
Thank you!
Upvotes: 2
Views: 2476
Reputation: 23
Take a look at: https://web.archive.org/web/20160409194848/http://www.jensbits.com/2012/04/05/google-analytics-reporting-api-using-oauth-2-with-coldfusion/
Create Login URL and Retrieve “Code” Parameters for Login URL
Client id and client secret are set by Google when the app is registered for api access in the Google APIs Console.
The redirect uri is a location on the server that the user is sent to after authenticating. This uri is registered in the Google APIs Console during app registration.
These values can be included in the application.cfc:
<cfset request.oauthSettings = {scope = "https://www.googleapis.com/auth/analytics.readonly",
client_id = "YOUR-CLIENT-ID.apps.googleusercontent.com",
client_secret = "YOUR-CLIENT-SECRET",
redirect_uri = "YOUR-REDIRECT-URI",
state = "optional"} />
User Login URL
The login URL will prompt the user for permission to access their Google content via the app and a “code” request variable will be returned in the URL. See Forming the URL for more detailed information.
<!--- create login url --->
<cfset loginURL = "https://accounts.google.com/o/oauth2/auth?scope=" & request.oauthSettings["scope"]
& "&redirect_uri=" & request.oauthSettings["redirect_uri"]
& "&response_type=code&client_id=" & request.oauthSettings["client_id"]
& "&access_type=online" />
Login with Google account that has access to analytics
Upvotes: 0
Reputation: 14435
Install the .jar
files from Google which can be downloaded at Google Analytics API Client Library for Java into the WEB-INF/lib folder of the CF server.
Create the objects in ColdFusion. Here is a single example:
variables.HTTP_Transport = createObject("java", "com.google.api.client.http.javanet.NetHttpTransport").init();
You will then follow the instructions Google has for using the java objects almost exactly.
There are too many lines to list (but it's not too crazy) so you can follow this post: Accessing Google Analytics API with Service Account and Coldfusion
Upvotes: 1