【问题标题】:Uploading a File to SharePoint Online using Microsoft Graph API使用 Microsoft Graph API 将文件上传到 SharePoint Online
【发布时间】:2020-06-09 13:06:19
【问题描述】:

我真的是 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 是我在我的站点上的Documents 中创建的文件夹。但是在执行请求时,我得到了这个:

StatusCode: 400 
StatusDescription: Bad Request

请帮忙

谢谢

【问题讨论】:

    标签: powershell sharepoint microsoft-graph-api


    【解决方案1】:

    改动列表:

    • 在提供的问题文件中没有正确解决,因为它 需要上传到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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 2018-06-16
      • 2021-09-25
      • 2021-09-25
      • 2021-09-25
      相关资源
      最近更新 更多