简体   繁体   English

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

[英]Uploading a File to SharePoint Online using Microsoft Graph API

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 ).我对 Microsoft Graph API 真的很陌生,我使用 powershell 创建了一个脚本,以从 Microsoft 365 获取报告,它被保存在我的驱动器上 ( c:\temp\reports.xlsx )

After it is being saved i wish to upload it to SharePoint online.保存后,我希望将其上传到 SharePoint 在线。 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'

The $File variable contains c:\temp\reports.xlsx the directory where my excel file is saved on getting the reports. $File变量包含c:\temp\reports.xlsx获取报告时保存我的 excel 文件的目录。 The Test in my Url is a folder I created in Documents on my Site.我的 Url 中的Test是我在我的站点上的Documents中创建的文件夹。 But while doing the request I get this:但是在执行请求时,我得到了这个:

StatusCode: 400 
StatusDescription: Bad Request

Please Help请帮忙

Thanks谢谢

The list of changes:变更清单:

  • 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:在提供的问题文件中被错误地解决,因为它需要在Documents库的Test文件夹下上传,它可以这样解决:
    /v1.0/sites/{tenant}.sharepoint.com/drive/root:/Test/reports.xslt:/content refer Addressing resources in a drive on OneDrive for a more details /v1.0/sites/{tenant}.sharepoint.com/drive/root:/Test/reports.xslt:/content请参阅OneDrive 上驱动器中的寻址资源以获取更多详细信息
  • the content of file for Upload endpoint is missing , it could be provided via -InFile parameter , eg Invoke-RestMethod -InFile $path... Upload端点的文件内容丢失,可以通过-InFile参数提供,例如Invoke-RestMethod -InFile $path...

Here is a minimal example:这是一个最小的例子:

$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.

相关问题 使用图形 API 将大文件上传到 SharePoint Online - ContentType 错误 - Upload large file to SharePoint Online using Graph API - ContentType error 使用 Powershell 将文件上传到 Sharepoint Online (Microsoft 365)(选项 4 - 使用 Microsoft.SharePoint.Client.ClientContext) - Upload file to Sharepoint Online (Microsoft 365) using Powershell (Option 4 - Using Microsoft.SharePoint.Client.ClientContext) 使用 Powershell 将文件上传到 Sharepoint Online (Microsoft 365)(选项 2 - 使用 Microsoft.SharePoint.PowerShell) - Upload file to Sharepoint Online (Microsoft 365) using Powershell (Option 2 - Using Microsoft.SharePoint.PowerShell) Sharepoint Online 列表项查询使用 Microsoft Graph API 返回空数组 - Sharepoint Online list items query returns empty array with Microsoft Graph API 使用 Powershell(选项 1 - 使用 PnP.Powershell)将文件上传到 Sharepoint Online (Microsoft 365) - Upload file to Sharepoint Online (Microsoft 365) using Powershell (Option 1-Using PnP.Powershell) 使用PowerShell使用元数据将文件上载到SharePoint Online - Upload file to SharePoint Online with metadata using PowerShell 在Powershell 2.0中使用SharePoint Online客户端组件(Microsoft.SharePoint.Client) - Using SharePoint Online Client components (Microsoft.SharePoint.Client) with powershell 2.0 Powershell使用CSOM API从Sharepoint在线获取列表项 - Powershell get list items from Sharepoint online using CSOM API 使用 Graph API 管理 Microsoft Intune - Using Graph API to manage Microsoft Intune 尝试使用 powershell 访问 sharepoint 在线列表 REST API 时出错 - Error trying to access the sharepoint online list REST API using powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM