Last active
August 29, 2015 14:08
-
-
Save Mumakil/1d184a3f06bcd087c5e2 to your computer and use it in GitHub Desktop.
Generate Flowdock oauth token
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
#!/usr/bin/env ruby | |
begin | |
require 'oauth2' | |
rescue LoadError | |
puts "You need to install oauth2 gem, run `gem install oauth2`" | |
exit 1 | |
end | |
require 'uri' | |
require 'net/http' | |
redirect_uri = 'urn:ietf:wg:oauth:2.0:oob' | |
$site = ENV['FLOWDOCK_API_URL'] || "https://api.flowdock.com" | |
$email = nil | |
$password = nil | |
def ask_credentials | |
return if $email | |
puts "\nPlease input your email address\n" | |
$email = gets.chomp | |
puts "Please input your password\n" | |
$password = gets.chomp | |
end | |
if ENV['FLOWDOCK_OAUTH_APP_ID'] && ENV['FLOWDOCK_OAUTH_APP_SECRET'] | |
client_id = ENV['FLOWDOCK_OAUTH_APP_ID'] | |
client_secret = ENV['FLOWDOCK_OAUTH_APP_SECRET'] | |
puts "Using preconfigured OAuth app #{client_id}." | |
else | |
puts "First, let's setup an OAuth app." | |
puts "\nYou can skip this by adding\n FLOWDOCK_OAUTH_APP_ID\n FLOWDOCK_OAUTH_APP_SECRET\nto your environment variables." | |
ask_credentials | |
puts "\nPlease input a name for your app\n" | |
app_name = gets.chomp | |
url = URI("#{$site}/oauth/applications") | |
response = Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == 'https') do |http| | |
req = Net::HTTP::Post.new(url.path) | |
req.body = JSON.dump(application: {name: app_name, redirect_uri: redirect_uri}) | |
req.basic_auth $email, $password | |
req['Accept'] = 'application/json' | |
req['Content-Type'] = 'application/json' | |
http.request(req) | |
end | |
if response.code.to_i < 400 | |
app_data = JSON.parse(response.body) | |
else | |
puts "Failed creating app with code #{response.code}:" | |
puts response.body.inspect | |
exit 1 | |
end | |
puts "\nCreated OAuth app:" | |
puts "Application id: #{app_data['client_id']}" | |
puts "Application secret: #{app_data['client_secret']}" | |
client_id = app_data['client_id'] | |
client_secret = app_data['client_secret'] | |
end | |
ask_credentials | |
puts "\nCreating an access token. Continue?\n" | |
gets | |
client = OAuth2::Client.new(client_id, client_secret, :site => $site) | |
token = client.password.get_token($email, $password, redirect_uri: redirect_uri, scope: 'flow integration') | |
puts "\nYour OAuth token is:\n====================\n#{token.token}\n====================" | |
puts "\n\nNow fetching your flows" | |
response = token.get('/flows.json') | |
flows = JSON.parse(response.body).select { |flow| flow['open'] } | |
puts 'Flows you have open: ' | |
flows.each_with_index do |flow, index| | |
puts "#{index + 1}. #{flow['name']} (#{flow['organization']['name']})" | |
end | |
puts "\nNext we'll create a source in each flow." | |
puts "Please input a comma separated list of flows you want to add (1,3,5) or hit enter to create a source in every flow.\n" | |
input = gets.chomp | |
flows_to_add = if input == '' | |
flows | |
else | |
input.split(',').map(&:chomp).map(&:to_i).map { |i| flows[i-1] } | |
end | |
puts "Selected #{flows_to_add.size} flows." | |
puts "Name for the source:\n" | |
source_name = gets.chomp | |
puts "\nCreating sources:" | |
flows_to_add.each do |flow| | |
response = token.post("#{flow["url"]}/sources", body: {name: source_name}) | |
data = JSON.parse(response.body) | |
puts "Flow #{flow['name']} (#{flow['organization']['name']}) with id #{flow['id']}:" | |
puts "Source { id: #{data['id']}, name: '#{data['name']}', flow_token: '#{data['flow_token']}' }" | |
end |
I did this yesterday, so I don't know what change you fix might have done to the flow. I'll try it now.
OK, I just tested the script as-is above, and it works now. So perhaps I was doing something wrong before, or perhaps your fix has resolved the issue.
Obviously it would be nicer if we didn't have to download a script from a gist in order to be able to generate these keys, but at least these steps work now. :)
Great that it works. And that not needing to use random gists is something that's going to get done eventually! =) We just published this so that we could get the heaven stuff easily out now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was able to eventually work around it by:
Creating the app here: https://www.flowdock.com/oauth/applications
Copying the the Testing - Authorization Link and adding a
scope=flow+integration
query parameterCopying the Auth Token returned from that link
Changing the token block of code to:
Then calling the script with 3 environment variable values collected from the above steps:
That finally got me to the "Now fetching your flows" step and I was able to generate source keys.