Created
July 30, 2019 19:32
-
-
Save jay0lee/331c42e7a5ed074011c6565345f3a8d9 to your computer and use it in GitHub Desktop.
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
import json | |
import httplib2 | |
import sys | |
from googleapiclient import discovery | |
from google.oauth2 import service_account | |
def divide_chunks(l, n): | |
for i in range(0, len(l), n): | |
yield l[i:i + n] | |
def parse_tokens(request_id, response, exception): | |
if exception: | |
print(exception.content) | |
sys.exit(1) | |
else: | |
global access_tokens | |
access_tokens.append({'user': request_id, 'token': response['access_token']}) | |
def drive_response(request_id, response, exception): | |
if response: | |
print('%s: %s' % (request_id, response)) | |
#httplib2.debuglevel = 4 | |
fileId = sys.argv[1] | |
with open('oauth2_v4.json') as f: | |
oauth2_v4 = f.read() | |
# OAuth token endpoint doesn't like the alt=json parameter | |
discovery.JsonModel.alt_param = None | |
oa2 = discovery.build_from_document(oauth2_v4) | |
drive = discovery.build('drive', 'v3', http=httplib2.Http()) | |
with open('oauth2service.json') as f: | |
sa_info = json.loads(f.read()) | |
body = {'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer'} | |
creds = service_account.Credentials.from_service_account_info(sa_info) | |
creds = creds.with_scopes(['https://www.googleapis.com/auth/drive']) | |
users = sys.stdin.read().split('\n')[1:] | |
user_chunks = divide_chunks(users, 500) | |
global access_keys | |
access_tokens = [] | |
for users in user_chunks: | |
obatch = oa2.new_batch_http_request(callback=parse_tokens) | |
for user in users: | |
if not user: | |
continue | |
creds = creds.with_subject(user) | |
assertion = creds._make_authorization_grant_assertion().decode('utf-8') | |
body['assertion'] = assertion | |
obatch.add(oa2.tokens().get(body=body), request_id=user) | |
obatch.execute() | |
print('Got total %s access tokens...' % len(access_tokens)) | |
access_token_chunks = divide_chunks(access_tokens, 1000) | |
for access_token_chunk in access_token_chunks: | |
dbatch = drive.new_batch_http_request(callback=drive_response) | |
for access_token in access_token_chunk: | |
dbatch.add(drive.files().get(fileId=fileId, supportsAllDrives=True, oauth_token=access_token['token']), request_id=access_token['user']) | |
print('Searching %s users Drive for file' % len(dbatch._order)) | |
dbatch.execute() |
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
{ | |
"version": "v5", | |
"baseUrl": "https://oauth2.googleapis.com/", | |
"servicePath": "", | |
"description": "Google OAuth 2.0 Authentication API", | |
"kind": "discovery#restDescription", | |
"basePath": "", | |
"revision": "20190730", | |
"documentationLink": "https://developers.google.com/oauth2", | |
"id": "oauth2:v5", | |
"discoveryVersion": "v1", | |
"version_module": true, | |
"rootUrl": "https://oauth2.googleapis.com/", | |
"ownerDomain": "google.com", | |
"name": "oauth2", | |
"batchPath": "batch", | |
"fullyEncodeReservedExpansion": true, | |
"title": "Google OAuth 2.0 API", | |
"ownerName": "Google", | |
"parameters": {}, | |
"schemas": { | |
"Token": { | |
"description": "OAuth 2.0 Access Token", | |
"type": "object", | |
"properties": { | |
"access_token": { | |
"description": "Access token", | |
"type": "string" | |
}, | |
"expires_in": { | |
"description": "Seconds until token expires", | |
"type": "integer", | |
"format": "int32" | |
}, | |
"token_type": { | |
"description": "Type of token", | |
"type": "string" | |
} | |
}, | |
"id": "Token" | |
}, | |
"TokenRequest": { | |
"description": "OAuth 2.0 Token Request", | |
"type": "object", | |
"properties": { | |
"grant_type": { | |
"description": "Type of Grant", | |
"type": "string" | |
}, | |
"assertion": { | |
"description": "JWT Assertion", | |
"type": "string" | |
} | |
}, | |
"id": "TokenRequest" | |
} | |
}, | |
"resources": { | |
"tokens": { | |
"methods": { | |
"get": { | |
"request": { | |
"$ref": "TokenRequest" | |
}, | |
"response": { | |
"$ref": "Token" | |
}, | |
"httpMethod": "POST", | |
"flatPath": "token", | |
"path": "token", | |
"id": "tokens.get" | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment