Users
Create a Posit Connect user from LDAP or OAuth2
The following snippets search for a user in LDAP or OAuth2 and then create a Posit Connect account for that user.
Workflow
There are three steps:
- Search for the user via the
GET /v1/users/remote
endpoint. - Note the
temp_ticket
for the desired user account. - Use the
PUT /v1/users
endpoint with thetemp_ticket
to create a corresponding account on Connect.
Search for a user
Search for the user via the GET /v1/users/remote
endpoint:
library(httr)
# The CONNECT_SERVER URL must have a trailing slash.
<- Sys.getenv("CONNECT_SERVER")
connectServer <- Sys.getenv("CONNECT_API_KEY")
connectAPIKey
# set the search parameter
<- "julie"
prefix
# make the query request
<- GET(
response paste0(connectServer, "__api__/v1/users/remote"),
add_headers(Authorization = paste("Key", connectAPIKey)),
query = list(prefix = prefix)
)
<- content(response)$results
results
# print the results of the API call
<- function(guid) {
formatGuid if (is.null(guid)) {
"NULL"
else {
}
guid
}
}
cat(sprintf("FIRST\tLAST\tUSERNAME\tGUID\n"))
for (user in results) {
cat(
sprintf("%s\t%s\t%s\t\t%s\n",
$first_name,
user$last_name,
user$username,
userformatGuid(user$guid)
)
) }
The output looks like the following:
FIRST LAST USERNAME GUID
Julie Goolly julie1 15f5f51d-08ff-4e5b-beba-4ccf24e248dd Julie Jolly julie2 NULL
Let’s break this down:
- In this particular case, there are two users matching the search for the prefix
julie
:julie1
julie2
- The user
julie1
has aGUID
value, which means that this user already has a Connect account. - The user
julie2
does not have aGUID
value, which means that this user does not have a Connect account.
Additionally, included in the API response for each user is a temp_ticket
value that can be used to give the user a Connect account. In the example above, the second user, julie2
, needs an account, so you need that user’s temp_ticket
:
<- results[[2]]$temp_ticket tempTicket
You can use this tempTicket
value in the next section to create the account.
Create a Posit Connect user account
Using the tempTicket
value from the previous section, you can give the user a Connect account with a PUT /v1/users
request:
library(httr)
# The CONNECT_SERVER URL must have a trailing slash.
<- Sys.getenv("CONNECT_SERVER")
connectServer <- Sys.getenv("CONNECT_API_KEY")
connectAPIKey
# The 'tempTicket' value comes from an earlier /users/remote search.
<- PUT(
response paste0(connectServer, "__api__/v1/users"),
add_headers(Authorization = paste("Key", connectAPIKey)),
body = list(temp_ticket = tempTicket),
encode = "json"
)
print(content(response))
When the call succeeds, the response contains a non-NULL GUID
value, which is a unique identifier for the user account.
If the user already exists in Connect, the response will contain an error:
$error [1] "The requested username is already in use."