繁体   English   中英

使用 Microsoft Graph 在线上传文件到 SharePoint API

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM