Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick script for OAuth2 dancin'
require 'oauth2'
client_id = ''
client_secret = ''
login_server = 'https://test.salesforce.com'
redirect_uri = 'https://login.salesforce.com/services/oauth2/callback'
client = OAuth2::Client.new(
client_id,
client_secret,
site: login_server,
authorize_url: '/services/oauth2/authorize',
token_url: '/services/oauth2/token'
)
# STEP 1
puts "1) Paste this URL into your browser where you are logged in to the relevant account:\n\n"
puts client.auth_code.authorize_url(redirect_uri: redirect_uri)
# STEP 2
puts "\n\n2) Accept the authorization request in your browser."
# STEP 3
puts "\n\n3) Copy the code param from the URL they redirect you to (including the ending '=='), paste it here and hit enter:\n"
code = gets.chomp.strip
access_token_obj = client.auth_code.get_token(code, redirect_uri: redirect_uri, token_method: :post)
# STEP 4
puts "\n\nAccess Token is: #{access_token_obj.token}"
puts "Refresh token is: #{access_token_obj.refresh_token}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.