Created
January 13, 2015 20:23
-
-
Save mikegrassotti/db59f8862277d55b3adb to your computer and use it in GitHub Desktop.
Use google refresh token to create a contextio account
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
# gem 'contextio' | |
# gem 'httparty' | |
# gem 'gmail_xoauth' | |
require 'gmail_xoauth' | |
require 'contextio' | |
require 'httparty' | |
ctxio_key = ENV['CTXIO_KEY'] | |
ctxio_secret = ENV['CTXIO_SECRET'] | |
client_id = ENV['GOOGLE_CLIENT_ID'] | |
client_secret = ENV['GOOGLE_CLIENT_SECRET'] | |
if ctxio_key.nil? || ctxio_secret.nil? || client_id.nil? || client_secret.nil? | |
puts "MISSING ENV VARIABLES, PLEASE DEFINE CTXIO_KEY" | |
exit | |
end | |
if ctxio_secret.nil? | |
puts "MISSING ENV VARIABLES, PLEASE DEFINE CTXIO_SECRET" | |
exit | |
end | |
if client_id.nil? | |
puts "MISSING ENV VARIABLES, PLEASE DEFINE GOOGLE_CLIENT_ID" | |
exit | |
end | |
if client_secret.nil? | |
puts "MISSING ENV VARIABLES, PLEASE DEFINE GOOGLE_CLIENT_SECRET" | |
exit | |
end | |
email = ARGV[0] | |
refresh_token = ARGV[1] | |
if email.nil? || refresh_token.nil? | |
puts "mail.rb <email> <refresh_token>" | |
puts "EMAIL AND REFRESH TOKEN ARE REQUIRED. TO GET TOKEN RUN THIS COMMAND:" | |
puts "python oauth2.py --generate_oauth2_token --client_id=#{client_id} --client_secret=#{client_secret}" | |
exit | |
end | |
def fetch_token cid, secret, refresh | |
options = { | |
body: { | |
client_id: cid, | |
client_secret: secret, | |
refresh_token: refresh, | |
grant_type: 'refresh_token' | |
}, | |
headers: { | |
'Content-Type' => 'application/x-www-form-urlencoded' | |
} | |
} | |
response = HTTParty.post('https://accounts.google.com/o/oauth2/token', options) | |
if response.code == 200 | |
return response.parsed_response['access_token'] | |
else | |
puts("ERROR: Unable to refresh google_oauth2 authentication token.") | |
puts("ERROR: Refresh token response body: #{response.body}") | |
end | |
end | |
puts "USING REFRESH TOKEN TO FETCH AN ACCESS TOKEN..." | |
access_token = fetch_token(client_id, client_secret, refresh_token) | |
puts ". access token is: #{access_token}" | |
puts "TESTING IMAP CONNECTION VIA gmail_xoauth..." | |
imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false) | |
imap.authenticate('XOAUTH2', email, access_token) | |
messages_count = imap.status('INBOX', ['MESSAGES'])['MESSAGES'] | |
puts ". found #{messages_count} messages in INBOX" | |
puts "CHECKING FOR AN EXISTING CTXIO ACCOUNT WITH EMAIL #{email}" | |
contextio = ContextIO.new(ctxio_key, ctxio_secret); | |
account = contextio.accounts.where(email: email).first | |
if !account.nil? | |
puts ". found ctxio account #{account.id}" | |
puts account.api_attributes | |
else | |
puts ". account was not found, creating one..." | |
account = contextio.accounts.create(email: email, first_name: 'test') | |
puts ". created account #{account.id}" | |
puts account.api_attributes | |
puts "ADDING SOURCE..." | |
server = 'imap.gmail.com' | |
ssl = 1 | |
port = 993 | |
type = 'IMAP' | |
options = { | |
provider_refresh_token: refresh_token, | |
provider_consumer_key: client_id | |
} | |
source = account.sources.create(email, server, email, ssl, port, type, options) | |
puts ". created source #{source.id}" | |
puts source.api_attributes | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment