SharePoint Online API – Get Access Token with Client ID and Secret – Part 2

In Part 1 of this post we created our SharePoint add-in and added permissions to it. We now can start using the SharePoint Online API.

With using PnP Framework library

Add the PnP Framework library nuget package to use the below code: https://www.nuget.org/packages/PnP.Framework

string siteUrl = "https://contoso.sharepoint.com/sites/demo";
using (var cc = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, "[Your Client ID]", "[Your Client Secret]"))
{
cc.Load(cc.Web, p => p.Title);
cc.ExecuteQuery();
Console.WriteLine(cc.Web.Title);
};

With using Postman

appReg_clientIdYour SharePoint add-in Client ID
realmYour SharePoint Tenant ID
principal00000003-0000-0ff1-ce00-000000000000
targetHost<domain_name>.sharepoint.com

With using .NET

private static void GetAccessToken()
{
try
{
SHAREPOINT_TOKEN = string.Empty;
WebRequest request = WebRequest.Create("https://accounts.accesscontrol.windows.net/&quot; + TENANT_ID + "/tokens/OAuth/2");
request.Method = "POST";
string postData = "grant_type=client_credentials" +
"&client_id=" + WebUtility.UrlEncode(CLIENT_ID + "@" + TENANT_ID) +
"&client_secret=" + WebUtility.UrlEncode(CLIENT_SECRET) +
"&resource=" + WebUtility.UrlEncode(SHAREPOINT_RESOURCE_ID + "/" + SHAREPOINT_DOMAIN + "@" + TENANT_ID);
_logger.LogInformation($"postData: {postData} ");
_logger.LogInformation("GetBytes: postData ");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
_logger.LogInformation("GetRequestStream: request ");
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
_logger.LogInformation("Calling: request.GetResponse ");
using WebResponse response = request.GetResponse();
_logger.LogInformation("GetResponseStream: response ");
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
_logger.LogInformation($"responseFromServer: {responseFromServer} ");
const string accessToken = "access_token\":\"";
int clientIndex = responseFromServer.IndexOf(accessToken, StringComparison.Ordinal);
int accessTokenIndex = clientIndex + accessToken.Length;
SHAREPOINT_TOKEN = responseFromServer.Substring(accessTokenIndex, (responseFromServer.Length – accessTokenIndex – 2));
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
throw;
}
}

One response to “SharePoint Online API – Get Access Token with Client ID and Secret – Part 2”

  1. […] using Postman appReg_clientId Your SharePoint add-in Client ID realm Your SharePoint Tenant … Continue reading SharePoint Online API – Get Access Token with Client ID and Secret – Part 2 Read Complete Post and Comments SBX – Two Col […]

Leave a reply to SharePoint Online API – Get Access Token with Client ID and Secret – Part 2 – Microsoft Dynamics 365 Community Cancel reply