如何使用 R 从共享点文件夹下载文件

how to download files from a sharepoint folder with R

我公司的 sharepint 文件夹中有一些文件。我需要使用 r 下载该文件夹中的所有文件。我怎样才能用 R 做到这一点?

我用谷歌搜索了这个问题并找到了这个 link。这是关于将文件上传到共享点文件夹。 [Uploading files to SharePoint from R

来自 link 的代码如下所示:

saveToSharePoint <- function(fileName) 
  {
   cmd <- paste("curl --max-time 7200 --connect-timeout 7200 --ntlm --user","username:password", 
              "--upload-file /home/username/FolderNameWhereTheFileToTransferExists/",fileName, 
              "teamsites.OrganizationName.com/sites/PageTitle/Documents/UserDocumentation/FolderNameWhereTheFileNeedsToBeCopied/",fileName, sep = " ")
   system(cmd)
  }

 saveToSharePoint("SomeFileName.Ext")

这是关于上传一个特定的文件。但我需要的是从共享点文件夹下载文件。

所以,我修改了代码以从共享点文件夹复制。我将 --upload-file 更改为 --download-file

copyFromSharePoint <- function(fileName) 
      {
       cmd <- paste("curl --max-time 7200 --connect-timeout 7200 --ntlm --user","username:password", 
                  "--download-file teamsites.OrganizationName.com/sites/PageTitle/Documents/FolderNameWhereTheFileToTransferExists/",fileName, 
                  "home/username/UserDocumentation/FolderNameWhereTheFileNeedsToBeCopied/",fileName, sep = " ")
       system(cmd)
      }

copyFromSharePoint("SomeFileName.Ext")

但是,这似乎不起作用。以下错误消息:

curl: option --download-file: is unknown

有谁知道我如何用 R 做到这一点?

此外,如果我需要下载文件夹中的所有文件,而不是一个特定的文件怎么办?file.Does有人知道我如何用 R 做到这一点吗?

解决方案

我制作了一个名为 sharepointr 的简单 R 包来从 SharePoint 上传和下载文件。

我使整个 download/upload 从 R 到共享点工作的方式是:

  1. 进行 SharePoint 应用程序注册
  2. 添加应用程序注册权限
  3. 使用 cURL 获取“tenant_id”和“resource_id”
  4. 对 API
  5. 进行 get/post 调用

包里都有描述Readme.md

示例

# Installing the package
install.packages("devtools")
devtools::install_github("esbeneickhardt/sharepointr")

# Setting parameters
client_id <- "insert_from_first_step"
client_secret <- "insert_from_first_step"
tenant_id <- "insert_from_fourth_step"
resource_id <- "insert_from_fourth_step"
site_domain <- "yourorganisation.sharepoint.com"
sharepoint_url <- "https://yourorganisation.sharepoint.com/sites/MyTestSite"

# Getting a SharePoint Token
sharepoint_token <- get_sharepoint_token(client_id, client_secret, tenant_id, resource_id, site_domain)

# Getting sharepoint digest value
sharepoint_digest_value <- get_sharepoint_digest_value(sharepoint_token, sharepoint_url)

# Downloading a file
sharepoint_path <- "Shared Documents/test"
sharepoint_file_name <- "Mappe.xlsx"
out_path <- "C:/Users/User/Desktop/"
download_sharepoint_file(sharepoint_token, sharepoint_url, sharepoint_digest_value, sharepoint_path, sharepoint_file_name, out_path)