SOLVED

Connect-PnPMicrosoftGraph - Azure AD OAuth 2.0 Access Token has expired

%3CLINGO-SUB%20id%3D%22lingo-sub-29870%22%20slang%3D%22en-US%22%3EConnect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-29870%22%20slang%3D%22en-US%22%3E%3CP%3EIf%20I%20try%20to%20connect%20with%20Azure%20AD%20OAuth%202.0%20using%20%3CSTRONG%3EConnect-PnPMicrosoftGraph%3C%2FSTRONG%3E%20it%20gives%20error.%20Please%20see%20following%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%23Connect%20to%20Azure%20AD%20and%20get%20back%20an%20OAuth%202.0%20Access%20Token%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EConnect-PnPMicrosoftGraph%3C%2FSTRONG%3E%20%3CSTRONG%3E-Scopes%3C%2FSTRONG%3E%20%3CSTRONG%3E%22Group.ReadWrite.All%22%3C%2FSTRONG%3E%3CSTRONG%3E%2C%3C%2FSTRONG%3E%3CSTRONG%3E%22User.Read.All%22%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%23Store%20the%20Access%20Token%20in%20a%20local%20variable%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%24accessToken%3C%2FSTRONG%3E%3CSTRONG%3E%3D%3C%2FSTRONG%3E%20%3CSTRONG%3EGet-PnPAccessToken%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%23Create%20a%20new%20office%20365%20Unified%20Group%2C%20together%20with%20the%20corresponding%20Model%20Site%20in%20SPO%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%24group%3C%2FSTRONG%3E%20%3CSTRONG%3E%3D%3C%2FSTRONG%3E%20%3CSTRONG%3ENew-PnPUnifiedGroup%3C%2FSTRONG%3E%20%3CSTRONG%3E-DisplayName%3C%2FSTRONG%3E%20%3CSTRONG%3E%22Pnp%20Demo%203%22%3C%2FSTRONG%3E%20%3CSTRONG%3E-Description%3C%2FSTRONG%3E%20%3CSTRONG%3E%22Pnp%20Demo%203%22%3C%2FSTRONG%3E%20%3CSTRONG%3E-MailNickname%3C%2FSTRONG%3E%20%3CSTRONG%3E%22pnp-demo-3%22%3C%2FSTRONG%3E%20%3CSTRONG%3E-Members%3C%2FSTRONG%3E%20%3CSTRONG%3E%22myemail%40mycompany.com%22%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EError%3A%3C%2FP%3E%3CP%3E%3CSTRONG%3EWARNING%3A%20The%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired.%20Authenticate%20again%20using%20the%20Connect-PnPMicrosoftGraph%20cmdlet.%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3ENew-PnPUnifiedGroup%20%3A%20Value%20cannot%20be%20null.%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EParameter%20name%3A%20accessToken%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EAt%20line%3A9%20char%3A10%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%2B%20%24group%20%3D%20New-PnPUnifiedGroup%20-DisplayName%20%22Pnp%20Demo%203%22%20-Description%20%22%20...%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%2B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%2B%20CategoryInfo%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3A%20NotSpecified%3A%20(%3A)%20%5BNew-PnPUnifiedGroup%5D%2C%20ArgumentNullException%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%20%2B%20FullyQualifiedErrorId%20%3A%20System.ArgumentNullException%2CSharePointPnP.PowerShell.Commands.Graph.NewPnPUnifiedGroup%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3ESince%20access%20token%20is%20expiring%20quite%20often%2C%20what%20is%20the%20best%20way%20to%20increase%20the%20expiry%20time%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-29870%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-149936%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-149936%22%20slang%3D%22en-US%22%3E%3CP%3EThanks!%20That%20is%20what%20I%20suspected.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-149024%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-149024%22%20slang%3D%22en-US%22%3E%3CP%3EGlobal%20Admin%20%3A(%3C%2Fimg%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-149020%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-149020%22%20slang%3D%22en-US%22%3EDid%20you%20find%20out%20what%20permissions%20are%20needed%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-107462%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-107462%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20are%20using%3C%2FP%3E%0A%3CP%20align%3D%22left%22%20class%3D%22CodeBlock%22%20style%3D%22text-align%3A%20left%3B%22%3EConnect-PnPMicrosoftGraph%20-Scopes%20%22Group.ReadWrite.All%22%2C%22User.Read.All%22%3C%2FP%3E%0A%3CP%3Eto%20provisioning%20sites%20with%20SharePoint%20Administrator%20but%20we%20receive%20following%20error.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20579px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F20275iBC4592570BDFA405%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Untitled.png%22%20title%3D%22Untitled.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ERequestign%20global%20Admin%20credentials.%3C%2FP%3E%0A%3CP%3Ewe%20can't%20use%20Global%20Admin.%20Could%20you%20give%20us%20some%20clue%20about%20error%3F%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127%22%20target%3D%22_blank%22%3E%40Paolo%20Pialorsi%3C%2FA%3E%3C%2FP%3E%0A%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-56158%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-56158%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127%22%20target%3D%22_blank%22%3E%40Paolo%20Pialorsi%3C%2FA%3E%2C%20Do%20you%20know%20when%20support%20for%20refresh%20tokens%26nbsp%3Bis%20in%20the%20planning%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-34679%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-34679%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F713%22%20target%3D%22_blank%22%3E%40Marc%20Vanderstraeten%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYes%20it%20is.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-34678%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-34678%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127%22%20target%3D%22_blank%22%3E%40Paolo%20Pialorsi%3C%2FA%3E%26nbsp%3BCan%20you%20be%20more%20specific%20with%20the%20problem%20using%26nbsp%3B%3CSPAN%3ENew-PnPUnifiedGroup%3F%20I%20was%20also%20wondering%20if%20we%20use%20this%20command%2C%20is%20the%20Team%20Site%20being%20created%20automagically%3F%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-31814%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-31814%22%20slang%3D%22en-US%22%3E%3CP%3E%40Hi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127%22%20target%3D%22_blank%22%3E%40Paolo%20Pialorsi%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOk%2C%20understood.%20As%20I've%20been%20using%20the%20PowerShell%20commands%20that%20wouldn't%20be%20somethign%20that%20I%20need%20to%20do%20as%20I%20waass%20already%20using%20this%20scope%20in%20the%20command.%20So%20that%20is%20fine.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWeird%20thing%20now%20is%20that%20since%20I%20made%20my%20corporate%20account%20a%20global%20admin%20things%20are%20working.%20Even%20though%20I've%20now%20taken%20the%20global%20Admin%20rights%20away%20again%20it's%20still%20working%20and%20I%20can't%20make%20this%20fail%20anymore.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20also%20tried%20the%20same%20procedure%20with%20my%20MSDN%20account%20and%20I%20can't%20make%20this%20work.%20Anyway%2C%20my%20problem%20seems%20%26nbsp%3Bto%20be%20resolved%20now%2C%20however%20I'm%20quite%20confused%20as%20to%20why%20it's%20working.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethanks%20for%20your%20help.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-31807%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-31807%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Pieter%2C%3C%2FP%3E%3CP%3EThe%20scope%20has%20to%20be%20provided%20as%20an%20input%20argument%20to%20the%20Connect-PnPMicrosoftGraph%20cmdlet.%20Something%20like%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EConnect-PnPMicrosoftGraph%20-Scopes%20%22Group.ReadWrite.All%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBye%2C%3C%2FP%3E%3CP%3EPaolo%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-31789%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-31789%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127%22%20target%3D%22_blank%22%3E%40Paolo%20Pialorsi%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOk%2C%20I'm%20getting%20there%20now.%20I'm%20global%20admin%20in%20Azure%20AD.%20Please%20let%20me%20know%20what%20I%20should%20do%20to%20get%20Group.ReadWrite.All%20scope.%20I'm%20still%20quite%20new%20to%20Azure%20AD%20and%20Unified%20Groups.%20(Who%20isn't%3F)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-31784%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-31784%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Pieter%2C%3C%2FP%3E%3CP%3EIn%20order%20to%20create%20a%20Unified%20Group%20you%20simply%20need%26nbsp%3B%22Group.ReadWrite.All%22%20scope.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-31782%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-31782%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127%22%20target%3D%22_blank%22%3E%40Paolo%20Pialorsi%3C%2FA%3E%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F369%22%20target%3D%22_blank%22%3E%40Vesa%20Juvonen%3C%2FA%3E%3C%2FP%3E%3CP%3EI%20think%20the%20problem%20is%20to%20do%20with%20permissions.%20My%20user%20account%20isn't%20a%20global%20admin%20in%20Azure%20AD.%3C%2FP%3E%3CP%3EI%20will%20get%20this%20chnaged%20and%20then%20I'll%20report%20back.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20your%20help.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-31769%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-31769%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EThe%20Warning%20message%20is%20managed%20by%20the%20implementation%20in%20Core%2C%20and%20could%20be%20that%20we%20have%20a%20bug%20related%20to%20timezones%20(I%20will%20double-check%20it%2C%20just%20in%20case).%20However%2C%20most%20likely%20the%20exception%20you%20see%20is%20not%20necessarily%20related%20to%20the%20token%20lifetime%2C%20but%20to%20a%20lack%20of%20proper%20permissions%20for%20the%20user%20or%20to%20a%20known%20bug%20of%20the%20New-PnPUnifiedGroup%20cmdlet.%20We%20are%20already%20aware%20of%20that%20bug%20and%20it%20has%20already%20been%20fixed%20in%20the%20DEV%20branch%20of%20Core.%20The%20fix%20will%20be%20included%20in%20the%20next%20monthly%20release%20of%20Core.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAbout%26nbsp%3Bthe%20tokens%20lifetime%2C%20by%20default%20an%20access%20token%20released%20by%20Azure%20AD%20lasts%20in%201%20hour%20and%20a%20refresh%20token%20lasts%20in%2014%20days.%20However%2C%20you%20can%20use%20the%20refresh%20token%20to%20create%20a%20new%20access%20token%20(and%20a%20new%20refresh%20token%2C%20too)%20for%20up%20to%2090%20days.%20Then%20you%20will%20have%20to%20re-authenticate.%3C%2FP%3E%3CP%3ENevertheless%2C%20an%20access%2Frefresh%20token%20can%20expire%20suddenly%2C%20for%20example%20if%20the%20user%26nbsp%3Bchanges%20her%2Fhis%20password%2C%20and%20because%20of%20some%20other%20happenings.%20Thus%2C%20we%20cannot%20rely%20on%20them%20and%20sometime%20we%20could%20have%20to%20re-authenticate.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe're%20thinking%20about%20refactoring%20a%20little%20bit%20the%20Connect-PnPMicrosoftGraph%20cmdlet%2C%20eventually%20merging%20it%20with%20Connect-PnPOnline%2C%20in%20order%20to%20support%20refresh%20tokens%20(which%20are%20not%20supported%20right%20now)%20and%20providing%20re-authentication%20capabilities%2C%20but%20we%20are%20still%20%22working%20on%20it%22.%20Stay%20tuned%20...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3CBR%20%2F%3EPaolo%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-31763%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-31763%22%20slang%3D%22en-US%22%3E%3CP%3ECode%20uses%20pretty%20much%20the%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fresources%2Fsamples%2Factive-directory-dotnet-webapp-openidconnect-v2%2F%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EMSAL%3C%2FA%3E%20as%20such%20and%20just%20stores%20the%20access%20token%20after%20user%20signing%20in%20for%20the%20next%20operations%2C%20so%20there's%20no%20additional%20magic%20around%20the%20expiration.%20Code%20for%20the%20signing%20in%20is%20in%20following%20file%20in%20the%20GitHub.%20Most%20likely%20issue%20was%20failure%20of%20authentication%20or%20the%20user%20did%20not%20had%20sufficient%20permissions%20for%20the%20requested%20scopes.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FOfficeDev%2FPnP-PowerShell%2Fblob%2Fmaster%2FCommands%2FBase%2FConnectPnPMicrosoftGraph.cs%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FOfficeDev%2FPnP-PowerShell%2Fblob%2Fmaster%2FCommands%2FBase%2FConnectPnPMicrosoftGraph.cs%26nbsp%3B%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI'll%20also%20ping%20Paolo%20around%20this%20using%20different%20channels%2C%20if%20he%20has%20anything%20to%20add%20or%20has%20seen%20the%20same%20behaviour.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-31598%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-31598%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F29645%22%20target%3D%22_blank%22%3E%40Umair%20Naeem%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EInteresteresting%20question.%20I've%20had%20a%20play%20around%20with%20this%20and%20I%20got%20the%20following%20results.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%20I%20started%20debugging%20the%20Connect-PnPMicrosoftGraph%20Cmdlet.%20I%20used%20different%20accounts%20and%20different%20tenants.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%20one%20of%20our%20prodcution%20tenants.%20I%20connected%20ith%20my%20admin%20accouont%20and%20I%20foudn%20that%20the%20expiry%20time%20on%20the%20authentiction%20request%20was%20instant.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20611px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F8748i4C87CE0F644E8EC9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22AuthenticationExpiry.PNG%22%20title%3D%22AuthenticationExpiry.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20I%20repeted%20my%20test%20nd%20I%20found%20tht%20the%20authetication%20expired%20in%201%20hour%20from%20the%20moment%20of%20requesting%20the%20access.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20587px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F8749i5068EE11124CCD09%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22AuthenticationExpiry2.PNG%22%20title%3D%22AuthenticationExpiry2.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOk%2C%20so%20there%20seems%20to%20be%20an%20issue%20here%20to%20do%20with%20expiring%20tokens.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F369%22%20target%3D%22_blank%22%3E%40Vesa%20Juvonen%3C%2FA%3E%2C%20where%20is%20the%20expiry%20for%20these%20access%20tokens%20retrieved%20from%3F%20It%20looks%20like%20Microsoft%20Graph%20uses%20different%20expiry%20times%20for%20differnt%20tenants.%20Or%20is%20this%20simply%20a%20matter%20of%20access%20denied%20defaulting%20the%20expiry%20to%20now%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-31025%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPMicrosoftGraph%20-%20Azure%20AD%20OAuth%202.0%20Access%20Token%20has%20expired%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-31025%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F369%22%20target%3D%22_blank%22%3E%40Vesa%20Juvonen%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20can%20you%20help.%20The%20only%20solution%20I%20can%20imagine%20is%20to%20keep%20re-trying%26nbsp%3B%20to%20connect%20in%20order%20to%20get%20new%20access%20token.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

If I try to connect with Azure AD OAuth 2.0 using Connect-PnPMicrosoftGraph it gives error. Please see following:

 

#Connect to Azure AD and get back an OAuth 2.0 Access Token

Connect-PnPMicrosoftGraph -Scopes "Group.ReadWrite.All","User.Read.All"

 

#Store the Access Token in a local variable

$accessToken= Get-PnPAccessToken

 

#Create a new office 365 Unified Group, together with the corresponding Model Site in SPO

$group = New-PnPUnifiedGroup -DisplayName "Pnp Demo 3" -Description "Pnp Demo 3" -MailNickname "pnp-demo-3" -Members "myemail@mycompany.com"

 

Error:

WARNING: The Azure AD OAuth 2.0 Access Token has expired. Authenticate again using the Connect-PnPMicrosoftGraph cmdlet.

New-PnPUnifiedGroup : Value cannot be null.

Parameter name: accessToken

At line:9 char:10

+ $group = New-PnPUnifiedGroup -DisplayName "Pnp Demo 3" -Description " ...

+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 + CategoryInfo         : NotSpecified: (:) [New-PnPUnifiedGroup], ArgumentNullException

   + FullyQualifiedErrorId : System.ArgumentNullException,SharePointPnP.PowerShell.Commands.Graph.NewPnPUnifiedGroup

 

Since access token is expiring quite often, what is the best way to increase the expiry time?

16 Replies
Highlighted

@Vesa Juvonen,

 

Please can you help. The only solution I can imagine is to keep re-trying  to connect in order to get new access token.

Highlighted

Hi @Umair Naeem,

 

Interesteresting question. I've had a play around with this and I got the following results.

 

First I started debugging the Connect-PnPMicrosoftGraph Cmdlet. I used different accounts and different tenants.

 

First one of our prodcution tenants. I connected ith my admin accouont and I foudn that the expiry time on the authentiction request was instant.

 

 

AuthenticationExpiry.PNG

 

Then I repeted my test nd I found tht the authetication expired in 1 hour from the moment of requesting the access.

 

AuthenticationExpiry2.PNG

 

 

Ok, so there seems to be an issue here to do with expiring tokens.

 

@Vesa Juvonen, where is the expiry for these access tokens retrieved from? It looks like Microsoft Graph uses different expiry times for differnt tenants. Or is this simply a matter of access denied defaulting the expiry to now?

 

 

 

 

Highlighted

Code uses pretty much the MSAL as such and just stores the access token after user signing in for the next operations, so there's no additional magic around the expiration. Code for the signing in is in following file in the GitHub. Most likely issue was failure of authentication or the user did not had sufficient permissions for the requested scopes.

 

 

I'll also ping Paolo around this using different channels, if he has anything to add or has seen the same behaviour.

 

Highlighted
Best Response confirmed by Umair Naeem (New Contributor)
Solution

Hi,

The Warning message is managed by the implementation in Core, and could be that we have a bug related to timezones (I will double-check it, just in case). However, most likely the exception you see is not necessarily related to the token lifetime, but to a lack of proper permissions for the user or to a known bug of the New-PnPUnifiedGroup cmdlet. We are already aware of that bug and it has already been fixed in the DEV branch of Core. The fix will be included in the next monthly release of Core.

 

About the tokens lifetime, by default an access token released by Azure AD lasts in 1 hour and a refresh token lasts in 14 days. However, you can use the refresh token to create a new access token (and a new refresh token, too) for up to 90 days. Then you will have to re-authenticate.

Nevertheless, an access/refresh token can expire suddenly, for example if the user changes her/his password, and because of some other happenings. Thus, we cannot rely on them and sometime we could have to re-authenticate.

 

We're thinking about refactoring a little bit the Connect-PnPMicrosoftGraph cmdlet, eventually merging it with Connect-PnPOnline, in order to support refresh tokens (which are not supported right now) and providing re-authentication capabilities, but we are still "working on it". Stay tuned ...

 

Thanks,
Paolo

Highlighted

Hi @Paolo Pialorsi@Vesa Juvonen

I think the problem is to do with permissions. My user account isn't a global admin in Azure AD.

I will get this chnaged and then I'll report back.

 

Thanks for your help.

Highlighted

Hi Pieter,

In order to create a Unified Group you simply need "Group.ReadWrite.All" scope.

Hi @Paolo Pialorsi,

 

Ok, I'm getting there now. I'm global admin in Azure AD. Please let me know what I should do to get Group.ReadWrite.All scope. I'm still quite new to Azure AD and Unified Groups. (Who isn't?)

 

 

 

 

 

Highlighted

Hi Pieter,

The scope has to be provided as an input argument to the Connect-PnPMicrosoftGraph cmdlet. Something like:

 

Connect-PnPMicrosoftGraph -Scopes "Group.ReadWrite.All"

 

Bye,

Paolo

Highlighted

@Hi @Paolo Pialorsi,

 

Ok, understood. As I've been using the PowerShell commands that wouldn't be somethign that I need to do as I waass already using this scope in the command. So that is fine.

 

Weird thing now is that since I made my corporate account a global admin things are working. Even though I've now taken the global Admin rights away again it's still working and I can't make this fail anymore.

 

I've also tried the same procedure with my MSDN account and I can't make this work. Anyway, my problem seems  to be resolved now, however I'm quite confused as to why it's working.

 

thanks for your help. 

 

 

Highlighted

@Paolo Pialorsi Can you be more specific with the problem using New-PnPUnifiedGroup? I was also wondering if we use this command, is the Team Site being created automagically?

Highlighted
Highlighted

@Paolo Pialorsi, Do you know when support for refresh tokens is in the planning?

Highlighted

We are using

Connect-PnPMicrosoftGraph -Scopes "Group.ReadWrite.All","User.Read.All"

to provisioning sites with SharePoint Administrator but we receive following error.

Untitled.png

Requestign global Admin credentials.

we can't use Global Admin. Could you give us some clue about error? @Paolo Pialorsi

Thanks

Highlighted
Did you find out what permissions are needed?
Highlighted
Highlighted

Thanks! That is what I suspected.