Last active
April 30, 2018 10:03
-
-
Save tumugin/a3ffec7842f53092a2f2d1819d542edb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'bundler/setup' | |
Bundler.require(:default) | |
require 'mastodon' | |
require 'highline/import' | |
require 'oauth2' | |
require 'dotenv' | |
require 'pp' | |
DEFAULT_APP_NAME = "mastodon-cli-sample" | |
DEFAULT_MASTODON_URL = 'https://mstdn.jp' | |
FULL_ACCESS_SCOPES = "read write follow" | |
Dotenv.load | |
if !ENV["MASTODON_URL"] | |
ENV["MASTODON_URL"] = ask("Instance URL: "){|q| q.default = DEFAULT_MASTODON_URL} | |
File.open(".env", "a+") do |f| | |
f.write "MASTODON_URL = '#{ENV["MASTODON_URL"]}'\n" | |
end | |
end | |
scopes = ENV["MASTODON_SCOPES"] || FULL_ACCESS_SCOPES | |
app_name = ENV["MASTODON_APP_NAME"] || DEFAULT_APP_NAME | |
if !ENV["MASTODON_CLIENT_ID"] || !ENV["MASTODON_CLIENT_SECRET"] | |
client = Mastodon::REST::Client.new(base_url: ENV["MASTODON_URL"]) | |
app = client.create_app(app_name, "urn:ietf:wg:oauth:2.0:oob", scopes) | |
ENV["MASTODON_CLIENT_ID"] = app.client_id | |
ENV["MASTODON_CLIENT_SECRET"] = app.client_secret | |
File.open(".env", "a+") do |f| | |
f.write "MASTODON_CLIENT_ID = '#{ENV["MASTODON_CLIENT_ID"]}'\n" | |
f.write "MASTODON_CLIENT_SECRET = '#{ENV["MASTODON_CLIENT_SECRET"]}'\n" | |
end | |
end | |
if !ENV["MASTODON_ACCESS_TOKEN"] | |
client = OAuth2::Client.new(ENV["MASTODON_CLIENT_ID"], | |
ENV["MASTODON_CLIENT_SECRET"], | |
site: ENV["MASTODON_URL"]) | |
print "Open URL below:\n" | |
print client.auth_code.authorize_url(redirect_uri: 'urn:ietf:wg:oauth:2.0:oob') + "\n" | |
code = ask('Your Code:') | |
token = client.auth_code.get_token(code,{redirect_uri:'urn:ietf:wg:oauth:2.0:oob'}) | |
ENV["MASTODON_ACCESS_TOKEN"] = token.token | |
File.open(".env", "a+") do |f| | |
f.write "MASTODON_ACCESS_TOKEN = '#{ENV["MASTODON_ACCESS_TOKEN"]}'\n" | |
end | |
end | |
client = Mastodon::REST::Client.new(base_url: ENV["MASTODON_URL"], | |
bearer_token: ENV["MASTODON_ACCESS_TOKEN"]) | |
## 投稿する | |
message = ARGV[0] || ask("Your Message: ") | |
response = client.create_status(message) | |
# | |
### とりあえず結果を出力してみる | |
pp response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment