Spaces:
Running
Running
File size: 11,324 Bytes
9d7733d 8b7bbc9 7c15110 8b7bbc9 7c15110 b4bcc22 8b7bbc9 4d85d66 8b7bbc9 388857b 732b374 8b7bbc9 e715b7e 8b7bbc9 aaebbaa a7c9f3e 8b7bbc9 a07cfe0 68bbf2b a07cfe0 6470042 8e36064 6470042 f562fef 8f237b9 f562fef 8b7bbc9 87cc66e 0f607c6 8b7bbc9 b427156 8b7bbc9 238f6ea 8b7bbc9 d580044 0f607c6 8b7bbc9 a6fb786 8b7bbc9 a6fb786 87cc66e 8b7bbc9 87cc66e 8b7bbc9 87cc66e 8b7bbc9 a5e2e68 2e70b03 8b7bbc9 e715b7e 7407e94 e715b7e 8b7bbc9 7e3b6a7 |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
from fastapi import FastAPI, Depends , Request
from fastapi.security import OAuth2PasswordBearer
import requests
from jose import jwt
import webbrowser
import base64
app = FastAPI()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
# Replace these with your own values from the Google Developer Console
# GOOGLE_CLIENT_ID = ""
# GOOGLE_CLIENT_SECRET = ""
# GOOGLE_REDIRECT_URI = ""
GOOGLE_CLIENT_ID = "485753721652-5uta3e18va2g6cnkldib2d68q39t4vod.apps.googleusercontent.com"
GOOGLE_CLIENT_SECRET = "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"
GOOGLE_REDIRECT_URI = "https://omkar008-receipt-radar-test.hf.space/auth/google"
GOOGLE_REDIRECT_URI_hr = "https://receiptradar-0bb387d81174.herokuapp.com/auth/google"
@app.get("/")
async def login_google():
# 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"
#Below is the URL to prompt the user to login to his specified gmail account and also give a readonly access to his gmail
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"
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%20https://www.googleapis.com/auth/gmail.readonly&access_type=offline&state=receipts"
webbrowser.open(oauth_url)
return {
"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",
"url_hr": oauth_url_hr
}
@app.post("/auth/google")
async def auth_google(request: Request):
#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 .
#We require the gmail.readonly scopes that requires verification of our application and all.
data = await request.json()
code = data.get("access_token")
print("Printing the authorisation token")
print(code)
if not code:
raise HTTPException(status_code=400, detail="Authorization code not provided")
# token_url = "https://accounts.google.com/o/oauth2/token"
# print(code)
# data = {
# "code": code,
# "client_id": GOOGLE_CLIENT_ID,
# "client_secret": GOOGLE_CLIENT_SECRET,
# "redirect_uri": GOOGLE_REDIRECT_URI,
# "grant_type": "authorization_code",
# }
# response = requests.post(token_url, data=data)
# access_token = response.json().get("access_token")
access_token_new = code
# print(response.json())
# print(access_token_new)
user_info = requests.get("https://www.googleapis.com/oauth2/v1/userinfo", headers={"Authorization": f"Bearer {access_token_new}"})
# user_query = data.get("user_query")
page_token = None
messages = []
jobs_query = "subject:receipt OR subject:receipts has:attachment"
while True:
# Construct Gmail API request with pageToken
gmail_url = f"https://www.googleapis.com/gmail/v1/users/me/messages?q={jobs_query}"
if page_token:
gmail_url += f"&pageToken={page_token}"
gmail_response = requests.get(gmail_url, headers={"Authorization": f"Bearer {access_token_new}"})
gmail_data = gmail_response.json()
# Check if there are messages in the response
if "messages" in gmail_data:
messages.extend(gmail_data["messages"])
# Check if there are more pages
if "nextPageToken" in gmail_data:
page_token = gmail_data["nextPageToken"]
else:
break # No more pages, exit the loop
print(len(messages))
# query = "subject:receipt OR subject:receipts has:attachment"
# gmail_url = f"https://www.googleapis.com/gmail/v1/users/me/messages?q={query}"
# gmail_response = requests.get(gmail_url, headers={"Authorization": f"Bearer {access_token_new}"})
# messages = gmail_response.json().get("messages", [])
# print(messages)
# print("Printing gmail response")
# print(gmail_response.json())
# Fetch attachments from the first message
attachments = []
attachment_no = 0
data_new = {}
for i,message in enumerate(messages) :
print(i)
print(message)
if message:
message_id = message.get("id")
print(message_id)
if message_id:
message_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}"
message_response = requests.get(message_url, headers={"Authorization": f"Bearer {access_token_new}"})
message_data = message_response.json()
# Check for parts in the message payload
if "payload" in message_data and "parts" in message_data["payload"]:
for part in message_data["payload"]["parts"]:
if "body" in part and "attachmentId" in part["body"]:
attachment_id = part["body"]["attachmentId"]
attachment_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}/attachments/{attachment_id}"
attachment_response = requests.get(attachment_url, headers={"Authorization": f"Bearer {access_token_new}"})
attachment_data = attachment_response.json()
data = attachment_data.get("data")
filename = part.get("filename", "untitled.txt")
if data:
data_new[filename]=data
# data_new.append({filename:data.encode("UTF-8")})
# Decode base64-encoded attachment data
print(filename)
attachment_content = base64.urlsafe_b64decode(data.encode("UTF-8"))
print(attachment_content)
print("yo")
print(data_new)
attachment_no+=1
# if data:
# # Decode base64-encoded attachment data
# attachment_content = base64.urlsafe_b64decode(data.encode("UTF-8"))
# # Save the attachment to a file
# save_path = f"/Users/omkarmalpure/Documents/Gmail_API/attachments/{filename}"
# with open(save_path, "wb") as file:
# file.write(attachment_content)
# attachments.append(save_path)
print(attachments)
print(len(data_new))
return {"attachment_count":attachment_no,"attachment_content":data_new}
@app.get("/auth1/google")
async def auth_google(request: Request):
#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 .
#We require the gmail.readonly scopes that requires verification of our application and all.
filter_query = None
if state:
state_params = state.split('&')
for param in state_params:
if param.startswith('filter_query='):
filter_query = param.split('=')[1]
token_url = "https://accounts.google.com/o/oauth2/token"
print(code)
data = {
"code": code,
"client_id": GOOGLE_CLIENT_ID,
"client_secret": GOOGLE_CLIENT_SECRET,
"redirect_uri": GOOGLE_REDIRECT_URI,
"grant_type": "authorization_code",
}
response = requests.post(token_url, data=data)
access_token = response.json().get("access_token")
print(response.json())
user_info = requests.get("https://www.googleapis.com/oauth2/v1/userinfo", headers={"Authorization": f"Bearer {access_token}"})
gmail_url = "https://www.googleapis.com/gmail/v1/users/me/messages?q=subject:receipt&maxResults=2"
gmail_response = requests.get(gmail_url, headers={"Authorization": f"Bearer {access_token}"})
messages = gmail_response.json().get("messages", [])
print(messages)
print("Printing gmail response")
print(gmail_response.json())
attachment_no = 0
data_new = [[]]
for i,message in enumerate(messages) :
if message:
message_id = message.get("id")
print(message_id)
if message_id:
message_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}"
message_response = requests.get(message_url, headers={"Authorization": f"Bearer {access_token}"})
message_data = message_response.json()
# Check for parts in the message payload
if "payload" in message_data and "parts" in message_data["payload"]:
for part in message_data["payload"]["parts"]:
if "body" in part and "attachmentId" in part["body"]:
attachment_id = part["body"]["attachmentId"]
attachment_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}/attachments/{attachment_id}"
attachment_response = requests.get(attachment_url, headers={"Authorization": f"Bearer {access_token}"})
attachment_data = attachment_response.json()
data = attachment_data.get("data")
filename = part.get("filename", "untitled.txt")
if data:
data_new.append([data.encode("utf-8")])
# Decode base64-encoded attachment data
attachment_content = base64.urlsafe_b64decode(data.encode("UTF-8"))
# print(attachment_content)
print("yo")
print(data_new)
attachment_no+=1
# Save the attachment to a file
#Here add the function or class to save the attachments to Google Cloud
# save_path = f"/Users/omkarmalpure/Desktop/Current Work/fastapi_gmail/attachments/{filename}"
# with open(save_path, "wb") as file:
# file.write(attachment_content)
return {"user_info": user_info.json(), "query": filter_query , "data":attachment_no , "attachment_content":data_new , "attachment_count":attachment_no}
@app.get("/token")
async def get_token(token: str = Depends(oauth2_scheme)):
return jwt.decode(token, GOOGLE_CLIENT_SECRET, algorithms=["HS256"])
# if __name__ == "__main__":
# import uvicorn
# uvicorn.run(app, host="0.0.0.0", port=8000)
|