get valid auth token from MSAL for SP Online?

%3CLINGO-SUB%20id%3D%22lingo-sub-354239%22%20slang%3D%22en-US%22%3Eget%20valid%20auth%20token%20from%20MSAL%20for%20SP%20Online%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-354239%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20in%20the%20middle%20of%20writing%20a%20.Net%20client%20app%20and%20have%20been%20using%20the%20latest%20Graph%20SDK%20to%20access%20OneDrive.%26nbsp%3B%20To%20authenticate%2C%20I%20used%20MSAL%20and%20with%20the%20appropriate%20%22scopes%22%2C%20this%20gets%20me%20an%20OAuth%20token%20that%20works%20great%20for%20OneDrive%20access.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUnfortunately%2C%20the%20Graph%20API%20(REST%20and%20SDK)%20are%20far%20from%20complete%20at%20this%20time.%26nbsp%3B%20I%20need%20to%20supplement%20my%20Graph%20calls%20with%20some%20more%20traditional%20ECM%20where%20OneDrive%20for%20Business%20and%20straight%20SP%20Online%20Libraries%20are%20concerned%20(calls%20like%20checkout%2Fcheckin%2Flocking%20and%20related%20properties%20like%20who%20a%20file%20is%20checked-out%20to%2C%20etc.)%26nbsp%3B%20To%20do%20this%2C%20I%20thought%20I'd%20leverage%20%22SharePoint%20CSOM%20for%20Online%22%20until%20Graph%20%22catches%20up%22.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHowever%2C%20the%20access%20token%20received%20via%20MSAL%20is%20refused%20by%20the%20ClientContext%20of%20the%20user's%20site%2Flist.%26nbsp%3B%20It%20always%20results%20in%20a%20401%3A%20Unauthorized%20being%20returned%20from%20the%20service.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDoes%20anyone%20have%20a%20code%20snippet%20or%20tips%2Ftricks%20to%20use%20MSAL%20to%20get%20a%20valid%20access%20token%20for%20the%20user's%20same%20SP%20Library%20-%20just%20connecting%20directly%20to%20the%20SP%20Online%20services%3F%26nbsp%3B%20I%20could%20be%20missing%20explicit%20%22scopes%22%20to%20request%20in%20the%20MSAL%20authentication%20too.%26nbsp%3B%20I%20am%20getting%20the%20feeling%20that%20MSAL%20is%20not%20compatible%20with%20SP%20Online%2C%20and%20that%20would%20be%20really%20unfortunate.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20any%20pointers!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-356895%22%20slang%3D%22en-US%22%3ERe%3A%20get%20valid%20auth%20token%20from%20MSAL%20for%20SP%20Online%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-356895%22%20slang%3D%22en-US%22%3EI%20have%20seen%20that%20post%20before.%20In%20fact%2C%20I%20spent%20quite%20some%20time%20searching%20and%20reading%20before%20posting%20here...%3CBR%20%2F%3E%3CBR%20%2F%3EThat%20post%20uses%20ADAL%20assembly%2Flibrary.%20From%20what%20I%20have%20read%2C%20it%20is%20the%20first%20gen%20support%20of%20AD%20authentication%20developed%20by%20MSFT.%20While%20it%20is%20still%20being%20worked-on%2C%20and%20probably%20worked%20_with_%20by%20many%20projects%2C%20we%20have%20started%20with%20MSAL%2C%20which%20appears%20to%20be%20the%20eventual%20replacement%20for%20ADAL.%20(Thought%20was%2C%20if%20we're%20starting%20work%20%22today%22%2C%20we%20should%20use%20MSFT's%20latest%20library%20and%20follow%20its%20direction.)%3CBR%20%2F%3E%3CBR%20%2F%3EMSAL%20works%20great%20to%20get%20OAuth%20tokens%20for%20Graph%20-%20we%20have%20that%20working.%20We%20can't%20seem%20to%20get%20MSAL's%20AD%20tokens%20to%20work%20with%20SP%20Online.%20Integrating%20another%20auth%20library%20(especially%20one%20with%20very%20similar%20classes%20and%20methods)%20would%20be%20a%20nuisance%20and%20cause%20confusion%20and%20reduce%20maintainability.%20Thus%2C%20I'm%20trying%20to%20figure%20out%20how%20to%20get%20MSAL%20to%20do%20the%20same.%3CBR%20%2F%3E%3CBR%20%2F%3EThank%20you%20for%20your%20reply.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-356853%22%20slang%3D%22en-US%22%3ERe%3A%20get%20valid%20auth%20token%20from%20MSAL%20for%20SP%20Online%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-356853%22%20slang%3D%22en-US%22%3E%3CP%3EHope%2C%20the%20below%20post%20will%20help..%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fsamlman.wordpress.com%2F2015%2F02%2F27%2Fusing-adal-access-tokens-with-o365-rest-apis-and-csom%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsamlman.wordpress.com%2F2015%2F02%2F27%2Fusing-adal-access-tokens-with-o365-rest-apis-and-csom%2F%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBTW%20%3A%20I%20have%20not%20personally%20tested%20the%20content%20listed%20in%20above%20post%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-752447%22%20slang%3D%22en-US%22%3ERe%3A%20get%20valid%20auth%20token%20from%20MSAL%20for%20SP%20Online%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-752447%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F288005%22%20target%3D%22_blank%22%3E%40AJWeber%3C%2FA%3E%26nbsp%3BThis%20is%20a%20really%20big%20issue%20for%20me%20right%20now%20as%20well...%20Building%20business%20apps%20with%20Graph%20works%20great%20until%20you%20run%20into%20some%20incomplete%20functionality.%26nbsp%3B%20For%20example%20creating%20a%20SharePoint%20site%2C%20or%20retrieving%20a%20SharePoint%20user%20id%20so%20that%20you%20can%20update%20a%20multiuser%20list%20field.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20active%20development%20under%20the%20beta%20graph%3F%20v1.0%20after%20a%20couple%20years%20does%20not%20look%20like%20a%20good%20sign.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20anyone%20out%20there%20has%20already%20solved%20the%20issue%20of%20integrating%20CSOM%20or%20direct%20REST%20calls%20to%20SharePoint%20in%20a%20Microsoft.Graph%20MSAL%20application%20that%20would%20be%20a%20big%20help!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20stick%20with%20CSOM%20accept%20that%20there%20is%20no%20good%20way%20to%20respect%20Multi-factor%20logins....%20We%20really%20need%20graph%20to%20be%20completed%20to%20unify%20authentication%20across%20the%20API%20families.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-911124%22%20slang%3D%22en-US%22%3ERe%3A%20get%20valid%20auth%20token%20from%20MSAL%20for%20SP%20Online%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-911124%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F7811%22%20target%3D%22_blank%22%3E%40Ed%20Alexander%3C%2FA%3E%26nbsp%3B%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F288005%22%20target%3D%22_blank%22%3E%40AJWeber%3C%2FA%3E%26nbsp%3B%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F38365%22%20target%3D%22_blank%22%3E%40Kevin%20Morgan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDid%20you%20find%20any%20solution%20for%20getting%20Access%20Token%20using%20MSAL.NET%3F%3C%2FP%3E%3CP%3EI%20am%20also%20stuck%20with%20this%20issue%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20someone%20can%20help%20with%20this%20it%20would%20be%20great.%3C%2FP%3E%3CP%3EI%20have%20also%20asked%20a%20question%20on%20MSAL.NET%20Repos%20as%20well.%3C%2FP%3E%3CP%3ELink%20is%20here%20%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzureAD%2Fmicrosoft-authentication-library-for-dotnet%2Fissues%2F1439%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FAzureAD%2Fmicrosoft-authentication-library-for-dotnet%2Fissues%2F1439%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1155036%22%20slang%3D%22en-US%22%3ERe%3A%20get%20valid%20auth%20token%20from%20MSAL%20for%20SP%20Online%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1155036%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F71188%22%20target%3D%22_blank%22%3E%40ArvindKumar%20Mepani%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%20I%20was%20stuck%20like%20you%20and%20then%20I%20figured%20out%20that%20it%20works%20with%20the%20client%20credentials%20flow%20(certificate%2C%20not%20client%20%2F%20secret).%20Here%20my%20proposed%20answer%20%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F54771270%2Fmsal-ad-token-not-valid-with-sharepoint-online-csom%2F60090373%2360090373%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F54771270%2Fmsal-ad-token-not-valid-with-sharepoint-online-csom%2F60090373%2360090373%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20let%20me%20know.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

I'm in the middle of writing a .Net client app and have been using the latest Graph SDK to access OneDrive.  To authenticate, I used MSAL and with the appropriate "scopes", this gets me an OAuth token that works great for OneDrive access.

 

Unfortunately, the Graph API (REST and SDK) are far from complete at this time.  I need to supplement my Graph calls with some more traditional ECM where OneDrive for Business and straight SP Online Libraries are concerned (calls like checkout/checkin/locking and related properties like who a file is checked-out to, etc.)  To do this, I thought I'd leverage "SharePoint CSOM for Online" until Graph "catches up". 

 

However, the access token received via MSAL is refused by the ClientContext of the user's site/list.  It always results in a 401: Unauthorized being returned from the service.

 

Does anyone have a code snippet or tips/tricks to use MSAL to get a valid access token for the user's same SP Library - just connecting directly to the SP Online services?  I could be missing explicit "scopes" to request in the MSAL authentication too.  I am getting the feeling that MSAL is not compatible with SP Online, and that would be really unfortunate.

 

Thanks for any pointers!

5 Replies
Highlighted

Hope, the below post will help..

https://samlman.wordpress.com/2015/02/27/using-adal-access-tokens-with-o365-rest-apis-and-csom/

 

BTW : I have not personally tested the content listed in above post

Highlighted
I have seen that post before. In fact, I spent quite some time searching and reading before posting here...

That post uses ADAL assembly/library. From what I have read, it is the first gen support of AD authentication developed by MSFT. While it is still being worked-on, and probably worked _with_ by many projects, we have started with MSAL, which appears to be the eventual replacement for ADAL. (Thought was, if we're starting work "today", we should use MSFT's latest library and follow its direction.)

MSAL works great to get OAuth tokens for Graph - we have that working. We can't seem to get MSAL's AD tokens to work with SP Online. Integrating another auth library (especially one with very similar classes and methods) would be a nuisance and cause confusion and reduce maintainability. Thus, I'm trying to figure out how to get MSAL to do the same.

Thank you for your reply.
Highlighted

@AJWeber This is a really big issue for me right now as well... Building business apps with Graph works great until you run into some incomplete functionality.  For example creating a SharePoint site, or retrieving a SharePoint user id so that you can update a multiuser list field.

 

Is there active development under the beta graph? v1.0 after a couple years does not look like a good sign.

 

If anyone out there has already solved the issue of integrating CSOM or direct REST calls to SharePoint in a Microsoft.Graph MSAL application that would be a big help!

 

I would stick with CSOM accept that there is no good way to respect Multi-factor logins.... We really need graph to be completed to unify authentication across the API families.

 

Highlighted

@Ed Alexander , @AJWeber  @Kevin Morgan 

Did you find any solution for getting Access Token using MSAL.NET?

I am also stuck with this issue?

 

If someone can help with this it would be great.

I have also asked a question on MSAL.NET Repos as well.

Link is here : https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/1439

 

Highlighted

@ArvindKumar Mepani

 

Hi, I was stuck like you and then I figured out that it works with the client credentials flow (certificate, not client / secret). Here my proposed answer : https://stackoverflow.com/questions/54771270/msal-ad-token-not-valid-with-sharepoint-online-csom/600...

 

Please let me know.