Computer scienceBackendKtorKtor Plugins

OAuth in Ktor

OAuth routing

Report a typo

Arrange the code so that when a user accesses the "/start" route, they are redirected to the authorization page of the OAuth provider. After successful authorization, the user should be redirected to the "/callback" route where the server saves the access token in the UserSession. Finally, the user should be redirected to the "/congratulations" route, which response with the retrieved information.

Reorder lines using drag or arrows. Adjust indentation with left buttons
                val userSession: UserSession? = call.sessions.get()
              
                }
              
                get("/congratulations") {
              
                }.body()
              
                call.respondRedirect("/greetings")
              
                call.sessions.set(UserSession(principal?.accessToken.toString()))
              
                headers { append(HttpHeaders.Authorization, "Bearer ${userSession!!.accessToken}") }
              
                val principal: OAuthAccessTokenResponse.OAuth2? = call.authentication.principal()
              
                get("/start") { /* Redirects to Google authorization page automatically */ }
              
                authenticate("auth-oauth-google") {
              
                call.respondText("You retrieve this information from Google API: $userInfo")
              
                val userInfo: String = applicationHttpClient.get("https://www.googleapis.com/oauth2/v2/userinfo") {
              
                }
              
                get("/callback") {
              
                }
              
___

Create a free account to access the full topic