0

I want to access a SharePoint site with a Token, which I get by Username and PW (AcquireTokenByUsernamePassword). I get the Token and I can access the site via Postman without any problems, but in my code is an error.

Can you help me to find the error?

Here is my code so far:

    else
            {
                try
                {


                    var securePassword = new SecureString();

                    foreach (char c in {Password})
                    {
                        securePassword.AppendChar(c);
                    }


                    result = await app.AcquireTokenByUsernamePassword(scopes, "[email protected]", securePassword)
                        .ExecuteAsync();

                    var tokenSP = result.AccessToken;
                     var AccountName =   result.Account;


                    log.LogInformation($"Token : {tokenSP} {AccountName}");

                    var siteUrl = "https://xxx.sharepoint.com/sites/TestGetfolder";

                    try
                    {
                        ClientContext clientContext = new ClientContext(siteUrl);
                        
                        

                        clientContext.ExecutingWebRequest += (sender, args) =>
                        {
                            args.WebRequestExecutor.RequestHeaders["Authoritzation"] = "Bearer " + tokenSP;

                        };

                        

                        var web = clientContext.Web;

                        clientContext.Load(web, w => w.Title);
                        clientContext.ExecuteQuery();


                        log.LogInformation(web.Title);

                        

                    }
                    catch (Exception e)
                    {
                        log.LogInformation(e.ToString());

                    }




                }
                catch (MsalException e)
                {
                    Console.WriteLine(e);
                }
            }
            Console.WriteLine(result.Account.Username);




            return new OkObjectResult("End");
        }
    }
}

When I'm executing the code, I get the following error message:

Problem with Token

1
  • I solved the problem. I used the wrong Nuget Package. I needed to use the following: Microsoft.SharePointOnline.CSOM Version 16.1.23408.12000
    – Matthias
    Mar 11, 2023 at 19:23

1 Answer 1

0

Header name "Authoritzation" should be "Authorization"

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.