0

My end goal is getting an access token with which I can communicate to a SharePoint site and manage lists via CSOM (mostly upload documents into lists). I also need a refresh token to manage refreshing the tokens silently in the background.

There seems to be 2 ways of obtaining the credentials required for OAuth 2.0 with SharePoint Online. Via the: appregnew.aspx, and the appinv.aspx.

The "appregnew.aspx" gives you a ClientID and ClientSecret and seems to be independent from Azure AD.

The "appinv.aspx" seems to be dependant on Azure AD. You need to specify a ClientID which refers to an App registration that needs to have already been registered in the Azure AD.

I had been using a library which I had found online. This seems to include an implementation which is similar to what is explained here. This is still working in my localhost. However, when deployed to an Azure Web App I am getting the below error / partial stack trace:

"Token request failed" at Microsoft.IdentityModel.S2S.Protocols.OAuth2.OAuth2S2SClient.Issue(String securityTokenServiceUrl, OAuth2AccessTokenRequest oauth2Request) at SP.SiteCreatorWeb.TokenHelper.GetAccessToken(String authorizationCode, String targetPrincipalName, String targetHost, String targetRealm, String redirectUri, String inClientID, String inClientSecret) in C:\AppsGit\SharePointPowerHour\SP.SiteCreatorWeb\TokenHelper.cs:line 215

I searched the web about this error and some people suggested upgrading the .Net Framework, which I've upgraded to 4.8, and forcing TLS 1.2, which I also did. The issue still persisted.

I was also wondering about the "appinv.aspx" / app registration way. At some point I had managed to get this to work via the same library - I would create the app registration, possibly specify the SharePoint related API permissions, grant admin consent, then use the client ID to register the app registration within the "appinv.aspx" page. Then using the library linked above, I would be able to get an access token and use it with CSOM.

Today, I tried that same approach and I got a "Token request failed" error in the same method: Microsoft.IdentityModel.S2S.Protocols.OAuth2.OAuth2S2SClient.Issue.

I am also aware of the Authorization Code grant flow against Azure AD directly. Is the Azure AD token you retrieve from this process somehow usable with SharePoint? In the Azure AD App Registrations you can specify SharePoint related API permissions but not sure the OAuth process with Azure AD would be enough.

Any other ideas about the aforementioned issues would be appreciated. Also, it would be great if someone could direct me towards the "low-level" endpoints and payload examples that need to be called, to generate a SharePoint Online access token, and refresh the access token with a refresh token. It would be useful to find the same level of detail that one can find in the Azure AD article for the SharePoint Online OAuth 2.0 case. I prefer communicating directly with the endpoints, rather than using a library that obfuscates that part of the implementation. This is something I managed to find in all other cases of OAuth 2.0 that I ever had to work with, apart from SharePoint Online.

1 Answer 1

0

I managed to solve this but I am not entirely sure which one from a series of actions resolved the issue. First of all, as indicated in my initial post, I had already upgraded the framework from 4.5 to 4.8. However, I had a hunch that some of the NuGet references had not been properly refreshed. In this regard I uninstalled them and re-installed them from NuGet package manager.

I also noticed that when opening the library in Visual Studio I was getting a prompt to install the Office/SharePoint development toolset. This was presumably required as I recently started using Visual Studio 2022 - turns out I had missed the Office/SharePoint toolset when installing it. Not sure if this made a difference but I am listing it here just in case it helps anyone else!

Having said that, if anyone has any clue and can direct me towards the low-level endpoints & payload examples to communicate with SharePoint Online OAuth 2.0 to generate access/refresh tokens, that would still be most appreciated. I do not feel confident with the current approach and would rather skip the 3rd party libraries!

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.