import requests
import json
import oauth2client
import httplib2
import urllib.parse
import urllib.request
import urllib.error
from oauth2client import client
from oauth2client import tools
from apiclient import discovery
SCOPES = 'https://www.googleapis.com/auth/drive'
APPLICATION_NAME = 'G_Drive_auth'
CLIENT_SECRET_FILE = 'client_secret.json'
credential_path = ...\.credentials\drive-python-quickstart.json'
credential_file = ...\.credentials\drive-python-quickstart.json'
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
def credentials_renew():
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def refreshAccessToken(self):
"""Refresh current access token with refresh token
Return: access token
"""
params = {"grant_type": "refresh_token",
"refresh_token": self.refreshToken}
for i in [self.CLIENT_ID, self.CLIENT_SECRET]:
params[i] = self.conf[i]
data = urllib.parse.urlencode(params).encode("utf-8")
request = urllib.request.Request(self.conf[self.TOKEN_ENDPOINT])
request.add_header("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
f = urllib.request.urlopen(request, data)
root = json.loads(f.read().decode("utf-8"))
self.accessToken = root[self.ACCESS_TOKEN]
self.__saveCacheTokens()
return self.accessToken
cred = credentials_renew()
http = cred.authorize(httplib2.Http())
service = discovery.build('drive', 'v2', http=http)
results = service.files().list(maxResults=10).execute()
print(results)
with open(credential_file) as data_file:
data = json.load(data_file)
access_token = data['access_token']
print(access_token)
token = access_token
url = "https://www.googleapis.com/drive/v2/files?key="
key = ""
header = {'Authorization':'Bearer ' + token}
list_files = requests.get(url, headers=header)
answer = list_files.json()
print(answer)
ask_token = requests.get('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token='+token)
token_expires = ask_token.json()
print(token_expires['expires_in'])
What I want to do is to use python’s requests module to clear this shit out of code and make automation token renewal process.
But just one problem I can’t handle even in plans – is to make generation of this token from steps 2,3 – automate, because google wants me, as user, to click an “Agree” button right in browser, and there is no way to automate it without some way of shitcode and pseudo-browsers.
Александр Брюндтзвельт - гений, филантроп, 100 гривен в кармане.
Этот блог - "сток" моих мыслей и заметок. Достаточно одного взгляда на него, чтобы понять, что такой же бардак творится у меня в голове.
Если вам этот бардак интересен - милости прошу.
Александр Брюндтзвельт - гений, филантроп, 100 гривен в кармане.
Этот блог - "сток" моих мыслей и заметок. Достаточно одного взгляда на него, чтобы понять, что такой же бардак творится у меня в голове.
Если вам этот бардак интересен - милости прошу.