使用 Microsoft Graph API 将文件上传到 SharePoint Online [英] Uploading a File to SharePoint Online using Microsoft Graph API

查看:70
本文介绍了使用 Microsoft Graph API 将文件上传到 SharePoint Online的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是 Microsoft Graph API 的新手,我使用 powershell 创建了一个脚本来从 Microsoft 365 获取报告,并将其保存在我的驱动器上 (c:\temp\reports.xlsx).

I'm really new to Microsoft Graph API, I created a script using powershell to get reports from Microsoft 365 and it is being saved on my drive (c:\temp\reports.xlsx).

保存后,我希望将其上传到 SharePoint Online.在阅读文档时,Microsoft 说要执行以下请求,

After it is being saved i wish to upload it to SharePoint online. On reading the docs, Microsoft says to do the following request,

PUT /sites/{site-id}/drive/items/{parent-id}:/{filename}:/content

然后我尝试将其应用于我的用例,这是我的要求:

I then tried to apply it to my use case and this was my request:

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 中创建的文件夹.但是在执行请求时我得到了这个:

The $File variable contains c:\temp\reports.xlsx the directory where my excel file is saved on getting the reports. The Test in my Url is a folder I created in Documents on my Site. But while doing the request I get this:

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 ...
  • in the provided question file is incorrectly addressed, since it needs to be uploaded under Test folder of Documents library, it could be addressed like this:
    /v1.0/sites/{tenant}.sharepoint.com/drive/root:/Test/reports.xslt:/content refer Addressing resources in a drive on OneDrive for a more details
  • the content of file for Upload endpoint is missing , it could be provided via -InFile parameter, e.g. 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 将文件上传到 SharePoint Online的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆