使用 Microsoft Graph 将文件上传到 SharePoint Online API
Uploading a File to SharePoint Online using Microsoft Graph API
我真的是 Microsoft Graph 的新手 API,我使用 powershell 创建了一个脚本来从 Microsoft 365 获取报告,它被保存在我的驱动器上 (c:\temp\reports.xlsx
)。
保存后我希望将其上传到 SharePoint 在线。在阅读文档时,Microsoft 说要执行以下请求,
PUT /sites/{site-id}/drive/items/{parent-id}:/{filename}:/content
然后我尝试将它应用到我的用例中,这是我的要求:
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'
$File
变量包含 c:\temp\reports.xlsx
获取报告时保存我的 excel 文件的目录。 Url 中的 Test
是我在网站 Documents
中创建的文件夹。但是在执行请求时我得到了这个:
StatusCode: 400
StatusDescription: Bad Request
请帮忙
谢谢
变更列表:
- 提供的问题文件中的地址不正确,因为它
需要上传到
Documents
库的Test
文件夹下,它
可以这样解决:
/v1.0/sites/{tenant}.sharepoint.com/drive/root:/Test/reports.xslt:/content
参考 Addressing resources in a drive on OneDrive 了解更多
详情
Upload
endpoint的文件内容丢失,可能是
通过 -InFile
parameter 提供,例如Invoke-RestMethod -InFile $path ...
这是一个最小的例子:
$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'
我真的是 Microsoft Graph 的新手 API,我使用 powershell 创建了一个脚本来从 Microsoft 365 获取报告,它被保存在我的驱动器上 (c:\temp\reports.xlsx
)。
保存后我希望将其上传到 SharePoint 在线。在阅读文档时,Microsoft 说要执行以下请求,
PUT /sites/{site-id}/drive/items/{parent-id}:/{filename}:/content
然后我尝试将它应用到我的用例中,这是我的要求:
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'
$File
变量包含 c:\temp\reports.xlsx
获取报告时保存我的 excel 文件的目录。 Url 中的 Test
是我在网站 Documents
中创建的文件夹。但是在执行请求时我得到了这个:
StatusCode: 400
StatusDescription: Bad Request
请帮忙
谢谢
变更列表:
- 提供的问题文件中的地址不正确,因为它
需要上传到
Documents
库的Test
文件夹下,它 可以这样解决:
/v1.0/sites/{tenant}.sharepoint.com/drive/root:/Test/reports.xslt:/content
参考 Addressing resources in a drive on OneDrive 了解更多 详情 Upload
endpoint的文件内容丢失,可能是 通过-InFile
parameter 提供,例如Invoke-RestMethod -InFile $path ...
这是一个最小的例子:
$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'