Repository navigation
Expand file tree
/
Copy pathcommon.py
More file actions
51 lines (41 loc) · 1.63 KB
/
Copy pathcommon.py
File metadata and controls
51 lines (41 loc) · 1.63 KB
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
import json
import socket
import tempfile
from google.oauth2 import service_account
import googleapiclient.discovery
from .config import (
GOOGLE_SERVICE_ACCOUNT_CLIENT_EMAIL,
GOOGLE_SERVICE_ACCOUNT_CLIENT_ID,
GOOGLE_SERVICE_ACCOUNT_DELEGATED_ACCOUNT,
GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY,
GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY_ID,
GOOGLE_SERVICE_ACCOUNT_PROJECT_ID,
GOOGLE_DEVELOPER_KEY,
)
TIMEOUT = 300
socket.setdefaulttimeout(TIMEOUT)
def get_service(service_name: str, version: str, scopes: list):
"""Formats specified credentials for Google clients."""
data = {
"type": "service_account",
"project_id": GOOGLE_SERVICE_ACCOUNT_PROJECT_ID,
"private_key_id": GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY_ID,
"private_key": str(GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY).replace("\\n", "\n"),
"client_email": GOOGLE_SERVICE_ACCOUNT_CLIENT_EMAIL,
"client_id": GOOGLE_SERVICE_ACCOUNT_CLIENT_ID,
"token_uri": "https://oauth2.googleapis.com/token",
}
with tempfile.NamedTemporaryFile() as service_account_file:
service_account_file.write(json.dumps(data).encode())
service_account_file.seek(0)
credentials = service_account.Credentials.from_service_account_file(
service_account_file.name, scopes=scopes
)
delegated_credentials = credentials.with_subject(GOOGLE_SERVICE_ACCOUNT_DELEGATED_ACCOUNT)
return googleapiclient.discovery.build(
service_name,
version,
credentials=delegated_credentials,
cache_discovery=False,
developerKey=GOOGLE_DEVELOPER_KEY,
)