Spaces:
Running
Running
Upload 3 files
Browse files- Dockerfile +7 -0
- requirements.txt +39 -0
- test.py +105 -0
Dockerfile
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.11.5-slim
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
COPY . /app
|
6 |
+
|
7 |
+
RUN pip install -r requirements.txt
|
requirements.txt
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
annotated-types==0.6.0
|
2 |
+
anyio==3.7.1
|
3 |
+
cachetools==5.3.2
|
4 |
+
certifi==2023.11.17
|
5 |
+
charset-normalizer==3.3.2
|
6 |
+
click==8.1.7
|
7 |
+
ecdsa==0.18.0
|
8 |
+
fastapi==0.104.1
|
9 |
+
google-api-core==2.14.0
|
10 |
+
google-api-python-client==2.109.0
|
11 |
+
google-auth==2.25.1
|
12 |
+
google-auth-httplib2==0.1.1
|
13 |
+
google-auth-oauthlib==1.1.0
|
14 |
+
googleapis-common-protos==1.61.0
|
15 |
+
gunicorn==21.2.0
|
16 |
+
h11==0.14.0
|
17 |
+
httplib2==0.22.0
|
18 |
+
idna==3.6
|
19 |
+
jose==1.0.0
|
20 |
+
oauthlib==3.2.2
|
21 |
+
packaging==23.2
|
22 |
+
protobuf==4.25.1
|
23 |
+
pyasn1==0.5.1
|
24 |
+
pyasn1-modules==0.3.0
|
25 |
+
pydantic==2.5.2
|
26 |
+
pydantic_core==2.14.5
|
27 |
+
pyparsing==3.1.1
|
28 |
+
python-jose==3.3.0
|
29 |
+
python-multipart==0.0.6
|
30 |
+
requests==2.31.0
|
31 |
+
requests-oauthlib==1.3.1
|
32 |
+
rsa==4.9
|
33 |
+
six==1.16.0
|
34 |
+
sniffio==1.3.0
|
35 |
+
starlette==0.27.0
|
36 |
+
typing_extensions==4.8.0
|
37 |
+
uritemplate==4.1.1
|
38 |
+
urllib3==2.1.0
|
39 |
+
uvicorn==0.24.0.post1
|
test.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Depends
|
2 |
+
from fastapi.security import OAuth2PasswordBearer
|
3 |
+
import requests
|
4 |
+
from jose import jwt
|
5 |
+
import webbrowser
|
6 |
+
import base64
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
# Replace these with your own values from the Google Developer Console
|
16 |
+
GOOGLE_CLIENT_ID = ""
|
17 |
+
GOOGLE_CLIENT_SECRET = ""
|
18 |
+
GOOGLE_REDIRECT_URI = ""
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
@app.get("/login/google")
|
23 |
+
async def login_google():
|
24 |
+
# oauth_url = f"https://accounts.google.com/o/oauth2/auth?response_type=code&client_id={GOOGLE_CLIENT_ID}&redirect_uri={GOOGLE_REDIRECT_URI}&scope=openid%20profile%20email&access_type=offline"
|
25 |
+
|
26 |
+
#Below is the URL to prompt the user to login to his specified gmail account and also give a readonly access to his gmail
|
27 |
+
oauth_url = f"https://accounts.google.com/o/oauth2/auth?response_type=code&client_id={GOOGLE_CLIENT_ID}&redirect_uri={GOOGLE_REDIRECT_URI}&scope=openid%20profile%20email%20https://www.googleapis.com/auth/gmail.readonly&access_type=offline"
|
28 |
+
|
29 |
+
# webbrowser.open(oauth_url)
|
30 |
+
return {
|
31 |
+
"url": f"https://accounts.google.com/o/oauth2/auth?response_type=code&client_id={GOOGLE_CLIENT_ID}&redirect_uri={GOOGLE_REDIRECT_URI}&scope=openid%20profile%20email&access_type=offline"
|
32 |
+
}
|
33 |
+
|
34 |
+
@app.get("/auth/google")
|
35 |
+
async def auth_google(code: str):
|
36 |
+
#This code is basically the authorization code and this authorization code helps us to get the access token with the required scopes that we have set .
|
37 |
+
#We require the gmail.readonly scopes that requires verification of our application and all.
|
38 |
+
token_url = "https://accounts.google.com/o/oauth2/token"
|
39 |
+
print(code)
|
40 |
+
data = {
|
41 |
+
"code": code,
|
42 |
+
"client_id": GOOGLE_CLIENT_ID,
|
43 |
+
"client_secret": GOOGLE_CLIENT_SECRET,
|
44 |
+
"redirect_uri": GOOGLE_REDIRECT_URI,
|
45 |
+
"grant_type": "authorization_code",
|
46 |
+
}
|
47 |
+
response = requests.post(token_url, data=data)
|
48 |
+
access_token = response.json().get("access_token")
|
49 |
+
print(response.json())
|
50 |
+
user_info = requests.get("https://www.googleapis.com/oauth2/v1/userinfo", headers={"Authorization": f"Bearer {access_token}"})
|
51 |
+
jobs_query = "subject:New application:ios"
|
52 |
+
gmail_url = f"https://www.googleapis.com/gmail/v1/users/me/messages?q={jobs_query}&maxResults=110"
|
53 |
+
gmail_response = requests.get(gmail_url, headers={"Authorization": f"Bearer {access_token}"})
|
54 |
+
messages = gmail_response.json().get("messages", [])
|
55 |
+
print(messages)
|
56 |
+
print("Printing gmail response")
|
57 |
+
print(gmail_response.json())
|
58 |
+
# Fetch attachments from the first message
|
59 |
+
attachments = []
|
60 |
+
for i,message in enumerate(messages) :
|
61 |
+
print(i)
|
62 |
+
print(message)
|
63 |
+
|
64 |
+
if message:
|
65 |
+
message_id = message.get("id")
|
66 |
+
print(message_id)
|
67 |
+
if message_id:
|
68 |
+
message_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}"
|
69 |
+
message_response = requests.get(message_url, headers={"Authorization": f"Bearer {access_token}"})
|
70 |
+
message_data = message_response.json()
|
71 |
+
|
72 |
+
# Check for parts in the message payload
|
73 |
+
if "payload" in message_data and "parts" in message_data["payload"]:
|
74 |
+
for part in message_data["payload"]["parts"]:
|
75 |
+
if "body" in part and "attachmentId" in part["body"]:
|
76 |
+
attachment_id = part["body"]["attachmentId"]
|
77 |
+
attachment_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}/attachments/{attachment_id}"
|
78 |
+
attachment_response = requests.get(attachment_url, headers={"Authorization": f"Bearer {access_token}"})
|
79 |
+
attachment_data = attachment_response.json()
|
80 |
+
data = attachment_data.get("data")
|
81 |
+
filename = part.get("filename", "untitled.txt")
|
82 |
+
|
83 |
+
if data:
|
84 |
+
# Decode base64-encoded attachment data
|
85 |
+
attachment_content = base64.urlsafe_b64decode(data.encode("UTF-8"))
|
86 |
+
|
87 |
+
# Save the attachment to a file
|
88 |
+
save_path = f"/Users/omkarmalpure/Documents/Gmail_API/attachments/{filename}"
|
89 |
+
with open(save_path, "wb") as file:
|
90 |
+
file.write(attachment_content)
|
91 |
+
|
92 |
+
attachments.append(save_path)
|
93 |
+
|
94 |
+
print(attachments)
|
95 |
+
print(len(attachments))
|
96 |
+
return user_info.json()
|
97 |
+
|
98 |
+
@app.get("/token")
|
99 |
+
async def get_token(token: str = Depends(oauth2_scheme)):
|
100 |
+
return jwt.decode(token, GOOGLE_CLIENT_SECRET, algorithms=["HS256"])
|
101 |
+
|
102 |
+
# if __name__ == "__main__":
|
103 |
+
# import uvicorn
|
104 |
+
|
105 |
+
# uvicorn.run(app, host="0.0.0.0", port=8000)
|