Third-party tools need "token"(OAuth 2.0) to get authenticated. And the token is generated based on "Client Id" and "Client Secret (key)". We can do this either manually (Postman or Fiddler), or programmatically (C#, JavaScript, etc.). This is how our cloud based application to run across different cloud platforms.
You can get more details of
SharePoint OAuth 2.0 here.
For SharePoint Online, we have two options to get the token, that depends on what type of admin rights we have and what we need.
There are already some pretty good posts tell us how to do it. However, I found some description is confusing, especially about the naming of some parameters. So, I try to explain it here, based on my understanding.
The GUIDs and Keys in the sample code below are all generated randomly.
- In tenant scope (need Tenant, Global or AAD admin rights)
We can follow
this post. It contains 6 steps.
====== 1. Register app ======
Go to azure portal site through web browser:
https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps
Display name: PostMan
Application type: Web app / API
Home page (Sign-on URL): https://www.getpostman.com/oauth2/callback
Application ID (Client ID of PostMan, auto-generated): 7f925812-d466-4c46-8737-0fcc1e172a98
Object ID (not used, auto-generated): 864dc037-153d-4097-8105-0454bf3042fd
Managed application in local directory
====== 2. Set permissions ======
Go to the settings of this app, then click "required permission"
Set permissions as needed, such as:
Office 365 SharePoint Online -> Delegated permissions -> Read items in all site collections
====== 3. Generate Key (Client Secret) ======
test, 27/06/2020, abcd/efghijklmnopqrstuv4yWLFWswZJGHlm9UFDp0cU=
Copy the key to a safe place. This key will be used to get the token.
====== 4. Access SPO through restful API ======
Launch PostMan,
https://<company name>
.sharepoint.com/sites/test2/_api/web/lists
Get
Headers
Key Syntax Value
Accept
application/json; odata=verbose
application/json; odata=verbose
====== 5. Get the Oauth 2.0 Bearer Token ======
Get New Access Token
Callback URL: https://www.getpostman.com/oauth2/callback
Auth URL : https://login.microsoftonline.com/common/oauth2/authorize?resource=https%3A%2F%2Fkenowau.sharepoint.com
Access Token URL : https://login.microsoftonline.com/common/oauth2/token
Client ID : 7f925812-d466-4c46-8737-0fcc1e172a98
Client Secret (Key) : abcd/efgsdksFME6u4yWLFWswZJGHlm9asdfasdflk=
Grant Type : Authorization Code
Click "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YWRXcml0ZS5BbGwiLCJzdWIiOiJPNkZtZVNnVk1ieUxXcUUtVVFhLUNxRUdxZ0ZOZW9adrwevzvzxcvcvzxVlX25hbWUiOiJlcmljZmFuZ0BrZW5vd2F1Lm9ubWljcm9zb2Z0LmNvbSIsInVwbiI6ImVyaWNmYW5nQGtlbm93YXUub25taWNyb3NvZnQuY29tIiwidXRpIjoibzY1WFZNOG53MGlWTFZxZnNvRU9BQSIsInZlciI6IjEuMCJ9.LjxUSSWnsMTzk1Mj4Y5xn2X9Q4arUxb1Tp1FDvQqckOYIlLhg8WPg0LcAOvQVBiTA3U9IkedpXaqfre6rvycj8OZI7a6UY3YUoppJMyZ9VmmvDuDHZVIawwIk61XBQGzfVrbRu5w9BJzrbTwJCw-zlGWxbtnx_Acvz1D8kPmsWKNP7OUCVjB9hlqdBx-wAwofKxNRuJRKzIcixHhwBAveNs9MoAvn-hQ3qLIuckkW6zyjhFAqo7C_n-3Gsu_ajvin0uIbEK2G_I3SqtEMOBa9ZMdCC4aq9Mlu9AADnBYMua_29-f5SoBXy1OIfjEasdfwer35asefyuksyhtBBkW1Chog

Click "Use Token"
====== 6. Get the Response Body ======
Click "Send" button. Done.
- In site collection scope (need SharePoint or Site Collection admin rights)
We can follow
this post. It contains 5 steps. (I choose an alternative way in step 3)
====== 1. Register app ======
Go to SPO site through web browser:
https://<company name>.SharePoint.com/sites/test1/_layouts/15/appregnew.aspx
The app identifier has been successfully created.
Client Id: 71b53e1e-6260-4a8d-8423-8ca65439271a
Client Secret: SbyJ/JghRiadfdasfadsLFWswZJGHlm9UFDp0cU=
Title: postman
App Domain: localhost
Redirect URI: https://localhost
Copy the Client Secret (key) to a safe place. This key will be used to get the token.
Click "OK"
====== 2. Grant permission to this App ======
https://<company name>.SharePoint.com/sites/test1/_layouts/15/appinv.aspx
Read-Only rights of the sub site:
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="Read" />
</AppPermissionRequests>
Full-Control rights of the whole site collection:
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" />
</AppPermissionRequests>
====== 3. Get SPO Tenant GUID, resource GUID and client GUID ======
Instead of PostMan, it's much easier to get the information from this page: https://<company name>.sharepoint.com/sites/test1/_layouts/appprincipals.aspx
The app identifier syntax is: i:0i.t|ms.sp.ext|AppGUID@TenantGUID
"resource GUID" means the app GUID of SharePoint Online itself, which is: 00000003-0000-0ff1-ce00-000000000000
====== 4. Get Bearer token ======
Launch PostMan,
https://accounts.accesscontrol.windows.net//tokens/OAuth/2
Post
Headers
Key Value
Content-Type application/x-www-form-urlencoded
Body
Key Value
grant_type client_credentials
client_id <ClientID>@<TenantID>
client_secret SbyJ/JghRiadfdasfadsLFWswZJGHlm9UFDp0cU=
resource 00000003-0000-0ff1-ce00-000000000000/<company name>.sharepoint.com@<TenantGUID>
Click "Send". Copy "access_token" to a safe place.
====== 5. Access SPO through restful API ======
Launch PostMan,
https://<company name>.sharepoint.com/sites/test2/_api/web/lists
Get
Headers
Key Value
Accept application/json;odata=verbose
Authorization Bearer
Click "Send"
Done.
The procedure is similar with C# or Javascript.