Granting Access to a User

Problem

You need to grant a user access to a content item.

Solution

from posit import connect

CONTENT_GUID = "c92da1ad-9c48-420f-8e2a-dcd2d58f9723"
USER_GUID = "20a79ce3-6e87-4522-9faf-be24228800a4"
# Valid values are "viewer" and "owner" (collaborator)
ACCESS_TYPE = "viewer"

client = connect.Client()

# Add a user permission directly
user = client.users.get(USER_GUID)
client.content.get(CONTENT_GUID).permissions.create(user, role=ACCESS_TYPE)

# Add a user permission manually
client.content.get(CONTENT_GUID).permissions.create(
    principal_guid=USER_GUID,
    principal_type="user",
    role=ACCESS_TYPE,
)

# Confirm new permissions
client.content.get(CONTENT_GUID).permissions.find()
library(connectapi)

CONTENT_GUID <- "c92da1ad-9c48-420f-8e2a-dcd2d58f9723"
USER_GUID <- "20a79ce3-6e87-4522-9faf-be24228800a4"
# Valid values are "viewer" and "owner" (collaborator)
ACCESS_TYPE <- "viewer"

client <- connect()

# For the specified content item add the desired user
content <- content_item(client, CONTENT_GUID)
content_add_user(content, USER_GUID, role = ACCESS_TYPE)

# Confirm new permissions
get_content_permissions(content)