How to upload files to Sharepoint with WSO2 – complete guide

Upload files to Sharepoint

Uploading files to Sharepoint through REST API with OAuth2 authorizaton can be tricky as it requires a mixture of Sharepoint web site configuration and OAuth2 API calls to create access token.

It also uses constants and site variables you should find before using the mechanism.

Preparing Sharepoint environment

Getting Sharepoint

If you do not have a sharepoint access, go to and register for Microsoft 365 Business Premium trial.

NOTE: This creates a 30-day free trial account. No credit card necessary.

Login to your account and go to Sharepoint application. Mind your Sharepoint URL (this is the one you have chosen during registration).


Take hostname as a tenant

Creating an application

To authorize with OAuth2 you first need to create an application.

  1. Go to register new application (Settings / add application): https://{{tenant}}/_layouts/15/appregnew.aspx
  2. Set the following options:
  • App Type – Select An app running on a web server. (You may not have this option)
  • Client Id – Click Generate, and copy the generated value to a text file.
  • Client Secret – Click Generate, and copy the generated value to a text file.
  • Title – Enter a name for the app.
  • App Domain – Enter the domain name ( is a good example).
  • Redirect URL – Enter the Callback URL. It does not have to exist. Remember to use https and add a trailing slash if you use host name only ( is a good example).
  1. Click create. Save the response with all parameters to a text file, you will need it for accessing Sharepoint.

Getting the Realm

You will need the realm of your site to work with tokens.

Make a get request to your site without being logged in. You can use curl or Postman for that.

GET https://{{tenant}}

Authorization: Bearer realm="realm"

Get the Bearer realm component from the response header and save it as realm. You will need it for further calls.

Authorizing the application

Now comes the tricky part. You have to authorize the application to use Sharepoint. Here you have to decide what type of access you need.

To generate a code for creating a token you need to supply the scope. The scope defines which Sharepoint components you will able to see or write to. Common scopes are Web.Read for reading documents and Web.Write for reading and writting. You may have different tokens with different scopes to make sure the application is only permitted to perform authorized operations. If you know you need only to read files and will not write anything it’s good to create a read-only scope to guard against changes.

We will use a Web.Write scope as we are going to create folders and upload files.

Open your browser where you are logged in and go to:


Remember to change the client id and redirect uri to what you generated for the app. Mind the parameters should be url-encoded for the operation to succeed (client_id does not contain any characters to be encoded, but redirect uri should have a form of redirect_uri=https%3A%2F%2F127.0.0.1%2F)

Choose the required Sharepoint component if there is a list (choose Documents to work with folder and files) and click trust.

Do not bother seeing page not found as we used our local machine for the redirection. It is intentional not to pass the code to any application.

Save the code for generating the token. The code is everything after This will be a long string of characters, digits and other characters. Remember to take the whole string as the code. The code will be valid for ony 5 minutes, so you have to create an access token immediately

Creating an access token

You will use the code to create an access token to work with Sharepoint.

You need to POST a request to your realm URL:{{realm}}/tokens/OAuth/2

The request has to be passed as application/x-www-form-urlencoded (parameters like in GET request, all URL-encoded). You will need all previously saved variables. The resource principal for Sharepoint is 00000003-0000-0ff1-ce00-000000000000 (you have to use it as it is)

grant_type: "authorization_code"
client_id: "{{client_id}}@{{realm}}"
client_secret: "{{client_secret}}"
resource: "00000003-0000-0ff1-ce00-000000000000/{{tenant}}@{{realm}}"
code: "{{code}}"
redirect_uri: "{{redirect_uri}}"



Save the access_token and the refresh_token. The latter will be necessary to generate another access token when the first one expires.

From now on you will use the access token in the authorization header as follows:

Authorizaton: Bearer {{access_token}}

Working with files and folders

Remember to use the valid access token in each and every request.

Listing folders

You can list folders using a simple GET response.
To list subfolders in a standard Sharepoint documents folder, use:

GET https://{{tenant}}/_api/web/GetFolderByServerRelativeUrl(‘/Shared Documents’)

Listing files

To list files use similar endpoint URI ended with files.

GET https://{{tenant}}/_api/web/GetFolderByServerRelativeUrl(‘/Shared Documents’)/Files

Creating a folder

To create a folder you POST to the folders endpoint:

POST https://{{tenant}}/_api/web/folders

  "ServerRelativeUrl": "/Shared Documents/NewFolder1"

Uploading a file

To upload a file you use endpoint parameters to define folder path and file name as the body of the request will contain the file itself.

POST https://{{tenant}}/_api/web/GetFolderByServerRelativeUrl(‘/Shared Documents/NewFolder1′)/Files/add(url=’a.txt’,overwrite=true)


You can use Postman to issue request. All URLs and bodies are in Postman format to use with variables.

WSO2 Sharepoint connector

You can also use WSO2 Sharepoint connector


Need help?

Need to store files to Sharepoint and do not know where to start?

Do not worry, we are here to help. Drop us a line and we will contact you as soon as we can!

How to upload files to Sharepoint with WSO2 – complete guide
Scroll to top