-
Notifications
You must be signed in to change notification settings - Fork 3
Switch to Google API client #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mbirman
wants to merge
10
commits into
caffo:master
Choose a base branch
from
mbirman:google-api-client
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d230864
Update gems
mbirman a25753a
Install google-api-client
mbirman 8d0305a
Add Google client id and secret to Config
mbirman a1184a5
Implement Authorizer
mbirman 5260019
Rewrite Digest using the API client
mbirman a4a8af5
Use the API client in Inbox
mbirman e3b523e
Make it work with multiple accounts
mbirman da8d7d8
Remove password from Account
mbirman 1458404
Refactor Inbox and Digest
mbirman 0b4c972
Add a link to credetials wizard
mbirman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,32 @@ | ||
| # encoding: UTF-8 | ||
| require 'gmail' | ||
| require 'google/apis/gmail_v1' | ||
| require 'logger' | ||
| require 'date' | ||
| require 'mail' | ||
| require 'digest/sha1' | ||
| require 'aws/s3' | ||
| require 'yaml' | ||
| require 'singleton' | ||
| require 'baconmail/object' | ||
| require 'baconmail/service' | ||
| require 'baconmail/account' | ||
| require 'baconmail/authorizer' | ||
| require 'baconmail/config' | ||
| require 'baconmail/settings' | ||
| require 'baconmail/inbox' | ||
| require 'baconmail/digest' | ||
| require 'baconmail/digest' | ||
|
|
||
| module Baconmail | ||
| Gmail = Google::Apis::GmailV1 | ||
|
|
||
| def self.logger | ||
| @logger ||= Logger.new(STDOUT) | ||
| end | ||
|
|
||
| def self.authorized_gmail(username) | ||
| Gmail::GmailService.new.tap do |g| | ||
| g.authorization = Authorizer.credentials(username) | ||
| g.client_options.application_name = 'Baconmail' | ||
| end | ||
| end | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # encoding: UTF-8 | ||
| module Baconmail | ||
| Account = Struct.new(:username, :password, :email) | ||
| Account = Struct.new(:username, :email) | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # require 'google/api_client/client_secrets' | ||
| require 'googleauth/stores/file_token_store' | ||
|
|
||
| # encoding: UTF-8 | ||
| module Baconmail | ||
| class Authorizer | ||
| OOB_URI = 'urn:ietf:wg:oauth:2.0:oob' | ||
| SCOPE = Google::Apis::GmailV1::AUTH_SCOPE | ||
|
|
||
| class << self | ||
| def call(user_id) | ||
| raise "Google Client ID or Google Client Secret are invalid." if invalid_keys? | ||
|
|
||
| credentials(user_id) || fetch_credentials(user_id) | ||
| end | ||
|
|
||
| def credentials(user_id) | ||
| authorizer.get_credentials(user_id) | ||
| end | ||
|
|
||
| def fetch_credentials(user_id) | ||
| Baconmail.logger.info "Open the following URL in your browser, log in to #{user_id} and authorize the application." | ||
| Baconmail.logger.info authorization_url | ||
| Baconmail.logger.info "Enter the authorization code:" | ||
|
|
||
| code = STDIN.gets.chomp | ||
|
|
||
| authorizer.get_and_store_credentials_from_code(user_id: user_id, code: code, base_url: OOB_URI) | ||
| end | ||
|
|
||
| def authorizer | ||
| Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store) | ||
| end | ||
|
|
||
| def client_id | ||
| Google::Auth::ClientId.new(config.google_client_id, config.google_client_secret) | ||
| end | ||
|
|
||
| def token_store | ||
| Google::Auth::Stores::FileTokenStore.new(file: Baconmail::Settings::BACONMAIL_CONFIG_PATH) | ||
| end | ||
|
|
||
| def authorization_url | ||
| authorizer.get_authorization_url(base_url: OOB_URI) | ||
| end | ||
|
|
||
| def invalid_keys? | ||
| "#{config.google_client_id}".empty? || "#{config.google_client_secret}".empty? | ||
| end | ||
|
|
||
| def config | ||
| Baconmail::Settings.instance.config | ||
| end | ||
| end | ||
| end | ||
| end | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| # encoding: UTF-8 | ||
| module Baconmail | ||
| Config = Struct.new(:use_preview, :bucket, :aws_key, :aws_secret) | ||
| end | ||
| Config = Struct.new( | ||
| :use_preview, :bucket, :aws_key, :aws_secret, | ||
| :google_client_id, :google_client_secret | ||
| ) | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I try to run
bin/baconmail authorizeI get the following error:I think you are checking if the keys are invalid before we fetch them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@caffo hmm, did you add the keys to
.baconmail? See an example in README: https://github.com/mbirman/baconmail/tree/google-api-client#setup-instructions