Last active
November 17, 2024 17:21
-
-
Save tanho63/a9965e5c9c8ef99cb2d8cd4d58fc0040 to your computer and use it in GitHub Desktop.
Upload file to Microsoft Graph/Sharepoint API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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