8
votes

I am trying to read data from my company intranet sharepoint.

require(httr)
url <- "http://<domain>/<path>/_vti_bin/ListData.svc/<something>"
r <- GET(url)

The problem is, access to Sharepoint uses Windows authentication. The above, expectedly, gave me 401 Unauthorized error.

How can I incorporate my windows authentication into the request in R, without typing my credential in clear text in the GET parameter? (using authenticate() with my credentials do work).

5
Have you looked at the authenticate() function in the httr package?MrFlick
Ah sorry I should have clarified that I'm trying to avoid typing my credential in plain text (and having to update it everytime the periodic password change policy is enforced).Ricky

5 Answers

2
votes

authenticate() is the right function to use, but you need to change the input as shown below:

require(httr)
url <- "http://<domain>/<path>/_vti_bin/ListData.svc/<something>"
r <- GET(url, authenticate("username","password",type="any"))

I'm not sure which specific type works, but I was able to establish a session using any. I found the solution in the httr package documentation.

https://cran.r-project.org/web/packages/httr/httr.pdf

2
votes

Get your site-id as outlined here. Get your list-id as outlined here. If you need to expand any list items, check this out.

And to make sure you don't pull your hair out and go bald like me, make sure that your item-level permissions for read access in the SharePoint site are set to "Read all items" (here's a reference).

library(AzureGraph)

gr <- create_graph_login()
me <- gr$get_user("me")
url <- "https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items"
r <- call_graph_url(me$token, url, http_verb = c("GET"))
0
votes

If authenticate works for you and your primary goal here is to not have your user and pw hard coded and exposed in the script, try using the package getPass, which has a function called getPass(msg="message you want to display") which opens up a prompt for you to enter in a string (which is masked for anyone watching your screen), and then returns that string.

Disclaimer: If you do it this way, whoever runs the script has to have security access and enter their own credentials for the script to work. In addition, if you're working in something like RStudio where your environment is displayed in a side panel, the values will be exposed to anyone looking over your shoulder:

require(httr)
require(getPass)

user <- getPass(msg="Enter User ID: ")
pw <- getPass(msg="Enter password: ")

url <- "http://<domain>/<path>/_vti_bin/ListData.svc/<something>"
r <- GET(url, authenticate(user,pw,type="any"))
0
votes

I am able to authenticate my session and turn a SharePoint list into a dataframe using a combination of functions from the RCurl and XML packages. This is the only method that is effective for me.

library(RCurl)
library(XML)
URL = "http://<domain>/<path>/_vti_bin/ListData.svc/<something>"
rawData = getURL(URL,userpwd=:"username:password")
xmlData = xmlParse(rawData)
items = getNodeSet(xmlData,"//m:properties")
df = xmlToDataFrame(items,stringsAsFactors=F)
0
votes

If on a company networking using windows authentication, the following will work without requiring to type user name and password:

library(httr)

url <- "http://<domain>/<path>/_vti_bin/ListData.svc/<something>"
r <- GET(url, authenticate(":", ":", "ntlm"))