0

I am trying to POST new siteusers to <sp_url>/_api/web/sitegroups(6)/users. I have managed to do so using the chrome rest client application addon.

The request contains following headers and payload:

Accept: application/json;odata=verbose
X-RequestDigest: <formDigestValue>
Content-Type: application/json;odata=verbose 

{ '__metadata': { 'type': 'SP.User' }, 'LoginName' : 'i:0#.w|domain\\user' }

Now i tried to reproduce this request on my Java Spring server using Apache httpclient:

CredentialsProvider credsProvider = initCredsProvider();
    CloseableHttpClient client = HttpClients.custom()
            .setSSLSocketFactory(getSSLConnectionSocketFactory())
            .setDefaultCredentialsProvider(credsProvider)
            .build();
HttpPost postUser = new HttpPost(sp_host + "/_api/web/sitegroups(6)/users");
postUser.addHeader("Accept", "application/json;odata=verbose");
postUser.addHeader("Content-Type", "application/json;odata=verbose");
postUser.addHeader("X-RequestDigest", getFormDigestValue(client));
String s = "{'__metadata': { 'type': 'SP.User' }, 'LoginName':'i:0#.w|domain\\\\user'}";
postUser.setEntity(new StringEntity(s));
HttpResponse fdvResponse = client.execute(postUser);
String response = EntityUtils.toString(fdvResponse.getEntity());
logger.info("postUserResponse" + response);

But I get the following error:
Zugriff verweigert. Sie haben keine Berechtigung, diesen Vorgang auszuführen oder auf diese Ressource zuzugreifen.
Translation:
Access denied . You do not have permission to perform this action or to access this resource.

I have created several POST and GET requests like that before but never got that error.

Note: It seems like the Chrome Rest client automatically adds a few headers to the POST request:

Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 Accept-Encoding: gzip, deflate Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4

Any help will be appreciated!

1

Turned out that the user I was logged in with did not have permission to create new siteusers. I just gave him Admin-Rights and it worked :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.