1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
import requests
from ._version import __version__
from . import google
# The key is distirbuted with Google Play Services.
# This one is from version 7.3.29.
b64_key_7_3_29 = (b"AAAAgMom/1a/v0lblO2Ubrt60J2gcuXSljGFQXgcyZWveWLEwo6prwgi3"
b"iJIZdodyhKZQrNWp5nKJ3srRXcUW+F1BD3baEVGcmEgqaLZUNBjm057pK"
b"RI16kB0YppeGx5qIQ5QjKzsR8ETQbKLNWgRY0QRNVz34kMJR3P/LgHax/"
b"6rmf5AAAAAwEAAQ==")
android_key_7_3_29 = google.key_from_b64(b64_key_7_3_29)
auth_url = 'https://android.clients.google.com/auth'
useragent = 'gpsoauth/' + __version__
def _perform_auth_request(data, proxy=None):
session = requests.session()
if proxy:
session.proxies = proxy
res = session.post(auth_url, data,
headers={'User-Agent': useragent})
return google.parse_auth_response(res.text)
def perform_master_login(email, password, android_id,
service='ac2dm', device_country='us', operatorCountry='us',
lang='en', sdk_version=17, proxy=None):
"""
Perform a master login, which is what Android does when you first add a Google account.
Return a dict, eg::
{
'Auth': '...',
'Email': 'email@gmail.com',
'GooglePlusUpgrade': '1',
'LSID': '...',
'PicasaUser': 'My Name',
'RopRevision': '1',
'RopText': ' ',
'SID': '...',
'Token': 'oauth2rt_1/...',
'firstName': 'My',
'lastName': 'Name',
'services': 'hist,mail,googleme,...'
}
"""
data = {
'accountType': 'HOSTED_OR_GOOGLE',
'Email': email,
'has_permission': 1,
'add_account': 1,
'EncryptedPasswd': google.signature(email, password, android_key_7_3_29),
'service': service,
'source': 'android',
'androidId': android_id,
'device_country': device_country,
'operatorCountry': device_country,
'lang': lang,
'sdk_version': sdk_version
}
return _perform_auth_request(data, proxy)
def perform_oauth(email, master_token, android_id, service, app, client_sig,
device_country='us', operatorCountry='us', lang='en', sdk_version=17,
proxy=None):
"""
Use a master token from master_login to perform OAuth to a specific Google service.
Return a dict, eg::
{
'Auth': '...',
'LSID': '...',
'SID': '..',
'issueAdvice': 'auto',
'services': 'hist,mail,googleme,...'
}
To authenticate requests to this service, include a header
``Authorization: GoogleLogin auth=res['Auth']``.
"""
data = {
'accountType': 'HOSTED_OR_GOOGLE',
'Email': email,
'has_permission': 1,
'EncryptedPasswd': master_token,
'service': service,
'source': 'android',
'androidId': android_id,
'app': app,
'client_sig': client_sig,
'device_country': device_country,
'operatorCountry': device_country,
'lang': lang,
'sdk_version': sdk_version
}
return _perform_auth_request(data, proxy)
|