[英]Uploading a File to SharePoint Online using Microsoft Graph API
I'm really new to Microsoft Graph API, I created a script using powershell to get reports from Microsoft 365 and it is being saved on my drive ( c:\temp\reports.xlsx
).我对 Microsoft Graph API 真的很陌生,我使用 powershell 创建了一个脚本,以从 Microsoft 365 获取报告,它被保存在我的驱动器上 (
c:\temp\reports.xlsx
)
After it is being saved i wish to upload it to SharePoint online.保存后,我希望将其上传到 SharePoint 在线。 On reading the docs, Microsoft says to do the following request,
在阅读文档时,微软表示要执行以下请求,
PUT /sites/{site-id}/drive/items/{parent-id}:/{filename}:/content
I then tried to apply it to my use case and this was my request:然后我尝试将它应用到我的用例中,这是我的要求:
function RestMethod {
Param (
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$Request,
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$RequestMethod,
[parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[String]$Body
)
$Headers = @{
Authorization = "$($global:RequestToken.token_type) $($global:RequestToken.access_token)"
}
$RestResults = $null
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
try {
$RestResults = Invoke-RestMethod -Uri $Request -Headers $Headers -Method $RequestMethod -ContentType "application/json"
} catch {
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
}
return $RestResults
}
$upLoadRequest = "https://graph.microsoft.com/v1.0/sites/tenant.sharepoint.com/drive/items/Test:/$($File):/content"
$upLoadResults = RestMethod $upLoadRequest 'PUT'
The $File
variable contains c:\temp\reports.xlsx
the directory where my excel file is saved on getting the reports. $File
变量包含c:\temp\reports.xlsx
获取报告时保存我的 excel 文件的目录。 The Test
in my Url is a folder I created in Documents
on my Site.我的 Url 中的
Test
是我在我的站点上的Documents
中创建的文件夹。 But while doing the request I get this:但是在执行请求时,我得到了这个:
StatusCode: 400
StatusDescription: Bad Request
Please Help请帮忙
Thanks谢谢
The list of changes:变更清单:
Test
folder of Documents
library, it could be addressed like this:Documents
库的Test
文件夹下上传,它可以这样解决:/v1.0/sites/{tenant}.sharepoint.com/drive/root:/Test/reports.xslt:/content
refer Addressing resources in a drive on OneDrive for a more details /v1.0/sites/{tenant}.sharepoint.com/drive/root:/Test/reports.xslt:/content
请参阅OneDrive 上驱动器中的寻址资源以获取更多详细信息Upload
endpoint is missing , it could be provided via -InFile
parameter , eg Invoke-RestMethod -InFile $path...
Upload
端点的文件内容丢失,可以通过-InFile
参数提供,例如Invoke-RestMethod -InFile $path...
Here is a minimal example:这是一个最小的例子:
$access_token = "--access token goes here--"
$path = "--path to local file, e.g. c:\data\report.xlsx--"
$url = "https://graph.microsoft.com/v1.0/sites/{tenant}.sharepoint.com/drive/root:/{folder}/{filename}:/content"
$headers = @{'Authorization' = "Bearer $access_token" }
Invoke-RestMethod -Uri $url -Headers $headers -Method Put -InFile $path -ContentType 'multipart/form-data'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.