Last active
November 3, 2015 17:46
-
-
Save ses4j/6b38cd7bb391e8f41912 to your computer and use it in GitHub Desktop.
Example code for
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
# Sample database table to use to store ExactTarget credentials. | |
create_table "vestorly_marketing_cloud_accounts", force: :cascade do |t| | |
t.string "exacttarget_user_id", limit: 256, null: false | |
t.string "exacttarget_user_email", limit: 256 | |
t.string "exacttarget_internal_oauth_token", limit: 512 | |
t.string "exacttarget_oauth_token", limit: 512 | |
t.string "exacttarget_refresh_token", limit: 512 | |
t.datetime "exacttarget_token_expiration_date" | |
end | |
# Provide functionality to get, store, and refresh ET API tokens for | |
# user-contextualized ExactTarget API access. | |
class Account < ActiveRecord::Base | |
def self.upsert_from_encoded_jwt(encodedJWT) | |
secret = ENV['ET_APP_SIGNATURE'] | |
decodedJWT = JWT.decode(encodedJWT.to_s, secret) | |
fuelsdk_client = FuelSDK::Client.new({'client' => { | |
'id' => ENV['ET_CLIENT_ID'], | |
'secret' => ENV['ET_CLIENT_SECRET'], | |
'signature' => secret | |
}, 'jwt' => encodedJWT }, false) | |
user_id = decodedJWT['request']['user']['id'] | |
email = decodedJWT['request']['user']['email'] | |
fuelsdk_client.refresh | |
a = Account.find_by(exacttarget_user_id: user_id) | |
if a.nil? | |
a = Account.new | |
a.exacttarget_user_id = user_id | |
a.is_active = false | |
a.is_installing = false | |
p "[Account.upsert_from_encoded_jwt] NEW USER! #{user_id} email=#{email}." | |
end | |
a.exacttarget_user_email = email | |
a.store_tokens(fuelsdk_client) | |
p "[Account.upsert_from_encoded_jwt] Updated user data #{user_id} email=#{email}." | |
a.log_token_data("upsert_from_encoded_jwt_end") | |
return a | |
end | |
def log_token_data(why) | |
p "[Account.#{why}] #{exacttarget_user_id}: exp=#{exacttarget_token_expiration_date} token=#{exacttarget_oauth_token} refresh=#{exacttarget_refresh_token}" | |
end | |
def store_tokens(fuelsdk_client) | |
p "[Account.get_fuelsdk_client] refreshed tokens for #{exacttarget_user_id}, new exp=#{fuelsdk_client.auth_token_expiration}, ref=#{fuelsdk_client.refresh_token}..." | |
self.exacttarget_internal_oauth_token = fuelsdk_client.internal_token | |
self.exacttarget_oauth_token = fuelsdk_client.access_token | |
self.exacttarget_refresh_token = fuelsdk_client.refresh_token | |
self.exacttarget_token_expiration_date = fuelsdk_client.auth_token_expiration | |
self.save! | |
end | |
def reload_tokens(fuelsdk_client, failed_response) | |
reload | |
if fuelsdk_client.auth_token_expiration != exacttarget_token_expiration_date | |
fuelsdk_client.internal_token = exacttarget_internal_oauth_token | |
fuelsdk_client.access_token = exacttarget_oauth_token | |
fuelsdk_client.refresh_token = exacttarget_refresh_token | |
fuelsdk_client.auth_token_expiration = exacttarget_token_expiration_date | |
p "[Account.get_fuelsdk_client] reloaded tokens from db for #{exacttarget_user_id}, new exp=#{fuelsdk_client.auth_token_expiration}, ref=#{fuelsdk_client.refresh_token}..." | |
else | |
raise "Unable to refresh token: #{failed_response['message']}" | |
end | |
end | |
def get_fuelsdk_client() | |
# memoize the fuelsdk constructor. | |
return @fuelsdk_client if defined? @fuelsdk_client | |
@fuelsdk_client = begin | |
log_token_data("get_fuelsdk_client_start") | |
raise "Need Client ID in environment" if ENV['ET_CLIENT_ID'].nil? | |
storetoken_callback = self.method(:store_tokens) | |
reloadtoken_callback = self.method(:reload_tokens) | |
fuelsdk_client = FuelSDK::Client.new({'client' => { | |
'id' => ENV['ET_CLIENT_ID'], | |
'secret' => ENV['ET_CLIENT_SECRET'] | |
}, | |
'refresh_callback' => storetoken_callback, | |
'reload_tokens_callback' => reloadtoken_callback }, false) | |
retries = 0 | |
begin | |
fuelsdk_client.internal_token = exacttarget_internal_oauth_token | |
fuelsdk_client.access_token = exacttarget_oauth_token | |
fuelsdk_client.refresh_token = exacttarget_refresh_token | |
fuelsdk_client.auth_token_expiration = exacttarget_token_expiration_date | |
fuelsdk_client.refresh | |
rescue => e | |
p e.message | |
if e.message.include? "Unauthorized" | |
retries += 1 | |
if retries < 5 | |
p "[Account.get_fuelsdk_client] WARN: Unauthorized trying to refresh token for user #{exacttarget_user_id}, retrying... #{retries}/5" | |
sleep(1) # 1 second | |
reload | |
retry | |
else | |
p "[Account.get_fuelsdk_client] ERROR: Giving up. Cannot refresh token, user #{exacttarget_user_id} must relog in to update. exp=#{exacttarget_token_expiration_date}" | |
raise e | |
end | |
else | |
raise e | |
end | |
end | |
log_token_data("get_fuelsdk_client_end") | |
fuelsdk_client | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment