No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mastodon-register 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env ruby
  2. require 'cross-post'
  3. require 'mastodon'
  4. require 'oauth2'
  5. require 'launchy'
  6. require 'uri'
  7. config = CrossPost::Config.new
  8. settings = config[:settings]
  9. url = settings['mastodon.url']
  10. unless url
  11. print 'Mastodon URL ? '
  12. url = gets.chomp
  13. url = "https://#{url}" if URI.parse(url).class == URI::Generic
  14. settings['mastodon.url'] = url
  15. end
  16. user = settings['mastodon.user']
  17. unless user
  18. print 'Mastodon username ? '
  19. user = gets.chomp
  20. settings['mastodon.user'] = user
  21. end
  22. client_id, client_secret = unless settings['mastodon.consumer']
  23. puts 'Creating new app'
  24. token = SecureRandom.hex 64
  25. redirect_url = 'urn:ietf:wg:oauth:2.0:oob'
  26. client = Mastodon::REST::Client.new base_url: url, bearer_token: token
  27. app = client.create_app 'CrossPost', redirect_url,
  28. 'read write', 'https://git.imirhil.fr/aeris/cross-post/'
  29. settings['mastodon.consumer.key'] = app.client_id
  30. settings['mastodon.consumer.secret'] = app.client_secret
  31. [app.client_id, app.client_secret]
  32. else
  33. [settings['mastodon.consumer.key'], settings['mastodon.consumer.secret']]
  34. end
  35. client = OAuth2::Client.new client_id, client_secret, site: url
  36. url = client.auth_code.authorize_url redirect_uri: redirect_url
  37. puts url
  38. begin
  39. Launchy.open url
  40. rescue
  41. end
  42. print 'Token ? '
  43. token = gets.chomp
  44. settings['mastodon.token'] = token
  45. settings.save