Skip to content

Instantly share code, notes, and snippets.

@benmills
Last active March 4, 2016 20:11
Show Gist options
  • Save benmills/0ed1ea5fe0e2096ea3d4 to your computer and use it in GitHub Desktop.
Save benmills/0ed1ea5fe0e2096ea3d4 to your computer and use it in GitHub Desktop.
Simple command line tool to either show your next event and location or open the hangout for your next meeting.
require 'time'
require 'uri'
require 'net/http'
require 'net/https'
require "json"
CLIENT_ID = "google client id"
CLIENT_SECRET = "google client secret"
# Use the bash script to generate a refreh token
REFRESH_TOKEN = "refresh id"
# This is your email or a specific calendar ID
CALENDAR_ID = "some id"
def request(method, url, auth=true, body=nil)
uri = URI.parse(url)
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = method.new(uri)
if body
request.body = URI.encode_www_form(body)
end
if auth
request["Authorization"] = "Bearer #{access_token}"
end
JSON.parse(https.request(request).body)
end
def access_token
result = request(Net::HTTP::Post, "https://www.googleapis.com/oauth2/v4/token", false, {
"client_id" => CLIENT_ID,
"client_secret" => CLIENT_SECRET,
"refresh_token" => REFRESH_TOKEN,
"grant_type" => "refresh_token"
})
if result["access_token"]
result["access_token"]
else
raise "Client id, secret, or refresh token is wrong."
end
end
def get_next_event_for_calendar(id)
result = request(Net::HTTP::Get, "https://www.googleapis.com/calendar/v3/calendars/#{id}/events?timeMin=#{Time.now.to_datetime.rfc3339}")
result["items"].last
end
next_event = get_next_event_for_calendar(CALENDAR_ID)
if ARGV[0] == "h" || ARGV[0] == "hangout"
`open #{next_event["hangoutLink"]}`
else
puts "#{next_event["summary"]} | #{next_event["location"]}"
end
client_id=YOUR-CLIENT-SECRET
client_secret=YOUR-CLIENT-ID
result=$(curl -s "https://accounts.google.com/o/oauth2/device/code" \
-d "client_id=$client_id" \
-d "scope=https://www.googleapis.com/auth/calendar.readonly")
user_code=$(echo $result | sed -n 's/.*user_code" : "\([A-Z\-]*\)",.*/\1/p')
verification_url=$(echo $result | sed -n 's/.*"verification_url" : "\(http.*\)",.*/\1/p')
device_code=$(echo $result | sed -n 's/.*"device_code" : "\(.*\)", "user.*$/\1/p')
echo "Going to open browser at '$verification_url'"
echo "We put the user code in your clipboard. Just paste into the website when loaded"
echo "Just in case, this is the user code: $user_code"
echo $user_code | pbcopy
echo "Press any key to open browser. Come back to terminal when done..."
read
open "$verification_url"
echo "Press any key when you've entered the code..."
read
curl -v https://www.googleapis.com/oauth2/v4/token \
-d "client_id=$client_id" \
-d "client_secret=$client_secret" \
-d "code=$device_code" \
-d "grant_type=http://oauth.net/grant_type/device/1.0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment