![](/img/trans.png)
[英]Upload large file to SharePoint Online using Graph API - ContentType error
[英]Uploading a File to SharePoint Online using Microsoft Graph API
我對 Microsoft Graph API 真的很陌生,我使用 powershell 創建了一個腳本,以從 Microsoft 365 獲取報告,它被保存在我的驅動器上 ( c:\temp\reports.xlsx
)
保存后,我希望將其上傳到 SharePoint 在線。 在閱讀文檔時,微軟表示要執行以下請求,
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
請參閱OneDrive 上驅動器中的尋址資源以獲取更多詳細信息Upload
端點的文件內容丟失,可以通過-InFile
參數提供,例如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'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.