Dear Community,
I am trying to make a REST call from a groovy script to a sharepoint API. Unfortunately, I struggle at the authentication, as it should use NTLM.
How do I specify, that the authentication method should be NTLM and not basic or bearer token?
This is my current code, which delivers a "401" response form the sharepoint.
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import groovy.json.JsonSlurper
import net.sf.json.groovy.JsonSlurper
import groovyx.net.http.Method
def http = new HTTPBuilder('http://base.url')
http.request(POST, JSON) {
requestContentType = ContentType.JSON
headers."Accept-Encoding" = "gzip, deflate, br"
headers."Accept" = "application/json;odata=verbose"
headers."Content-Type" = "application/json;odata=verbose"
headers."Connection" = "keep-alive"
body = [username: 'user', password: 'password']
response.success = { resp, JSON ->
return JSON
}
response.failure = { resp ->
return "Request failed with status ${resp.status}"
}
}
(Scriptrunner does not validate the code, whereas JWME just runs it fine and delivers the expreded "401".)
After a couple of hours and some significant help from @Sayed Bares (Appfire) and @Ram Kumar Aravindakshan _Adaptavist_ (Adaptavist), I was able to successfully authenticate against our sharepoint:
//step 1: NTLM authentication to sharepoint
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.RESTClient
def baseURL = 'server-url'
def sharepointAPI = 'resource_path'
def domain = 'xxx'
def username = 'yyy'
def password = 'zzz'
def restClient = new RESTClient( baseURL )
restClient.auth.ntlm(username, password, '', domain)
def response = restClient.post(
path: sharepointAPI,
contentType: "application/json;odata=verbose",
headers: ['Connection': 'keep-alive', 'Accept': 'application/json;odata=verbose'])
response.getData()
//step 2: parse JSON -> "FormDigestValue"
The response looks like this (JWME (Appfire)):
{d={GetContextWebInformation={__metadata={type=SP.ContextWebInformation}, FormDigestTimeoutSeconds=3600, FormDigestValue=0xB6F2D8BEAE813C24C386D8E24...4067630E1393782C0E,31 Jan 2022 14:43:52 -0000, LibraryVersion=16.0.5266.1000, SiteFullUrl=https://server-url.local, SupportedSchemaVersions={__metadata={type=Collection(Edm.String)}, results=[14.0.0.0, 15.0.0.0]}, WebFullUrl=https://server-url.local/help-desk/it}}}
The response looks like this (Scriptrunner):
[d:[GetContextWebInformation:[__metadata:[type:SP.ContextWebInformation], FormDigestTimeoutSeconds:3600, FormDigestValue:0xB6F2D8BEAE813C24C386D8E24...4067630E1393782C0E,31 Jan 2022 14:43:52 -0000, LibraryVersion:16.0.5266.1000, SiteFullUrl:https://server-url.local, SupportedSchemaVersions:[__metadata:[type:Collection(Edm.String)], results:[14.0.0.0, 15.0.0.0]], WebFullUrl:https://server-url.local/help-desk/it]]]
Now, I need to figure out, how to parse the response and get the value "FormDigestValue" into a variable.
The response is already JSON and Groovy parsed it into a LazyMap, which is Groovy's internal representation of a JSON object.
response.data.d.GetContextWebInformation.FormDigestValue
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.