ToSkyX
ToSkyX

Reputation: 11

Getting" Token request failed" with CSOM PnP Framework and .NET 6

I am using Pnp Framwork with SharePoint online to retrieve elements of a list with .Net 6 WPF.

I am creating a client context and requesting data using an app id and secret.

The app registration has all the permissions on the site and is not expired.

However I get an error when it comes to "ExecuteQueryRetry" with the following error message : "Token request failed".

The thing is that most of the time I get data with no errors, and sometimes I randomly get this error.

I am calling this method each 5 minutes using an EventHandler.

The userName parameter is received as a parameter of my method and has a valid value.

Below the code of the method :

using (ClientContext ctx = new PnP.Framework.AuthenticationManager().GetACSAppOnlyContext(ConfigurationManager.AppSettings["SharePointSite"], ConfigurationManager.AppSettings["SharePointAppID"], ConfigurationManager.AppSettings["SharePointAppSecret"]))
            {
                Web web = ctx.Web;

                List myList = web.Lists.GetByTitle("MyList");
                ctx.Load(myList);
                ctx.ExecuteQueryRetry(5);
                CamlQuery caml = new()
                {
                    ViewXml = "<View><Query><Where><And>" +
                             "<Eq>" +
                               "<FieldRef Name=\"nomUtilisateur\"/>" +
                                "<Value Type=\"Text\">" + userName + "</Value>" +
                             "</Eq>" +
                             "<Eq>" +
                               "<FieldRef Name=\"DateDeclaration\"/>" +
                                "<Value IncludeTimeValue = 'False' Type=\"DateTime\">" + DateTime.Now.Date.ToString("yyyy-MM-dd") + "</Value>" +
                             "</Eq>" +
                             "</And>" +
                            "</Where></Query></View>"
                };

                ListItemCollection myListItems = myList.GetItems(caml);
                ctx.Load(myListItems );
                ctx.ExecuteQueryRetry(5);

Any ideas ?

Thanks in advance.

Upvotes: 1

Views: 2196

Answers (2)

Ekus
Ekus

Reputation: 1880

I know the OP mentioned that the app registration is not expired, but maybe this helps those googling the same error "Token request failed".

In my case I had to re-register our app after one year, I just went to the special "appregnew.aspx" page in the Sharepoint site, e.g. https://company.sharepoint.com/sites/Project1/_layouts/15/appregnew.aspx and registered our client app again to generate a new client id and secret id.

Then I went to the "appinv.aspx" page e.g. https://company.sharepoint.com/sites/Project1/_layouts/15/appinv.aspx and pasted the client id into the app id field, clicked "Lookup" to load the rest of the registration, and added a liberal registration request XML, e.g.

<AppPermissionRequests AllowAppOnlyPolicy="true">
        <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl"/>
        <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="FullControl"/>
        <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web/list" Right="FullControl"/>
</AppPermissionRequests>

Of course afterwards I had to update the client id and secret in our app.

Upvotes: 0

It is necessary to look at the exception message, because in my case I had a solution that had two projects and after installing the pnp framework the solution was with newtonsoft.json v12 and in the other project it was with newtonsoft.json v10,

I discovered this by the message from exception:

"Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, 
PublicKeyToken=30ad4fe6b2a6aeed' or one of the important dependencies. The assembly 
manifest definition does not match the reference to the assembly. ( Exception from 
HRESULT: 0x80131040)":"Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, 
PublicKeyToken=30ad4fe6b2a6aeed"

after discovering this I updated them both to have v12 and it worked.

And verify if your machine have TLS 1.2 Active, Win 10 is Default tls 1.2 actived

Upvotes: 0

Related Questions