Skip to content

Instantly share code, notes, and snippets.

@gregneagle
Created December 10, 2014 18:12
Show Gist options
  • Save gregneagle/fc66882e6fae64d82c8d to your computer and use it in GitHub Desktop.
Save gregneagle/fc66882e6fae64d82c8d to your computer and use it in GitHub Desktop.
Demo of determining if a Google account has 2FA active
#!/usr/bin/python
# Demo of determinining if a Google account has 2FA active
# Needs a Google account ID (email address) and a password
# If the password is the "main" password (not an app-specific password)
# and "Two-factor authentication required" is returned; then 2FA is active
import urllib
import urllib2
def check_google_login(email, password):
application_name = 'YourOrg-PythonScriptName-v1'
url ='https://www.google.com/accounts/ClientLogin'
values = {
'accountType': 'HOSTED_OR_GOOGLE',
'Email': email,
'Passwd': password,
'service': 'mail',
'source': application_name
}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
try:
response = urllib2.urlopen(req)
token = response.read()
return "Successful authentication"
except urllib2.HTTPError, err:
if err.code == 403:
error_result = err.read().splitlines()
if (len(error_result) > 1
and error_result[1] == 'Info=InvalidSecondFactor'):
# password was OK but we need 2FA
# or an application-specific password
return "Two-factor authentication required"
elif error_result[0] == "Error=BadAuthentication":
return "Invalid username or password"
return "Unknown authentication error: %s: %s" % (err.code, err.reason)
except BaseException, err:
return "Unknown error: %s" % err
email = '[email protected]'
password = 'Naughty0rN!c3'
print check_google_login(email, password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment