PnP-PowerShell
PnP-PowerShell copied to clipboard
using connect-pnponline with -accessToken and -url, doesn't create context
I use Connect-PnPOnline -url "https://tenant-admin.sharepoint.com" -AccessToken $token to try to establish a connection to perform operations.
#Expected behavior I would expect the next command to run on the context
Actual behavior
I see this error: "Get-PnPContext : The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument but not -SPOManagementShell or -PnPOManagementShell to connect."
It doesn't matter the command, it just has to be a command that uses the context object.
Steps to reproduce behavior
get a token string for auth in the var $token1, and run these commands- Connect-PnPOnline -Url "https://$tenant-admin.sharepoint.com" -AccessToken $token1 -ReturnConnection
Get-PnPContext
Which version of the PnP-PowerShell Cmdlets are you using?
- [ ] PnP PowerShell for SharePoint 2013
- [ ] PnP PowerShell for SharePoint 2016
- [ ] PnP PowerShell for SharePoint 2019
- [x] PnP PowerShell for SharePoint Online
What is the version of the Cmdlet module you are running?
SharePointPnPPowerShellOnline 3.23.2007.1
How did you install the PnP-PowerShell Cmdlets?
- [ ] MSI Installed downloaded from GitHub
- [x] Installed through the PowerShell Gallery with Install-Module
- [ ] Other means
+1 I have tried to find out when it was broken. It appears to be broken since 3.21.2005.2
so do earlier versions work ok?
Yes, i tried couple of commands in 3.21.2005.2, like Get-PnpContentType
and they worked after auth with access token
+1
+1 - getting this error after upgrading from version 3.20.2004.0 to the latest 3.24.2008.0
After obtaining a delegated access token using MSAL.PS I am trying to access SharePoint sites using code similar to the following:
$auth = Get-MsalToken -ClientId $appId -RedirectUri $redirectUri -TenantId $tenantId -Scopes $scopes -IntegratedWindowsAuth:$true
Connect-PnPOnline -Url $siteUrl -AccessToken $auth.AccessToken
$web = Get-PnPWeb
I get the following error: Get-PnPWeb : The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument to connect.
I tried the Connect-PnPOnline command with an access token. On connection, although a url is provided, it connects to the root tenant Sharepoint site i.e at https://tenantname.sharepoint.com and not the url specified.
$connection = Connect-PnPOnline -Url $HubSiteUrl -AccessToken $accessTokenSPO -ReturnConnection
The only way to make this work is installed this version Install-Module -Name SharePointPnPPowerShellOnline -RequiredVersion 3.21.2005.1 -Scope AllUsers -AllowClobber -Force -Verbose
When is the latest version going to be fixed?
@erwinvanhunen I had a deep dive into the code and I think I found at least part of the issue: File: /src/base/ConnectOnline.CS
Here's the method that connects using access tokens:
` private PnPConnection ConnectAccessToken() { var handler = new JwtSecurityTokenHandler(); var jwtToken = handler.ReadJwtToken(AccessToken); var aud = jwtToken.Audiences.FirstOrDefault(); var url = Url ?? aud ?? throw new PSArgumentException(Resources.AccessTokenConnectFailed);
switch (url.ToLower())
{
case GraphToken.ResourceIdentifier:
return PnPConnection.GetConnectionWithToken(new GraphToken(AccessToken), TokenAudience.MicrosoftGraph, InitializationType.Token, null, disableTelemetry: NoTelemetry.ToBool());
case OfficeManagementApiToken.ResourceIdentifier:
return PnPConnection.GetConnectionWithToken(new OfficeManagementApiToken(AccessToken), TokenAudience.OfficeManagementApi, InitializationType.Token, null, disableTelemetry: NoTelemetry.ToBool());
default:
return PnPConnection.GetConnectionWithToken(new SharePointToken(AccessToken), TokenAudience.SharePointOnline, InitializationType.Token, null, Url, disableTelemetry: NoTelemetry.ToBool());
}
}
` In the default case, you're passing Url, when I believe you mean to pass url, which is set on line 1153. This will cover cases where the token is passed without the url parameter.
Then, it calls looking at PNPConnection.GetConnectionWithToken and doesn't pass a clientContext (even though it may be able to build one using the url value...
I can't test much further, but maybe this will help?