3131# [START savegoogletoken]
3232@identity_fn .before_user_created ()
3333def savegoogletoken (
34- event : identity_fn .AuthBlockingEvent ) -> identity_fn .BeforeCreateResponse | None :
34+ event : identity_fn .AuthBlockingEvent ,
35+ ) -> identity_fn .BeforeCreateResponse | None :
3536 """During sign-up, save the Google OAuth2 access token and queue up a task
3637 to schedule an onboarding session on the user's Google Calendar.
3738
@@ -48,24 +49,19 @@ def savegoogletoken(
4849 doc_ref .set ({"calendar_access_token" : event .credential .access_token }, merge = True )
4950
5051 tasks_client = google .cloud .tasks_v2 .CloudTasksClient ()
51- task_queue = tasks_client .queue_path (params . PROJECT_ID . value ,
52- options .SupportedRegion .US_CENTRAL1 ,
53- "scheduleonboarding" )
52+ task_queue = tasks_client .queue_path (
53+ params . PROJECT_ID . value , options .SupportedRegion .US_CENTRAL1 , "scheduleonboarding"
54+ )
5455 target_uri = get_function_url ("scheduleonboarding" )
55- calendar_task = google .cloud .tasks_v2 .Task (http_request = {
56- "http_method" : google .cloud .tasks_v2 .HttpMethod .POST ,
57- "url" : target_uri ,
58- "headers" : {
59- "Content-type" : "application/json"
56+ calendar_task = google .cloud .tasks_v2 .Task (
57+ http_request = {
58+ "http_method" : google .cloud .tasks_v2 .HttpMethod .POST ,
59+ "url" : target_uri ,
60+ "headers" : {"Content-type" : "application/json" },
61+ "body" : json .dumps ({"data" : {"uid" : event .data .uid }}).encode (),
6062 },
61- "body" : json .dumps ({
62- "data" : {
63- "uid" : event .data .uid
64- }
65- }).encode ()
66- },
67- schedule_time = datetime .now () +
68- timedelta (minutes = 1 ))
63+ schedule_time = datetime .now () + timedelta (minutes = 1 ),
64+ )
6965 tasks_client .create_task (parent = task_queue , task = calendar_task )
7066# [END savegoogletoken]
7167
@@ -79,46 +75,48 @@ def scheduleonboarding(request: tasks_fn.CallableRequest) -> https_fn.Response:
7975 """
8076
8177 if "uid" not in request .data :
82- return https_fn .Response (status = https_fn .FunctionsErrorCode .INVALID_ARGUMENT ,
83- response = "No user specified." )
78+ return https_fn .Response (
79+ status = https_fn .FunctionsErrorCode .INVALID_ARGUMENT , response = "No user specified."
80+ )
8481 uid = request .data ["uid" ]
8582
8683 user_record : auth .UserRecord = auth .get_user (uid )
8784 if user_record .email is None :
88- return https_fn .Response (status = https_fn .FunctionsErrorCode .INVALID_ARGUMENT ,
89- response = "No email address on record." )
85+ return https_fn .Response (
86+ status = https_fn .FunctionsErrorCode .INVALID_ARGUMENT ,
87+ response = "No email address on record." ,
88+ )
9089
9190 firestore_client : google .cloud .firestore .Client = firestore .client ()
9291 user_info = firestore_client .collection ("user_info" ).document (uid ).get ().to_dict ()
9392 if not isinstance (user_info , dict ) or "calendar_access_token" not in user_info :
94- return https_fn .Response (status = https_fn .FunctionsErrorCode .PERMISSION_DENIED ,
95- response = "No Google OAuth token found." )
93+ return https_fn .Response (
94+ status = https_fn .FunctionsErrorCode .PERMISSION_DENIED ,
95+ response = "No Google OAuth token found." ,
96+ )
9697 calendar_access_token = user_info ["calendar_access_token" ]
9798 firestore_client .collection ("user_info" ).document (uid ).update (
98- {"calendar_access_token" : google .cloud .firestore .DELETE_FIELD })
99+ {"calendar_access_token" : google .cloud .firestore .DELETE_FIELD }
100+ )
99101
100102 google_credentials = google .oauth2 .credentials .Credentials (token = calendar_access_token )
101103
102- calendar_client = googleapiclient .discovery .build ("calendar" ,
103- "v3" ,
104- credentials = google_credentials )
104+ calendar_client = googleapiclient .discovery .build (
105+ "calendar" , "v3" , credentials = google_credentials
106+ )
105107 calendar_event = {
106108 "summary" : "Onboarding with ExampleCo" ,
107109 "location" : "Video call" ,
108110 "description" : "Walk through onboarding tasks with an ExampleCo engineer." ,
109111 "start" : {
110112 "dateTime" : (datetime .now () + timedelta (days = 3 )).isoformat (),
111- "timeZone" : "America/Los_Angeles"
113+ "timeZone" : "America/Los_Angeles" ,
112114 },
113115 "end" : {
114116 "dateTime" : (datetime .now () + timedelta (days = 3 , hours = 1 )).isoformat (),
115- "timeZone" : "America/Los_Angeles"
117+ "timeZone" : "America/Los_Angeles" ,
116118 },
117- "attendees" : [{
118- "email" : user_record .email
119- }, {
120- "email" : "onboarding@example.com"
121- }]
119+ "attendees" : [{"email" : user_record .email }, {"email" : "onboarding@example.com" }],
122120 }
123121 calendar_client .events ().insert (calendarId = "primary" , body = calendar_event ).execute ()
124122
@@ -137,10 +135,13 @@ def get_function_url(name: str, location: str = options.SupportedRegion.US_CENTR
137135 The URL of the function
138136 """
139137 credentials , project_id = google .auth .default (
140- scopes = ["https://www.googleapis.com/auth/cloud-platform" ])
138+ scopes = ["https://www.googleapis.com/auth/cloud-platform" ]
139+ )
141140 authed_session = google .auth .transport .requests .AuthorizedSession (credentials )
142- url = ("https://cloudfunctions.googleapis.com/v2beta/" +
143- f"projects/{ project_id } /locations/{ location } /functions/{ name } " )
141+ url = (
142+ "https://cloudfunctions.googleapis.com/v2beta/"
143+ + f"projects/{ project_id } /locations/{ location } /functions/{ name } "
144+ )
144145 response = authed_session .get (url )
145146 data = response .json ()
146147 function_url = data ["serviceConfig" ]["uri" ]
0 commit comments