Skip to content

[BUG] Connect-PnPOnline AccessToken The remote server returned an error: (401) Unauthorized. #305

@TalismanM

Description

@TalismanM

Reporting an Issue

When I run the Get-PnPSite command, I get the error
Get-PnPSite : The remote server returned an error: (401) Unauthorized.

Expected behavior

Get-PnPSite command result

Actual behavior

image

Steps to reproduce behavior

Script:

# Application (client) ID, tenant ID and secret
$clientId = "<Azure App ID>"
$tenantId = "<Tenant ID>"
$clientSecret = '<secret>'

# Construct URI
$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"

# Construct Body
$body = @{
    client_id     = $clientId
    scope         = "https://graph.microsoft.com/.default"
    client_secret = $clientSecret
    grant_type    = "client_credentials"
}

$tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing
$token = ($tokenRequest.Content | ConvertFrom-Json).access_token

Connect-PnPOnline -Url "<MySite URL>" -AccessToken $token
Get-PnPSite

What is the version of the Cmdlet module you are running?

(you can retrieve this by executing Get-Module -Name "PnP.PowerShell" -ListAvailable)
image

Which operating system/environment are you running PnP PowerShell on?

  • [*] Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions