Graph Api File upload Powershell Issue

%3CLINGO-SUB%20id%3D%22lingo-sub-2785606%22%20slang%3D%22en-US%22%3EGraph%20Api%20File%20upload%20Powershell%20Issue%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2785606%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Folks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20am%20i%20missing%3F%20I%20want%20to%20upload%20a%20file%20to%20SharePoint%20Online%20library%20using%20PS%20and%20Graph%20API%20for%20some%20test%20purpose.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20able%20to%20create%20the%20AAD%20oAuth%20Token%3C%2FP%3E%3CP%3EI%20am%20able%20to%20access%20the%20site%20and%20it's%20lists%3C%2FP%3E%3CP%3EBut%20when%20i%20try%20to%20upload%20the%20a%20file%20to%20a%20library%2C%20i%20always%20get%20a%20bad%20request.%20Here%20is%20the%20code%20snippet%20below%3A-%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E----------------------------------------------------%3C%2FP%3E%3CP%3E%24path%20%3D%20%22D%3A%5CKnowledge%20Base%5CMyFile.xlsx%22%3C%2FP%3E%3CP%3E%24fileName%20%3D%20%22MyFile.xlsx%22%3CBR%20%2F%3E%24url%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fsites%2F%24siteID%2Fdrive%2Fitems%2F%24listId%2F%24fileName%2Fcontent%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fsites%2F%24siteID%2Fdrive%2Fitems%2F%24listId%2F%24fileName%2Fcontent%3C%2FA%3E%22%3CBR%20%2F%3E%24headers%20%3D%20%40%7B'Authorization'%20%3D%20%22Bearer%20%24oAuthToken%22%20%7D%3CBR%20%2F%3EInvoke-RestMethod%20-Uri%20%24url%20-Headers%20%24headers%20-Method%20Put%20-InFile%20%24path%20-ContentType%20'multipart%2Fform-data'%3C%2FP%3E%3CP%3E-----------------------------------------%3C%2FP%3E%3CP%3EThe%20above%20code%20throws%20error%3A%26nbsp%3BInvoke-RestMethod%20%3A%20The%20remote%20server%20returned%20an%20error%3A%20(400)%20Bad%20Request.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3EAni%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2785606%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Contributor

Hi Folks

 

What am i missing? I want to upload a file to SharePoint Online library using PS and Graph API for some test purpose. 

 

I am able to create the AAD oAuth Token

I am able to access the site and it's lists

But when i try to upload the a file to a library, i always get a bad request. Here is the code snippet below:-

 

----------------------------------------------------

$path = "D:\Knowledge Base\MyFile.xlsx"

$fileName = "MyFile.xlsx"
$url = "https://graph.microsoft.com/v1.0/sites/$siteID/drive/items/$listId/$fileName/content"
$headers = @{'Authorization' = "Bearer $oAuthToken" }
Invoke-RestMethod -Uri $url -Headers $headers -Method Put -InFile $path -ContentType 'multipart/form-data'

-----------------------------------------

The above code throws error: Invoke-RestMethod : The remote server returned an error: (400) Bad Request.

 

Thanks

Ani

 

 

0 Replies