Spaces:
Sleeping
Sleeping
Update test.py
Browse files
test.py
CHANGED
@@ -20,6 +20,7 @@ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
|
20 |
GOOGLE_CLIENT_ID = "485753721652-5uta3e18va2g6cnkldib2d68q39t4vod.apps.googleusercontent.com"
|
21 |
GOOGLE_CLIENT_SECRET = "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"
|
22 |
GOOGLE_REDIRECT_URI = "https://omkar008-receipt-radar-test.hf.space/auth/google"
|
|
|
23 |
|
24 |
@app.get("/")
|
25 |
async def login_google():
|
@@ -27,10 +28,11 @@ async def login_google():
|
|
27 |
|
28 |
#Below is the URL to prompt the user to login to his specified gmail account and also give a readonly access to his gmail
|
29 |
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"
|
30 |
-
|
31 |
webbrowser.open(oauth_url)
|
32 |
return {
|
33 |
-
"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"
|
|
|
34 |
}
|
35 |
|
36 |
@app.get("/auth/google")
|
@@ -97,6 +99,85 @@ async def auth_google(code: str):
|
|
97 |
print(len(attachments))
|
98 |
return user_info.json()
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
@app.get("/token")
|
101 |
async def get_token(token: str = Depends(oauth2_scheme)):
|
102 |
return jwt.decode(token, GOOGLE_CLIENT_SECRET, algorithms=["HS256"])
|
|
|
20 |
GOOGLE_CLIENT_ID = "485753721652-5uta3e18va2g6cnkldib2d68q39t4vod.apps.googleusercontent.com"
|
21 |
GOOGLE_CLIENT_SECRET = "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"
|
22 |
GOOGLE_REDIRECT_URI = "https://omkar008-receipt-radar-test.hf.space/auth/google"
|
23 |
+
GOOGLE_REDIRECT_URI_hr = "https://receiptradar-0bb387d81174.herokuapp.com/auth1/google"
|
24 |
|
25 |
@app.get("/")
|
26 |
async def login_google():
|
|
|
28 |
|
29 |
#Below is the URL to prompt the user to login to his specified gmail account and also give a readonly access to his gmail
|
30 |
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"
|
31 |
+
oauth_url_hr = f"https://accounts.google.com/o/oauth2/auth?response_type=code&client_id={GOOGLE_CLIENT_ID}&redirect_uri={GOOGLE_REDIRECT_URI_hr}&scope=openid%20profile%20email&access_type=offline"
|
32 |
webbrowser.open(oauth_url)
|
33 |
return {
|
34 |
+
"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",
|
35 |
+
"url_hr": oauth_url_hr
|
36 |
}
|
37 |
|
38 |
@app.get("/auth/google")
|
|
|
99 |
print(len(attachments))
|
100 |
return user_info.json()
|
101 |
|
102 |
+
@app.get("/auth1/google")
|
103 |
+
async def auth_google(request: Request,code: str,state: str = None):
|
104 |
+
#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 .
|
105 |
+
#We require the gmail.readonly scopes that requires verification of our application and all.
|
106 |
+
|
107 |
+
filter_query = None
|
108 |
+
if state:
|
109 |
+
state_params = state.split('&')
|
110 |
+
for param in state_params:
|
111 |
+
if param.startswith('filter_query='):
|
112 |
+
filter_query = param.split('=')[1]
|
113 |
+
|
114 |
+
|
115 |
+
token_url = "https://accounts.google.com/o/oauth2/token"
|
116 |
+
print(code)
|
117 |
+
data = {
|
118 |
+
"code": code,
|
119 |
+
"client_id": GOOGLE_CLIENT_ID,
|
120 |
+
"client_secret": GOOGLE_CLIENT_SECRET,
|
121 |
+
"redirect_uri": GOOGLE_REDIRECT_URI,
|
122 |
+
"grant_type": "authorization_code",
|
123 |
+
}
|
124 |
+
response = requests.post(token_url, data=data)
|
125 |
+
access_token = response.json().get("access_token")
|
126 |
+
print(response.json())
|
127 |
+
user_info = requests.get("https://www.googleapis.com/oauth2/v1/userinfo", headers={"Authorization": f"Bearer {access_token}"})
|
128 |
+
|
129 |
+
gmail_url = "https://www.googleapis.com/gmail/v1/users/me/messages?q=subject:receipt&maxResults=2"
|
130 |
+
gmail_response = requests.get(gmail_url, headers={"Authorization": f"Bearer {access_token}"})
|
131 |
+
messages = gmail_response.json().get("messages", [])
|
132 |
+
print(messages)
|
133 |
+
print("Printing gmail response")
|
134 |
+
print(gmail_response.json())
|
135 |
+
attachment_no = 0
|
136 |
+
data_new = [[]]
|
137 |
+
|
138 |
+
for i,message in enumerate(messages) :
|
139 |
+
|
140 |
+
|
141 |
+
if message:
|
142 |
+
message_id = message.get("id")
|
143 |
+
print(message_id)
|
144 |
+
if message_id:
|
145 |
+
message_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}"
|
146 |
+
message_response = requests.get(message_url, headers={"Authorization": f"Bearer {access_token}"})
|
147 |
+
message_data = message_response.json()
|
148 |
+
|
149 |
+
# Check for parts in the message payload
|
150 |
+
if "payload" in message_data and "parts" in message_data["payload"]:
|
151 |
+
for part in message_data["payload"]["parts"]:
|
152 |
+
if "body" in part and "attachmentId" in part["body"]:
|
153 |
+
attachment_id = part["body"]["attachmentId"]
|
154 |
+
attachment_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}/attachments/{attachment_id}"
|
155 |
+
attachment_response = requests.get(attachment_url, headers={"Authorization": f"Bearer {access_token}"})
|
156 |
+
attachment_data = attachment_response.json()
|
157 |
+
data = attachment_data.get("data")
|
158 |
+
filename = part.get("filename", "untitled.txt")
|
159 |
+
|
160 |
+
if data:
|
161 |
+
data_new.append([data.encode("utf-8")])
|
162 |
+
# Decode base64-encoded attachment data
|
163 |
+
attachment_content = base64.urlsafe_b64decode(data.encode("UTF-8"))
|
164 |
+
# print(attachment_content)
|
165 |
+
print("yo")
|
166 |
+
print(data_new)
|
167 |
+
attachment_no+=1
|
168 |
+
|
169 |
+
|
170 |
+
# Save the attachment to a file
|
171 |
+
#Here add the function or class to save the attachments to Google Cloud
|
172 |
+
# save_path = f"/Users/omkarmalpure/Desktop/Current Work/fastapi_gmail/attachments/{filename}"
|
173 |
+
# with open(save_path, "wb") as file:
|
174 |
+
# file.write(attachment_content)
|
175 |
+
|
176 |
+
|
177 |
+
|
178 |
+
|
179 |
+
return {"user_info": user_info.json(), "query": filter_query , "data":attachment_no , "attachment_content":data_new , "attachment_count":attachment_no}
|
180 |
+
|
181 |
@app.get("/token")
|
182 |
async def get_token(token: str = Depends(oauth2_scheme)):
|
183 |
return jwt.decode(token, GOOGLE_CLIENT_SECRET, algorithms=["HS256"])
|