Skip to content

Instantly share code, notes, and snippets.

@msepcot
Created October 24, 2013 00:16
Show Gist options
  • Save msepcot/7129101 to your computer and use it in GitHub Desktop.
Save msepcot/7129101 to your computer and use it in GitHub Desktop.
require 'dfp_api'
require 'httparty'
require 'oauth2'
CLIENT_ID = 'MY CLIENT ID'
CLIENT_SECRET = 'MY CLIENT SECRET'
NETWORK_CODE = 'MY NETWORK CODE'
dfp = DfpApi::Api.new({
authentication: {
method: OAuth2,
oauth2_client_id: CLIENT_ID,
oauth2_client_secret: CLIENT_SECRET,
application_name: 'MY APPLICATION NAME',
network_code: NETWORK_CODE
},
service: { environment: 'PRODUCTION' },
connection: { enable_gzip: true },
library: { log_level: 'INFO' }
})
# First Pass, Force Authorization
authorization_token = dfp.authorize({ oauth_callback: 'oob' }) do |oauth_url|
puts "Hit Auth error, please navigate to URL:\n\t%s" % url
print 'Log in and type the verification code: '
gets.chomp
end
oauth_token = HTTParty.post('https://accounts.google.com/o/oauth2/token', body: {
code: authorization_token['access_token'],
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
grant_type: 'authorization_code'
})
# => {
# "access_token" => "ACCESS TOKEN",
# "token_type" => "Bearer",
# "expires_in" => 3600,
# "refresh_token" => "REFRESH TOKEN"
# }
puts "REFRESH TOKEN (KEEP THIS AROUND): #{oauth_token['refresh_token']}"
# Second Pass, Refresh Authorization
oauth_token = HTTParty.post('https://accounts.google.com/o/oauth2/token', body: {
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
refresh_token: REFRESH_TOKEN,
grant_type: 'refresh_token'
})
dfp.authorize({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
oauth2_token: oauth_token.to_hash,
refresh_token: REFRESH_TOKEN,
grant_type: 'refresh_token'
})
# Proceed with API def calls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment