0

I want to add users to groups on nextcloud via their API.

I succeeded with adding one user (ncuser) to Group1 and Group2 from my CLI, by running this request:

curl -X POST https://adminuser:'admin pass phrase'@cloud.example.org/ocs/v1.php/cloud/users/ncuser/groups -d groupid="Group1" -d groupid="Group2" -H "OCS-APIRequest: true"

With the following response:

<?xml version="1.0"?>
<ocs>
 <meta>
  <status>ok</status>
  <statuscode>100</statuscode>
  <message>OK</message>
  <totalitems></totalitems>
  <itemsperpage></itemsperpage>
 </meta>
 <data/>
</ocs>

I attempted to run the request in Rstudio with this code:

library(curl)
library(httr)

call <- POST("https://adminuser:'admin pass phrase'@cloud.example.org/ocs/v1.php/cloud/users/ncuser/groups",
     body = list(groupid = "Group1", groupid = "Group2"),
     add_headers('OCS-APIRequest' = "true", 'Content-Type' = "application/x-www-form-urlencoded"))

But this isn't successful. I get this response in R:

Response [https://adminuser:'admin pass phrase'@cloud.example.org/ocs/v1.php/cloud/users/ncuser/groups]
  Date: 2020-03-22 15:43
  Status: 401
  Content-Type: application/json; charset=utf-8
  Size: 140 B

Here is the content of my R POST request:

content(call)

$ocs
$ocs$meta
$ocs$meta$status
[1] "failure"

$ocs$meta$statuscode
[1] 997

$ocs$meta$message
[1] "Current user is not logged in"

$ocs$meta$totalitems
[1] ""

$ocs$meta$itemsperpage
[1] ""


$ocs$data
list()

It seems like I wasn't able to login through the POST() url, and need to add adminuser and 'admin pass phrase' in some other way, but i am not sure how.

Also, the nextcloud API documentation states that OCS-APIRequest and the content type needs to be specified in the header, but I am not sure if i did that correctly.

All calls to OCS endpoints require the OCS-APIRequest header to be set to true.

All POST requests require the Content-Type: application/x-www-form-urlencoded header. (Note: Some libraries like cURL set this header automatically, others require setting the header explicitly.)

How should my POST request be written in R so that I can successfully add my nextcloud users to groups?

Any help would be highly appreciated.

Community
  • 1
  • 1
Nowak
  • 135
  • 2
  • 14

1 Answers1

2

Maybe try

call <- POST("https://cloud.example.org/ocs/v1.php/cloud/users/ncuser/groups",
     body = list(groupid = "Group1", groupid = "Group2"),
     add_headers('OCS-APIRequest' = "true"),
     authenticate("adminuser", "admin pass phrase"),
     encode = "form")

This will make sure POST encode the body properly and will more safely pass the username/password. You can also try adding in verbose() to get more output. It's not going to be easy to help you without some sort of reproducible example we can use for testing.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • The request goes through, but it only adds the user to Group2. I tried `body = list(groupid = c("Group1", "Group2")` but I got this error: `Error in vapply(elements, encode, character(1)) : values must be length 1, but FUN(X[[1]]) result is length 2` – Nowak Mar 22 '20 at 20:06
  • I would love to give you a reproducible example, but I don't have access to a nextcloud server that I can give personal API token access to. – Nowak Mar 22 '20 at 20:57