Skip to content

Instantly share code, notes, and snippets.

@tanho63
Last active January 29, 2021 20:49
Show Gist options
  • Save tanho63/a9965e5c9c8ef99cb2d8cd4d58fc0040 to your computer and use it in GitHub Desktop.
Save tanho63/a9965e5c9c8ef99cb2d8cd4d58fc0040 to your computer and use it in GitHub Desktop.
Upload file to Microsoft Graph/Sharepoint API
# I struggled with uploading things to Microsoft Graph API/Sharepoint/OneDrive for business - here's the code that finally worked for me.
library(AzureGraph)
library(httr)
# Creates authentication token via browser
graph_token <- AzureGraph::create_graph_login()$token
# Send a PUT request to the Microsoft Graph API
AzureGraph::call_graph_url(graph_token,
"https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root:/filename.txt:/content",
body = httr::upload_file('path/filename.txt'),
encode = 'multipart',
http_verb = "PUT")
# I found Graph Explorer helpful for finding the correct site-id and drive-id (I was uploading to a Sharepoint subsite's document library and it took a while to find).
# httr:upload_file() encodes the file in a suitable way for uploading to Graph - and you need to pass encode = 'multipart' for it to work correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment