10

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).

2
  • Have you looked at the authenticate() function in the httr package? Commented May 7, 2015 at 4:24
  • 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). Commented May 7, 2015 at 4:28

5 Answers 5

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. As I mentioned, using authenticate did work for me. What I was trying to find out was if there's a way that I don't need to type '"username"' or `"password", since that will expose the credentials to anyone who has access to the R script.
Oh I gotcha, I actually saved mine in a txt file on a file directory. Still a weak solution, I'm not sure if there's a way to us AD but I did this at large corp so not always easy to get that type of integration :)
Use the R secret package to save your password within an encrypted file and access it from R. Or use one of the many options for requesting passwords in a popup: stackoverflow.com/questions/14600099/…
2

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"))

Comments

0

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"))

Comments

0

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)

Comments

0

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"))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.