如何使用Graph API将本地文件上传到共享点?

时间:2020-06-17 19:14:53

标签: api sharepoint file-upload azure-ad-graph-api

我试图通过使用Graph API将本地PDF文件上传到共享点,但是PUT方法只是让我在SharePoint中创建新文件,而不是让我上传本地现有的C:/ Drive PDF文件< / p>

这是我正在使用的API

输入https://graph.microsoft.com/v1.0/drives/ {drive-id} / root:/ SharePointFilePath / file-name:/ content

2 个答案:

答案 0 :(得分:0)

按照此处,这是正确的端点。但是,您需要将文件作为二进制流发送到正文中。 https://docs.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http

这意味着您需要将本地pdf文件读取为二进制流,并将其发送到放置请求主体中的端点。作为测试,例如,您可以创建一个常规文本文件,其主体为字符串,内容类型为文本。

如果它大于4MB,则需要使用其他方法。 https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/driveitem_createuploadsession主要是一个上传会话。

答案 1 :(得分:0)

以前的测试脚本,用于通过PnP PowerShell将文件上传到SharePoint。

https://social.technet.microsoft.com/Forums/msonline/en-US/25191b51-41dd-4f4b-93aa-a46594c9a184/uploading-a-file-to-sharepoint-online-using-microsoft-graph-api?forum=onlineservicessharepoint

Connect-PnPOnline -AppId 'yourAzure app id' -AppSecret "yourAzure app secret" -AADDomain 'xxx.onmicrosoft.com'
$accessToken= Get-PnPAccessToken
$apiUrl = "https://graph.microsoft.com/v1.0/sites/xxx.sharepoint.com,x,x/drives/driveid/items/01AKXHS4ELSEOTLPZHZZDYYBOW57WR6HK6:/test.xlsx:/content"
$path="C:\Lee\Script\testdata.xlsx"
$file=[IO.File]::ReadAllBytes($path)
Invoke-RestMethod -Headers @{Authorization = "Bearer $accessToken"} -Uri $apiUrl -Method Put -Body $file -ContentType "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"