1

For the past few days, I tried all the ways possible. But couldn't get the access token for accessing Sharepoint online addin using c# console application. However I was able to get the access token from POSTMAN.

I couldn't get any reasonable answer over the internet. I am framing a webrequest and passing the details like Client ID, Client Secret, Tenant, Resource etc similar to framing a POSTMAN request but always getting 401 Unauthorized.

Always getting this message:"The remote server returned an error: (401) Unauthorized."

0

Try demo in my previous test thread.

App Only Token Issue: Unauthorized access

Uri siteUri = site url;

//Get the realm for the URL
string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);

//Get the access token for the URL.  
//   Requires this app to be registered with the tenant
string accessToken = TokenHelper.GetAppOnlyAccessToken(
    TokenHelper.SharePointPrincipal,
    siteUri.Authority, realm).AccessToken;

//Get client context with access token
using (var clientContext =
    TokenHelper.GetClientContextWithAccessToken(
        siteUri.ToString(), accessToken))
{
    var web = clientContext.Web;
    var lib = web.Lists.GetByTitle("MyDoc");
    var item = lib.GetItemById(3);
    var file = item.File;
    clientContext.Load(file);
    clientContext.ExecuteQuery();
    Console.WriteLine(file.Name);
    Console.ReadKey();
}
3
  • Thank you...I will try this and let you know... Feb 24 '20 at 5:57
  • Tried installing similar package as per your test..but get this error...The package 'AppForSharePointOnlineWebToolkit' tried to add a framework reference to 'Microsoft.IdentityModel.Extensions' which was not found in the GAC. This is possibly a bug in the package. Please contact the package owners for assistance. Reference unavailable...can you suggest if this package is the correct one...as already tried installing to GAC.. Feb 24 '20 at 12:24
  • Thank you for your help Ganesh & Lee..I was able to get the access token with the test approach you suggested. Mar 5 '20 at 2:39

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.