From 92ae6eb633aa7a0dc593e6b244f45d5988e1ce31 Mon Sep 17 00:00:00 2001 From: Milan Holemans <11723921+milanholemans@users.noreply.github.com> Date: Tue, 23 Aug 2022 12:07:18 +0200 Subject: [PATCH] Fixes get accesstoken on different site --- src/Commands/Base/GetAccessToken.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Commands/Base/GetAccessToken.cs b/src/Commands/Base/GetAccessToken.cs index 4130d1fd2..3d1b45fc9 100644 --- a/src/Commands/Base/GetAccessToken.cs +++ b/src/Commands/Base/GetAccessToken.cs @@ -1,6 +1,7 @@ -using System.Management.Automation; -using PnP.PowerShell.Commands.Attributes; +using PnP.PowerShell.Commands.Attributes; using PnP.PowerShell.Commands.Enums; +using System; +using System.Management.Automation; namespace PnP.PowerShell.Commands.Base { @@ -43,7 +44,13 @@ protected override void ExecuteCmdlet() accessTokenValue = AccessToken; break; case ResourceTypeName.SharePoint: - accessTokenValue = TokenHandler.GetAccessToken(null, Connection?.Context?.Url?.TrimEnd('/') + "/.default", Connection); + var currentUrl = Connection?.Context?.Url?.TrimEnd('/'); + if (string.IsNullOrEmpty(currentUrl)) + { + throw new PSArgumentException("No connection found, please login first."); + } + var rootUrl = new Uri(currentUrl).GetLeftPart(UriPartial.Authority); + accessTokenValue = TokenHandler.GetAccessToken(null, rootUrl + "/.default", Connection); break; case ResourceTypeName.ARM: accessTokenValue = TokenHandler.GetAccessToken(null, "https://management.azure.com/.default", Connection);