问题
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).
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'
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
Please Help
Thanks
回答1:
The list of changes:
- in the provided question file is incorrectly addressed, since it
needs to be uploaded under Testfolder ofDocumentslibrary, it could be addressed like this:/v1.0/sites/{tenant}.sharepoint.com/drive/root:/Test/reports.xslt:/contentrefer 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 ...
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'
来源:https://stackoverflow.com/questions/62283355/uploading-a-file-to-sharepoint-online-using-microsoft-graph-api