File size: 1,684 Bytes
db78256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from os.path import exists
import pickle

from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow

TOKEN_FILE = "token.pickle"
OAUTH_SCOPE = ["https://www.googleapis.com/auth/drive"]


def load_credentials(token_file: str):
    if exists(token_file):
        try:
            with open(token_file, "rb") as f:
                creds = pickle.load(f)
                return creds
        except Exception as e:
            print(f"Error loading credentials: {e}")
    return None


def save_credentials(token_file: str, credentials) -> None:
    try:
        with open(token_file, "wb") as f:
            pickle.dump(credentials, f)
    except Exception as e:
        print(f"Error saving credentials: {e}")


def get_credentials():
    credentials = load_credentials(TOKEN_FILE)
    if credentials and credentials.valid:
        return credentials

    if credentials and credentials.expired and credentials.refresh_token:
        try:
            credentials.refresh(Request())
            return credentials
        except Exception as e:
            print(f"Error refreshing credentials: {e}")

    try:
        flow = InstalledAppFlow.from_client_secrets_file(
            "credentials.json", OAUTH_SCOPE
        )
        credentials = flow.run_local_server(port=0, open_browser=False)
    except Exception as e:
        print(f"Error during OAuth flow: {e}")
        raise

    return credentials


def main():
    try:
        credentials = get_credentials()
        save_credentials(TOKEN_FILE, credentials)
    except Exception as e:
        print(f"Failed to obtain credentials: {e}")


if __name__ == "__main__":
    main()