Last active
December 17, 2015 16:19
-
-
Save uu59/5638065 to your computer and use it in GitHub Desktop.
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 | |
# -- coding: utf-8 | |
# https://github.com/uu59/gyaazle | |
require "rubygems" | |
require "grill" | |
Grill.implant <<-G | |
source "https://rubygems.org" | |
gem "httpclient" | |
gem "multi_json" | |
gem "nokogiri" | |
gem "pry" | |
gem "bundler", :require => [ | |
"securerandom" | |
] | |
G | |
CREDENTIAL_FILE = "#{ENV["HOME"]}/.googleimageupload.json" | |
def read_saved_credentials | |
MultiJson.load(File.read(CREDENTIAL_FILE)) | |
end | |
def set_tokens | |
print "client id: " | |
client_id = STDIN.gets.strip | |
print "client secret: " | |
client_secret = STDIN.gets.strip | |
puts | |
puts | |
puts "client_id : #{client_id}" | |
puts "client_secret : #{client_secret}" | |
puts "correct? (Y/n)" | |
return set_tokens if STDIN.gets.strip[/^n/i] | |
[client_id, client_secret] | |
end | |
def init(client_id, client_secret) | |
url = "https://accounts.google.com/o/oauth2/auth?client_id=#{client_id}&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/drive" | |
link = Nokogiri::HTML.parse(HTTPClient.get(url).body).at('a').attributes["href"].to_s | |
puts "Open this link by your browser, and authorize" | |
puts link | |
print "Paste code here: " | |
code = STDIN.gets.strip | |
res = HTTPClient.post("https://accounts.google.com/o/oauth2/token",{ | |
:code => code, | |
:client_id => client_id, | |
:client_secret => client_secret, | |
:redirect_uri => "urn:ietf:wg:oauth:2.0:oob", | |
:grant_type => "authorization_code", | |
}) | |
response = MultiJson.load(res.body) | |
credentials = { | |
:access_token => response["access_token"], | |
:refresh_token => response["refresh_token"], | |
:client_id => client_id, | |
:client_secret => client_secret, | |
:token_type => response["token_type"], | |
} | |
File.open(CREDENTIAL_FILE, "w") do |f| | |
json = MultiJson.dump(credentials) | |
f.write json | |
end | |
read_saved_credentials | |
end | |
def refresh_token(credentials = nil) | |
credentials ||= read_saved_credentials | |
r = HTTPClient.post("https://accounts.google.com/o/oauth2/token", { | |
:refresh_token => credentials["refresh_token"], | |
:client_id => credentials["client_id"], | |
:client_secret => credentials["client_secret"], | |
:grant_type => "refresh_token", | |
}) | |
credentials["access_token"] = MultiJson.load(r.body)["access_token"] | |
credentials | |
end | |
def upload(credentials, file) | |
body = [ | |
{ | |
'Content-Type' => 'application/json;charset=utf-8', | |
:content => MultiJson.dump({ | |
:title => File.basename(file), | |
:shared => "true", | |
}) | |
}, | |
{ | |
:content => File.read(file), | |
}, | |
] | |
r = HTTPClient.post( | |
'https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart', | |
body, | |
{ | |
"Authorization" => "Bearer #{credentials["access_token"]}", | |
"Content-Type" => "multipart/related; boundary=#{SecureRandom.hex(32)}", | |
} | |
) | |
fileobj = MultiJson.load(r.body) | |
puts fileobj["alternateLink"] | |
domain = fileobj["alternateLink"][%r!/a/(.*?)/!, 1] | |
json = MultiJson.dump({ | |
:role => "reader", | |
:type => "#{domain ? "domain" : "anyone"}", | |
:value => "#{domain ? domain : "me"}", | |
:withLink => "true", | |
:additionalRoles => ["commenter"], | |
}) | |
HTTPClient.post_content( | |
"https://www.googleapis.com/drive/v2/files/#{fileobj["id"]}/permissions", | |
json, | |
{ | |
"Authorization" => "Bearer #{credentials["access_token"]}", | |
'Content-Type' => 'application/json;charset=utf-8', | |
} | |
) | |
end | |
if File.exists?(CREDENTIAL_FILE) | |
credentials = refresh_token | |
else | |
client_id, client_secret = set_tokens | |
credentials = { | |
"client_id" => client_id, | |
"client_secret" => client_secret | |
} | |
end | |
unless credentials["refresh_token"] | |
credentials = init(credentials["client_id"], credentials["client_secret"]) | |
end | |
upload(credentials, ARGV.first) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment