Finding Sessions

Problem

You want to find all sessions associated with your user.

Solution

Use the client.oauth.sessions.find() method to find all sessions owned by the current user.

from posit import connect

client = connect.Client()
sessions = client.oauth.sessions.find()

The results can be transformed into a DataFrame for easy viewing and filtering:

>>> import polars as pl
>>> pl.DataFrame(sessions)
shape: (5, 7)
┌─────┬─────────────────────────────────┬─────────────────────────────────┬─────────────────────────────────┬───────────────────┬──────────────────────┬──────────────────────┐
id  ┆ guid                            ┆ user_guid                       ┆ oauth_integration_guid          ┆ has_refresh_token ┆ created_time         ┆ updated_time         │
---------------------
strstrstrstrboolstrstr
╞═════╪═════════════════════════════════╪═════════════════════════════════╪═════════════════════════════════╪═══════════════════╪══════════════════════╪══════════════════════╡
846ec17261-1fff-4155-ac44-75a9e0… ┆ 2aa02212-e7ee-4689-966f-0bd8a1… ┆ 9304b050-ac0b-4b3f-b115-f0e8d8… ┆ true              ┆ 2024-08-05T21:13:19Z2024-08-05T21:13:23Z
8663d20b87-92cc-44d1-8144-b52a91… ┆ 2aa02212-e7ee-4689-966f-0bd8a1… ┆ 4b94d280-c9d2-4b09-8b14-0833fe… ┆ true              ┆ 2024-08-05T21:24:05Z2024-08-05T21:24:05Z
89  ┆ fe10c16f-3d4b-4be0-b371-f2fb9e… ┆ 2aa02212-e7ee-4689-966f-0bd8a1… ┆ f5315588-1155-4b32-872e-e9b7a9… ┆ true              ┆ 2024-08-05T21:31:44Z2024-08-05T21:32:26Z
902e4edbe7-117b-417f-b915-1d3851… ┆ 2aa02212-e7ee-4689-966f-0bd8a1… ┆ 5b3c3c91-6c77-4403-9852-3dd3ba… ┆ true              ┆ 2024-08-05T21:41:25Z2024-08-05T21:41:31Z
91547cab34-bee6-43b6-9684-a08f04… ┆ 2aa02212-e7ee-4689-966f-0bd8a1… ┆ ccfe80c0-3036-4b26-8e75-485a96… ┆ false             ┆ 2024-09-11T19:47:26Z2024-09-11T19:47:26Z
└─────┴─────────────────────────────────┴─────────────────────────────────┴─────────────────────────────────┴───────────────────┴──────────────────────┴──────────────────────┘

Use the connectapi client to make a direct call to the sessions endpoint.

library(connectapi)

client <- connect()
sessions <- client$GET("v1/oauth/sessions/")

The results can be transformed into a DataFrame for easy viewing and filtering:

> library(purrr)
> sessions_df <- map_dfr(sessions, ~.x)
> sessions_df
# A tibble: 5 × 7
  id    guid                                 user_guid                            oauth_integration_guid               has_refresh_token created_time         updated_time        
  <chr> <chr>                                <chr>                                <chr>                                <lgl>             <chr>                <chr>               
1 84    6ec17261-1fff-4155-ac44-75a9e063fd7d 2aa02212-e7ee-4689-966f-0bd8a195cfc7 9304b050-ac0b-4b3f-b115-f0e8d8023f3f TRUE              2024-08-05T21:13:19Z 2024-08-05T21:13:23Z
2 86    63d20b87-92cc-44d1-8144-b52a91177ac8 2aa02212-e7ee-4689-966f-0bd8a195cfc7 4b94d280-c9d2-4b09-8b14-0833fe1e02c5 TRUE              2024-08-05T21:24:05Z 2024-08-05T21:24:05Z
3 89    fe10c16f-3d4b-4be0-b371-f2fb9e728dd9 2aa02212-e7ee-4689-966f-0bd8a195cfc7 f5315588-1155-4b32-872e-e9b7a979a627 TRUE              2024-08-05T21:31:44Z 2024-08-05T21:32:26Z
4 90    2e4edbe7-117b-417f-b915-1d3851cf1268 2aa02212-e7ee-4689-966f-0bd8a195cfc7 5b3c3c91-6c77-4403-9852-3dd3ba33807d TRUE              2024-08-05T21:41:25Z 2024-08-05T21:41:31Z
5 91    547cab34-bee6-43b6-9684-a08f04d23fb6 2aa02212-e7ee-4689-966f-0bd8a195cfc7 ccfe80c0-3036-4b26-8e75-485a9605f252 FALSE             2024-09-11T19:47:26Z 2024-09-11T19:47:26Z

Discussion

These methods provide an effective way to search for sessions when you do not know the session unique identifier (GUID).

Several recipes require a single session or unique identifier.

Below is an example of how to extract the first session from the results that matches a given oauth_integration_guid and user_guid. The snippet returns None if there are no matches or the list is empty.

>>> next((s for s in sessions if s.oauth_integration_guid == "153a3d3f-e436-4502-8a5f-01fbb5def977" and s.user_guid == "2aa02212-e7ee-4689-966f-0bd8a195cfc7"), None)
{'id': '13', 'guid': '8f7b293d-2078-439a-bb0e-469e13d4d38d', 'user_guid': '2aa02212-e7ee-4689-966f-0bd8a195cfc7', 'oauth_integration_guid': '153a3d3f-e436-4502-8a5f-01fbb5def977', 'has_refresh_token': True, 'created_time': '2024-07-14T21:50:46Z', 'updated_time': '2024-07-14T21:53:39Z'}

Below is an example of how to extract the first GUID that matches a given oauth_integration_guid and user_guid.

> library(dplyr)
> sessions_df |>
  filter(oauth_integration_guid == "153a3d3f-e436-4502-8a5f-01fbb5def977", user_guid == "2aa02212-e7ee-4689-966f-0bd8a195cfc7") |>
  slice(1) |>
  pull(guid)
[1] "8f7b293d-2078-439a-bb0e-469e13d4d38d"

Administrators can use the all parameter to find sessions for all users.

sessions = client.oauth.sessions.find(all=True)
>>> pl.DataFrame(sessions)
shape: (24, 7)
┌─────┬─────────────────────────────────┬─────────────────────────────────┬─────────────────────────────────┬───────────────────┬──────────────────────┬──────────────────────┐
id  ┆ guid                            ┆ user_guid                       ┆ oauth_integration_guid          ┆ has_refresh_token ┆ created_time         ┆ updated_time         │
---------------------
strstrstrstrboolstrstr
╞═════╪═════════════════════════════════╪═════════════════════════════════╪═════════════════════════════════╪═══════════════════╪══════════════════════╪══════════════════════╡
61e95da26-4018-4b76-adf9-34baaf… ┆ d9dab9af-24da-4fee-8b24-b85e68… ┆ d24b4651-29a5-4555-b0f3-b5989b… ┆ true              ┆ 2024-07-11T19:48:58Z2024-07-12T14:28:59Z
8   ┆ fb38db71-bef1-4f99-a9f7-63d0b8… ┆ cd63801b-cd57-4432-9745-46c471… ┆ d24b4651-29a5-4555-b0f3-b5989b… ┆ true              ┆ 2024-07-11T19:55:55Z2024-07-11T19:55:56Z
138f7b293d-2078-439a-bb0e-469e13… ┆ 2aa02212-e7ee-4689-966f-0bd8a1… ┆ 153a3d3f-e436-4502-8a5f-01fbb5… ┆ true              ┆ 2024-07-14T21:50:46Z2024-07-14T21:53:39Z
1668cfa104-f723-413f-8917-773be1… ┆ 2aa02212-e7ee-4689-966f-0bd8a1… ┆ 1331c0dc-fc97-464e-a97a-862a85… ┆ false             ┆ 2024-07-16T20:52:20Z2024-07-16T20:52:20Z
│ …   ┆ …                               ┆ …                               ┆ …                               ┆ …                 ┆ …                    ┆ …                    │
902e4edbe7-117b-417f-b915-1d3851… ┆ 2aa02212-e7ee-4689-966f-0bd8a1… ┆ 5b3c3c91-6c77-4403-9852-3dd3ba… ┆ true              ┆ 2024-08-05T21:41:25Z2024-08-05T21:41:31Z
└─────┴─────────────────────────────────┴─────────────────────────────────┴─────────────────────────────────┴───────────────────┴──────────────────────┴──────────────────────┘
sessions <- client$GET("v1/oauth/sessions?all=true")
> map_dfr(sessions, ~.x)
# A tibble: 24 × 7
   id    guid                                 user_guid                            oauth_integration_guid               has_refresh_token created_time         updated_time        
   <chr> <chr>                                <chr>                                <chr>                                <lgl>             <chr>                <chr>               
 1 6     1e95da26-4018-4b76-adf9-34baafc23504 d9dab9af-24da-4fee-8b24-b85e68955af7 d24b4651-29a5-4555-b0f3-b5989bfe40fe TRUE              2024-07-11T19:48:58Z 2024-07-12T14:28:59Z
 2 8     fb38db71-bef1-4f99-a9f7-63d0b80f21f2 cd63801b-cd57-4432-9745-46c471df803d d24b4651-29a5-4555-b0f3-b5989bfe40fe TRUE              2024-07-11T19:55:55Z 2024-07-11T19:55:56Z
 3 13    8f7b293d-2078-439a-bb0e-469e13d4d38d 2aa02212-e7ee-4689-966f-0bd8a195cfc7 153a3d3f-e436-4502-8a5f-01fbb5def977 TRUE              2024-07-14T21:50:46Z 2024-07-14T21:53:39Z
 4 16    68cfa104-f723-413f-8917-773be1bcbaca 2aa02212-e7ee-4689-966f-0bd8a195cfc7 1331c0dc-fc97-464e-a97a-862a85452490 FALSE             2024-07-16T20:52:20Z 2024-07-16T20:52:20Z
 ...

See also

  • For more details about the data returned in this recipe, Get OAuth session details in the API Reference.
  • See OAuth Integrations in the User Guide to learn more about how publishers and viewers interact with OAuth integrations.
  • See OAuth Integrations in the Admin Guide to for more detailed information on how to configure OAuth integrations.