Gaykar commited on
Commit
0210631
·
1 Parent(s): 45cdac8

changes in gmail auth.py

Browse files
Files changed (2) hide show
  1. app/gmail_auth.py +32 -2
  2. app/main.py +2 -12
app/gmail_auth.py CHANGED
@@ -1,20 +1,50 @@
 
 
1
  from app.core.config import settings
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from langchain_google_community.gmail.utils import (
3
  build_resource_service,
4
  get_gmail_credentials,
5
  )
6
 
7
  from langchain_google_community import GmailToolkit
 
 
 
8
  credentials = get_gmail_credentials(
9
- token_file=settings.GMAIL_TOKEN_PATH,
10
  scopes=["https://mail.google.com/"],
11
- client_sercret_file=settings.GMAIL_CREDENTIALS_PATH,
12
  )
13
 
 
 
14
  api_resource = build_resource_service(
15
  credentials=credentials
16
  )
17
 
 
 
18
  gmail_toolkit = GmailToolkit(
19
  api_resource=api_resource
20
  )
 
1
+ import os
2
+ from pathlib import Path
3
  from app.core.config import settings
4
+
5
+ # CREATE TEMP DIRECTORY
6
+
7
+ AUTH_DIR = Path("/tmp/google_auth")
8
+ AUTH_DIR.mkdir(parents=True, exist_ok=True)
9
+
10
+ # FULL FILE PATHS
11
+
12
+ CREDENTIALS_PATH = AUTH_DIR / "credentials.json"
13
+ TOKEN_PATH = AUTH_DIR / "token.json"
14
+
15
+ # CREATE FILES FROM HF SECRETS
16
+
17
+ if not CREDENTIALS_PATH.exists():
18
+ CREDENTIALS_PATH.write_text(os.environ["GOOGLE_CREDENTIALS"])
19
+
20
+ if not TOKEN_PATH.exists():
21
+ TOKEN_PATH.write_text(os.environ["GOOGLE_TOKEN"])
22
+
23
+ # GMAIL IMPORTS
24
+
25
  from langchain_google_community.gmail.utils import (
26
  build_resource_service,
27
  get_gmail_credentials,
28
  )
29
 
30
  from langchain_google_community import GmailToolkit
31
+
32
+ # LOAD CREDENTIALS
33
+
34
  credentials = get_gmail_credentials(
35
+ token_file=str(TOKEN_PATH),
36
  scopes=["https://mail.google.com/"],
37
+ client_secret_file=str(CREDENTIALS_PATH),
38
  )
39
 
40
+ # BUILD API RESOURCE
41
+
42
  api_resource = build_resource_service(
43
  credentials=credentials
44
  )
45
 
46
+ # TOOLKIT
47
+
48
  gmail_toolkit = GmailToolkit(
49
  api_resource=api_resource
50
  )
app/main.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  from fastapi import FastAPI, HTTPException, Depends
2
  from pydantic import BaseModel, EmailStr
3
  from typing import Optional, Dict, Any, TypedDict, Annotated, Sequence
@@ -11,25 +13,13 @@ from app.database.connection import get_session
11
  from app.database.utils import get_or_create_user
12
  from sqlalchemy.orm import Session
13
  from app.database.connection import SessionLocal
14
- from app.core.config import settings
15
  from fastapi import Request
16
- import os
17
  from app.database.models import User
18
  from app.core.auth import create_access_token,get_current_user
19
 
20
  # CREATE GMAIL AUTH FILES FROM HF SECRETS
21
 
22
 
23
- if not os.path.exists(settings.GMAIL_CREDENTIALS_PATH):
24
-
25
- with open(settings.GMAIL_CREDENTIALS_PATH, "w") as f:
26
- f.write(os.environ["GOOGLE_CREDENTIALS"])
27
-
28
- if not os.path.exists(settings.GMAIL_TOKEN_PATH):
29
-
30
- with open(settings.GMAIL_TOKEN_PATH, "w") as f:
31
- f.write(os.environ["GOOGLE_TOKEN"])
32
-
33
 
34
  logger = logging.getLogger(__name__)
35
 
 
1
+ import os
2
+ from app.core.config import settings
3
  from fastapi import FastAPI, HTTPException, Depends
4
  from pydantic import BaseModel, EmailStr
5
  from typing import Optional, Dict, Any, TypedDict, Annotated, Sequence
 
13
  from app.database.utils import get_or_create_user
14
  from sqlalchemy.orm import Session
15
  from app.database.connection import SessionLocal
 
16
  from fastapi import Request
 
17
  from app.database.models import User
18
  from app.core.auth import create_access_token,get_current_user
19
 
20
  # CREATE GMAIL AUTH FILES FROM HF SECRETS
21
 
22
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  logger = logging.getLogger(__name__)
25