1

I am trying to upload a file to SharePoint drive using graph API. I am doing OAuth using the below-mentioned link:

GET https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/authorize?client_id={CLIENT_ID}&scope=offline_access myfiles.write&response_type=code&redirect_uri=http://localhost:3000/sharepoint/integrate

After the authentication flow, I am getting both access token and refresh token. Using the access token I am able to fetch the drive list by calling the below-mentioned REST API endpoint:

GET https://graph.microsoft.com/v1.0/sites/root/drives

Headers:
Authorization: bearer {ACCESS_TOKEN}

I am also able to get the list of items present inside a specific drive by calling the below-mentioned REST API endpoint:

GET https://graph.microsoft.com/v1.0/sites/root/drives/{DRIVE_ID}/root/children/

Headers:
Authorization: bearer {ACCESS_TOKEN}

But when I am trying to upload a file to SharePoint drive using the below-mentioned REST API endpoint:

POST https://graph.microsoft.com/v1.0/sites/root/drives/{DRIVE_ID}/root/{FILE_NAME}:/content

Headers:
Authorization: bearer {ACCESS_TOKEN}
Content-Type: application/json;odata=verbose
Content-Length: FILE_SIZE

Data:
File Buffer

I am getting an error from the SharePoint API stating:

{
"error": {
    "code": "BadRequest",
    "message": "Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.",
    "innerError": {
        "date": "2021-07-26T12:27:31",
        "request-id": "d54e4e48-738b-4a5c-9499-7e8c6f35ced1",
        "client-request-id": "d54e4e48-738b-4a5c-9499-7e8c6f35ced1"
    }
}
}

Can someone tell me what I am doing wrong here. I am unable to figure it out, been stuck on it for a long time.

My code is in Node.JS, any help related to this would be appreciated.

Thank you in advance.

0

In your endpoint, you miss a colon character after root, and you need to change http method to put to upload files.

PUT https://graph.microsoft.com/v1.0/sites/root/drives/{DRIVE_ID}/root:/{FILE_NAME}:/content

Reference: https://docs.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.