Created
February 3, 2012 16:27
-
-
Save gsiener/1730965 to your computer and use it in GitHub Desktop.
Intuit Anywhere sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#<!-- | |
# Copyright Intuit, Inc 2011 | |
# Intuit Anywhere Sample Code | |
# This sample demonstrates how to complete the authentication with an IA application and make IA specific API calls | |
# This sample is for reference purposes only. | |
# | |
# | |
# Copyright (c) 2011 Intuit Inc. All rights reserved. | |
# Redistribution and use in source and binary forms, with or without modification, are permitted in conjunction | |
# with Intuit Partner Platform. | |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | |
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT AND FITNESS FOR A | |
# PARTICULAR PURPOSE ARE DISCLAIMED. | |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
# OF THE USE OF THIS SOFTWARE, WHETHER OR NOT SUCH DAMAGES WERE FORESEEABLE AND EVEN IF THE AUTHOR IS ADVISED | |
# OF THE POSSIBILITY OF SUCH DAMAGES. | |
# --> | |
# Most of the values below you would instead want to persist instead of being in the session | |
# This code depends on the Ruby OAuth gem (http://rubygems.org/gems/oauth) | |
class IntuitController < ApplicationController | |
# This method begins the OAuth flow by getting a request token and redirecting to the authorize URL | |
def connect | |
consumer = Intuit::API.get_consumer("intuit_consumer_key", "intuit_consumer_secret") | |
token = consumer.get_request_token(:oauth_callback => intuit_callback_url) | |
session[:request_token] = token | |
redirect_to Intuit::API.authorize_url(token.token) | |
end | |
# This is routed by Rails as intuit_callback and trades the request token and OAuth verifier for an access token | |
def callback | |
at = session[:request_token].get_access_token(:oauth_verifier => params[:oauth_verifier]) | |
session[:request_token] = nil | |
session[:access_token] = at | |
session[:realm] = params["realmId"] | |
sessions[:data_source] = params["dataSource"] | |
redirect_to '/' | |
end | |
# This is the Menu Proxy that gets called by the javascript SDK | |
def proxy | |
response = intuit_token.get("https://appcenter.intuit.com/api/v1/Account/AppMenu") | |
render :text => response.body, :status => response.code | |
end | |
# This is the disconnect URL that users will come to in order to disconnect, and it calls the Intuit API to disconnect | |
def disconnect | |
if(intuit_token) | |
intuit_token.get("https://appcenter.intuit.com/api/v1/Connection/Disconnect") | |
session[:access_token] = nil | |
end | |
redirect_to '/' | |
end | |
private | |
# Shortcut to return the access token which is stored in the session | |
def intuit_token | |
return session[:access_token] | |
end | |
end | |
module Intuit | |
module API | |
# This returns a Ruby OAuth consumer object initialized with the correct Intuit endpoints | |
def self.get_consumer(key, secret) | |
@consumer = OAuth::Consumer.new(key, secret, { | |
:site => "https://oauth.intuit.com", | |
:request_token_path => "/oauth/v1/get_request_token", | |
:authorize_path => "/oauth/v1/get_access_token", | |
:access_token_path => "/oauth/v1/get_access_token" | |
}) | |
end | |
# This returns the URL the application needs to redirect to for authorization, given a request token | |
def self.authorize_url(token, returl = "") | |
return "https://appcenter.intuit.com/Connect/Begin?oauth_token=#{token}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment