google py

Solutions on MaxInterview for google py by the best coders in the world

showing results for - "google py"
Maylis
25 Mar 2017
1import pickle
2import os
3from google_auth_oauthlib.flow import Flow, InstalledAppFlow
4from googleapiclient.discovery import build
5from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
6from google.auth.transport.requests import Request
7
8
9def Create_Service(client_secret_file, api_name, api_version, *scopes):
10    print(client_secret_file, api_name, api_version, scopes, sep='-')
11    CLIENT_SECRET_FILE = client_secret_file
12    API_SERVICE_NAME = api_name
13    API_VERSION = api_version
14    SCOPES = [scope for scope in scopes[0]]
15    print(SCOPES)
16
17    cred = None
18
19    pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}.pickle'
20    # print(pickle_file)
21
22    if os.path.exists(pickle_file):
23        with open(pickle_file, 'rb') as token:
24            cred = pickle.load(token)
25
26    if not cred or not cred.valid:
27        if cred and cred.expired and cred.refresh_token:
28            cred.refresh(Request())
29        else:
30            flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
31            cred = flow.run_local_server()
32
33        with open(pickle_file, 'wb') as token:
34            pickle.dump(cred, token)
35
36    try:
37        service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
38        print(API_SERVICE_NAME, 'service created successfully')
39        return service
40    except Exception as e:
41        print('Unable to connect.')
42        print(e)
43        return None
44
45def convert_to_RFC_datetime(year=1900, month=1, day=1, hour=0, minute=0):
46    dt = datetime.datetime(year, month, day, hour, minute, 0).isoformat() + 'Z'
47    return dt
similar questions
queries leading to this page
google py