In order to use the Constant Contact SDK follow these steps:
A. Rails example to retrieve your list of contacts:
-
Install the gem :
gem install constantcontact
-
Configure Rails to load the gem :
Rails::Initializer.run do |config| ... config.gem "constantcontact" ... end
or add the following in your .Gemfile :
gem 'constantcontact'
-
Create a new action and add the following code, entering your API key, client secret, and redirect URL:
@oauth = ConstantContact::Auth::OAuth2.new( :api_key => 'your api key', :api_secret => 'your secret key', :redirect_url => 'your redirect url' ) @error = params[:error] @user = params[:username] @code = params[:code] if @code response = @oauth.get_access_token(@code) if response token = response['access_token'] cc = ConstantContact::Api.new('your api key') @contacts = cc.get_contacts(token) end end
Note: 'your redirect url' is the URL of the action you just created.
-
Create a view for the above mentioned action with the following code that retrieves your list of contacts:
<% if @error %> <p> <%=@error%> </p> <% end %> <% if @code %> <% if @contacts %> <% for contact in @contacts.results %> <p> Contact name: <%= contact.first_name + contact.last_name %> </p> <% end %> <% end %> <% else %> <a href="<%=@oauth.get_authorization_url%>">Click to authorize</a> <% end %>
-
The first time you access the action in browser you should see the "Click to authorize" link. Follow the link, go through all the Constant Contact steps required, and then you will be redirected back to your action and you should see the list of contacts.
B. Sinatra example to retrieve list of contacts:
-
Install the gem :
gem install constantcontact
-
Add the following code in myapp.rb (just an example): require 'active_support' require 'constantcontact'
get '/my_url' do @oauth = ConstantContact::Auth::OAuth2.new( :api_key => 'your api key', :api_secret => 'your secret key', :redirect_url => 'your redirect url' ) @error = params[:error] @user = params[:username] @code = params[:code] if @code response = @oauth.get_access_token(@code) if response token = response['access_token'] cc = ConstantContact::Api.new('your api key') @contacts = cc.get_contacts(token) end end erb :my_view end
Note: 'your redirect url' is the URL of the route you just created ( get '/my_url' ).
-
Create a my_view.rhtml (or my_view.erb) with the following code:
<% if @error %> <p> <%=@error%> </p> <% end %> <% if @code %> <% if @contacts %> <% for contact in @contacts.results %> <p> Contact name: <%= contact.first_name + contact.last_name %> </p> <% end %> <% end %> <% else %> <a href="<%=@oauth.get_authorization_url%>">Click to authorize</a> <% end %>
-
The first time you access the action in a browser you should see the "Click to authorize" link. Follow the link, go through all the Constant Contact steps required, and then you will be redirected back to your action and you should see the list of contacts.