Omkar008 commited on
Commit
a4fff20
1 Parent(s): a7d45a3

Update test.py

Browse files
Files changed (1) hide show
  1. test.py +2 -30
test.py CHANGED
@@ -37,14 +37,12 @@ async def login_google():
37
  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"
38
  webbrowser.open(oauth_url)
39
  return {
40
- "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",
41
  "url_hr": oauth_url_hr
42
  }
43
 
44
  @app.post("/auth/google")
45
  async def auth_google(request: Request):
46
- #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 .
47
- #We require the gmail.readonly scopes that requires verification of our application and all.
48
  data = await request.json()
49
  code = data.get("access_token")
50
  print("Printing the access token")
@@ -64,10 +62,8 @@ async def auth_google(request: Request):
64
  # response = requests.post(token_url, data=data)
65
  # access_token = response.json().get("access_token")
66
  access_token_new = code
67
- # print(response.json())
68
- # print(access_token_new)
69
  user_info = requests.get("https://www.googleapis.com/oauth2/v1/userinfo", headers={"Authorization": f"Bearer {access_token_new}"})
70
- # user_query = data.get("user_query")
71
  page_token = None
72
  messages = []
73
  jobs_query = "subject:receipt OR subject:receipts has:attachment"
@@ -91,18 +87,6 @@ async def auth_google(request: Request):
91
  else:
92
  break # No more pages, exit the loop
93
 
94
-
95
- # print(len(messages))
96
-
97
-
98
- # query = "subject:receipt OR subject:receipts has:attachment"
99
- # gmail_url = f"https://www.googleapis.com/gmail/v1/users/me/messages?q={query}"
100
- # gmail_response = requests.get(gmail_url, headers={"Authorization": f"Bearer {access_token_new}"})
101
- # messages = gmail_response.json().get("messages", [])
102
- # print(messages)
103
- # print("Printing gmail response")
104
- # print(gmail_response.json())
105
- # Fetch attachments from the first message
106
  attachments = []
107
  attachment_no = 0
108
  data_new = {}
@@ -131,13 +115,7 @@ async def auth_google(request: Request):
131
 
132
  if data:
133
  data_new[filename]=data
134
- # data_new.append({filename:data.encode("UTF-8")})
135
- # Decode base64-encoded attachment data
136
- # print(filename)
137
  attachment_content = base64.urlsafe_b64decode(data.encode("UTF-8"))
138
- # print(attachment_content)
139
- print("yo")
140
- # print(data_new)
141
  attachment_no+=1
142
 
143
  # if data:
@@ -151,8 +129,6 @@ async def auth_google(request: Request):
151
 
152
  # attachments.append(save_path)
153
 
154
- # print(attachments)
155
- # print(len(data_new))
156
  return {"attachment_count":attachment_no,"attachment_content":data_new}
157
 
158
  @app.websocket("/ws")
@@ -323,9 +299,5 @@ async def test_websocket(websocket: WebSocket):
323
  async def get_token(token: str = Depends(oauth2_scheme)):
324
  return jwt.decode(token, GOOGLE_CLIENT_SECRET, algorithms=["HS256"])
325
 
326
- # if __name__ == "__main__":
327
- # import uvicorn
328
-
329
- # uvicorn.run(app, host="0.0.0.0", port=8000)
330
 
331
 
 
37
  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"
38
  webbrowser.open(oauth_url)
39
  return {
 
40
  "url_hr": oauth_url_hr
41
  }
42
 
43
  @app.post("/auth/google")
44
  async def auth_google(request: Request):
45
+
 
46
  data = await request.json()
47
  code = data.get("access_token")
48
  print("Printing the access token")
 
62
  # response = requests.post(token_url, data=data)
63
  # access_token = response.json().get("access_token")
64
  access_token_new = code
65
+
 
66
  user_info = requests.get("https://www.googleapis.com/oauth2/v1/userinfo", headers={"Authorization": f"Bearer {access_token_new}"})
 
67
  page_token = None
68
  messages = []
69
  jobs_query = "subject:receipt OR subject:receipts has:attachment"
 
87
  else:
88
  break # No more pages, exit the loop
89
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  attachments = []
91
  attachment_no = 0
92
  data_new = {}
 
115
 
116
  if data:
117
  data_new[filename]=data
 
 
 
118
  attachment_content = base64.urlsafe_b64decode(data.encode("UTF-8"))
 
 
 
119
  attachment_no+=1
120
 
121
  # if data:
 
129
 
130
  # attachments.append(save_path)
131
 
 
 
132
  return {"attachment_count":attachment_no,"attachment_content":data_new}
133
 
134
  @app.websocket("/ws")
 
299
  async def get_token(token: str = Depends(oauth2_scheme)):
300
  return jwt.decode(token, GOOGLE_CLIENT_SECRET, algorithms=["HS256"])
301
 
 
 
 
 
302
 
303