使用Microsoft Graph API向SharePoint Online上传文件

问题描述 投票:0回答:1

我对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文件在获取报表时保存的目录。该 Test 在我的Url中是我在我的网站上创建的一个文件夹。Documents 在我的网站上。但是当我进行请求时,我得到了这个。

StatusCode: 400 
StatusDescription: Bad Request

请帮助我

谢谢你

powershell sharepoint microsoft-graph
1个回答
1
投票

变化列表。

  • 在所提供的问题文件中的变化列表:是不正确的,因为它需要上传的下 Test 文件夹 Documents 库,可以这样处理。/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'
© www.soinside.com 2019 - 2024. All rights reserved.