|
| 1 | +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) |
| 2 | +require 'createsend' |
| 3 | + |
| 4 | +class AuthorizationSample |
| 5 | + def initialize |
| 6 | + raise 'CREATESEND_API_KEY env var missing' if ENV['CREATESEND_API_KEY'].nil? |
| 7 | + raise 'CREATESEND_CLIENT_ID env var missing' if ENV['CREATESEND_CLIENT_ID'].nil? |
| 8 | + raise 'CREATESEND_OAUTH_CLIENT_ID env var missing' if ENV['CREATESEND_OAUTH_CLIENT_ID'].nil? |
| 9 | + raise 'CREATESEND_OAUTH_CLIENT_SECRET env var missing' if ENV['CREATESEND_OAUTH_CLIENT_SECRET'].nil? |
| 10 | + raise 'CREATESEND_OAUTH_REDIRECT_URL env var missing' if ENV['CREATESEND_OAUTH_REDIRECT_URL'].nil? |
| 11 | + raise 'CREATESEND_OAUTH_SCOPE env var missing' if ENV['CREATESEND_OAUTH_SCOPE'].nil? |
| 12 | + |
| 13 | + @createsendApiKey = ENV['CREATESEND_API_KEY'] |
| 14 | + @oauthClientId = ENV['CREATESEND_OAUTH_CLIENT_ID'] |
| 15 | + @oauthClientSecret = ENV['CREATESEND_OAUTH_CLIENT_SECRET'] |
| 16 | + @oauthRedirectUrl = ENV['CREATESEND_OAUTH_REDIRECT_URL'] |
| 17 | + @oauthScope = ENV['CREATESEND_OAUTH_SCOPE'] |
| 18 | + ) |
| 19 | + end |
| 20 | + |
| 21 | + def authentication_with_api_key |
| 22 | + auth = {:api_key => @createsendApiKey} |
| 23 | + @client = CreateSend::Client.new auth, @createsendClientId |
| 24 | + |
| 25 | + @client.scheduled |
| 26 | + end |
| 27 | + |
| 28 | + def get_authorise_url |
| 29 | + state = 'some state data' |
| 30 | + |
| 31 | + @authorize_url = CreateSend::CreateSend.authorize_url(@oauthClientId, @oauthRedirectUrl, @oauthScope, state); |
| 32 | + end |
| 33 | + |
| 34 | + def exchange_token(code) |
| 35 | + CreateSend::CreateSend.exchange_token( |
| 36 | + client_id=@oauthClientId, |
| 37 | + client_secret=@oauthClientSecret, |
| 38 | + redirect_uri=@oauthRedirectUrl, |
| 39 | + code=code # Get the code from the query string after hitting authorise url |
| 40 | + ) |
| 41 | + end |
| 42 | + |
| 43 | + def authentication_with_oauth(access_token, refresh_token) |
| 44 | + auth = {:access_token => access_token, :refresh_token => refresh_token} |
| 45 | + @client = CreateSend::Client.new auth, @createsendClientId |
| 46 | + |
| 47 | + @client.scheduled |
| 48 | + end |
| 49 | +end |
| 50 | + |
| 51 | +sample = AuthorizationSample.new |
| 52 | +authoriseUrl = sample.get_authorise_url |
| 53 | +# hit the authorise url, where you would be redirected and receive the code parameter in the query string |
| 54 | +access_token, expires_in, refresh_token = sample.exchange_token('code that you get once you hit authorize url') |
| 55 | + |
| 56 | +puts "Getting scheduled campaigns with api authentication: #{sample.authentication_with_api_key.to_json}\n\n" |
| 57 | +puts "Getting authorise url: #{authoriseUrl.to_json}\n\n" |
| 58 | +puts "Getting access_token: #{access_token.to_json}\n\n" |
| 59 | +puts "Getting scheduled campaigns with oauth authentication: #{sample.authentication_with_oauth(access_token, refresh_token).to_json}\n\n" |
| 60 | + |
| 61 | + |
| 62 | + @client.scheduled |
| 63 | + end |
| 64 | +end |
0 commit comments