-
-
Save Mumakil/1d184a3f06bcd087c5e2 to your computer and use it in GitHub Desktop.
#!/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 fixed one issue with app creation. It seems that the script was not getting back the client id and secret on the creation api call. However, I'm pretty sure the problem here wasn't that. From the error messages I'd guess you're using your personal api token instead of email and password? The oauth app/token/etc apis don't work with an api token, you need to use email address and password.
No, I was definitely using my password.
Ok. I couldn't reproduce the errors you got today so I'll need to look into this more on monday. I hoped that the one fix we deployed today would've fixed this.
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 parameter -
Copying the Auth Token returned from that link
-
Changing the token block of code to:
client = OAuth2::Client.new(client_id, client_secret, :site => $site) if ENV['FLOWDOCK_AUTH_CODE'] token = client.auth_code.get_token(ENV['FLOWDOCK_AUTH_CODE'], redirect_uri: redirect_uri, scope: 'flow integration') else ask_credentials puts "\nCreating an access token. Continue?\n" gets token = client.password.get_token($email, $password, redirect_uri: redirect_uri, scope: 'flow integration') end puts "\nYour OAuth token is:\n====================\n#{token.token}\n===================="
-
Then calling the script with 3 environment variable values collected from the above steps:
FLOWDOCK_OAUTH_APP_ID=... FLOWDOCK_OAUTH_APP_SECRET=... FLOWDOCK_AUTH_CODE=... ./flowdock-oauth.rb
That finally got me to the "Now fetching your flows" step and I was able to generate source keys.
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.
Trying to use this and it fails with the following error:
If I create the application via Flowdock's admin interface and supplying the App ID and Secret via env vars, the script fails after "Creating an access token. Continue?":