-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathexternal_cons.py
48 lines (37 loc) · 1.38 KB
/
external_cons.py
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
# import.standard
import os
from typing import Any
# import.thirdparty
import praw
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
async def the_reddit() -> Any:
""" Gets the Reddit connection. """
reddit = praw.Reddit(client_id=os.getenv('REDDIT_CLIENT_ID'), # client id
client_secret=os.getenv('REDDIT_CLIENT_SECRET'), # my client secret
user_agent=os.getenv('USER_AGENT'), # my user agent. It can be anything
username='', # Not needed
password='') # Not needed
return reddit
async def the_drive() -> Any:
""" Gets the GoogleDrive connection. """
gauth = GoogleAuth()
# gauth.LocalWebserverAuth()
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
# This is what solved the issues:
gauth.GetFlow()
gauth.flow.params.update({'access_type': 'offline'})
gauth.flow.params.update({'approval_prompt': 'force'})
# Authenticate if they're not there
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")
drive = GoogleDrive(gauth)
return drive