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.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. APP_NAME = 'CrossPost'
  23. APP_URL = 'https://git.imirhil.fr/aeris/cross-post/'
  24. REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
  25. SCOPES = 'read'
  26. client_id, client_secret = unless settings['mastodon.consumer']
  27. puts 'Creating new app'
  28. token = SecureRandom.hex 64
  29. client = Mastodon::REST::Client.new base_url: url, bearer_token: token
  30. app = client.create_app APP_NAME, REDIRECT_URI, SCOPES, APP_URL
  31. settings['mastodon.consumer.key'] = app.client_id
  32. settings['mastodon.consumer.secret'] = app.client_secret
  33. [app.client_id, app.client_secret]
  34. else
  35. [settings['mastodon.consumer.key'], settings['mastodon.consumer.secret']]
  36. end
  37. client = OAuth2::Client.new client_id, client_secret, site: url
  38. url = client.auth_code.authorize_url redirect_uri: REDIRECT_URI
  39. puts url
  40. begin
  41. Launchy.open url
  42. rescue
  43. end
  44. print 'Code? '
  45. code = gets.chomp
  46. token = client.auth_code.get_token code, scopes: SCOPES, redirect_uri: REDIRECT_URI
  47. token = token.token
  48. puts "Token: #{token}"
  49. settings['mastodon.token'] = token
  50. settings.save