PnP-PowerShell icon indicating copy to clipboard operation
PnP-PowerShell copied to clipboard

using connect-pnponline with -accessToken and -url, doesn't create context

Open westleyMS opened this issue 4 years ago • 9 comments

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

westleyMS avatar Jul 22 '20 02:07 westleyMS

+1 I have tried to find out when it was broken. It appears to be broken since 3.21.2005.2

sanek112r avatar Jul 23 '20 12:07 sanek112r

so do earlier versions work ok?

westleyMS avatar Jul 23 '20 20:07 westleyMS

Yes, i tried couple of commands in 3.21.2005.2, like Get-PnpContentType and they worked after auth with access token

sanek112r avatar Jul 24 '20 09:07 sanek112r

+1

antoinegenot avatar Aug 05 '20 12:08 antoinegenot

+1 - getting this error after upgrading from version 3.20.2004.0 to the latest 3.24.2008.0

PedroMordeP avatar Aug 18 '20 17:08 PedroMordeP

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.

tlingenf avatar Oct 09 '20 14:10 tlingenf

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

priyachads avatar Oct 20 '20 07:10 priyachads

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?

pablogalansabugo avatar Nov 17 '20 01:11 pablogalansabugo

@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?

ioamnesia avatar Nov 17 '20 04:11 ioamnesia