Last active
August 29, 2015 14:23
-
-
Save arshpreetsingh/cd11e1854858dd3bb0d9 to your computer and use it in GitHub Desktop.
Wana get Idea what script is doing just concentrate on Main function. Python-social-auth returns Access_token(y29.xxxxx)oauth2.py made only to wok for auth_token and Email, Basically It generates the auth_string, Which help to IMAP login for Gmail using Oauth2. main idea is got from http://google-mail-oauth2-tools.googlecode.com/svn/trunk/python…
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
| #!/usr/bin/python | |
| # | |
| # Copyright 2012 Google Inc. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| """Performs client tasks for testing IMAP OAuth2 authentication. | |
| To use this script, you'll need to have registered with Google as an OAuth | |
| application and obtained an OAuth client ID and client secret. | |
| See https://developers.google.com/identity/protocols/OAuth2 for instructions on | |
| registering and for documentation of the APIs invoked by this code. | |
| """ | |
| import base64 | |
| import imaplib | |
| import json | |
| from optparse import OptionParser | |
| import smtplib | |
| import sys | |
| import urllib | |
| from datetime import date,timedelta,datetime | |
| # The URL root for accessing Google Accounts. | |
| GOOGLE_ACCOUNTS_BASE_URL = 'https://accounts.google.com' | |
| # Hardcoded dummy redirect URI for non-web apps. | |
| REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob' | |
| def AccountsUrl(command): | |
| """Generates the Google Accounts URL. | |
| Args: | |
| command: The command to execute. | |
| Returns: | |
| A URL for the given command. | |
| """ | |
| return '%s/%s' % (GOOGLE_ACCOUNTS_BASE_URL, command) | |
| def UrlEscape(text): | |
| # See OAUTH 5.1 for a definition of which characters need to be escaped. | |
| return urllib.quote(text, safe='~-._') | |
| def UrlUnescape(text): | |
| # See OAUTH 5.1 for a definition of which characters need to be escaped. | |
| return urllib.unquote(text) | |
| def FormatUrlParams(params): | |
| """Formats parameters into a URL query string. | |
| Args: | |
| params: A key-value map. | |
| Returns: | |
| A URL query string version of the given parameters. | |
| """ | |
| param_fragments = [] | |
| for param in sorted(params.iteritems(), key=lambda x: x[0]): | |
| param_fragments.append('%s=%s' % (param[0], UrlEscape(param[1]))) | |
| return '&'.join(param_fragments) | |
| def GenerateOAuth2String(username, access_token, base64_encode=True): | |
| """Generates an IMAP OAuth2 authentication string. | |
| See https://developers.google.com/google-apps/gmail/oauth2_overview | |
| Args: | |
| username: the username (email address) of the account to authenticate | |
| access_token: An OAuth2 access token. | |
| base64_encode: Whether to base64-encode the output. | |
| Returns: | |
| The SASL argument for the OAuth2 mechanism. | |
| """ | |
| auth_string = 'user=%s\1auth=Bearer %s\1\1' % (username, access_token) | |
| if base64_encode: | |
| auth_string = base64.b64encode(auth_string) | |
| return auth_string | |
| def TestImapAuthentication(user, auth_string): | |
| """Authenticates to IMAP with the given auth_string. | |
| Prints a debug trace of the attempted IMAP connection. | |
| Args: | |
| user: The Gmail username (full email address) | |
| auth_string: A valid OAuth2 string, as returned by GenerateOAuth2String. | |
| Must not be base64-encoded, since imaplib does its own base64-encoding. | |
| """ | |
| imap_conn = imaplib.IMAP4_SSL('imap.gmail.com') | |
| imap_conn.debug = 4 | |
| imap_conn.authenticate('XOAUTH2', lambda x: auth_string) | |
| imap_conn.select('INBOX') | |
| def RequireOptions(options, *args): | |
| missing = [arg for arg in args if getattr(options, arg) is None] | |
| if missing: | |
| print 'Missing options: %s' % ' '.join(missing) | |
| sys.exit(-1) | |
| if __name__ == '__main__': | |
| user = raw_input("user") | |
| access_token = raw_input("token") | |
| TestImapAuthentication(user, | |
| GenerateOAuth2String(user,access_token, | |
| base64_encode=False)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment