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."

1 Answer 1

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... Commented Feb 24, 2020 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.. Commented Feb 24, 2020 at 12:24
  • Thank you for your help Ganesh & Lee..I was able to get the access token with the test approach you suggested. Commented Mar 5, 2020 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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