1

I am using following commands to connect to my SharePoint Online environment.

  1. Connect-SPOService -Url "https://testdomain.sharepoint.com/sites/test1"
  2. Connect-PnPOnline -Url "https://testdomain.sharepoint.com/sites/test1"

In an ideal case this command should connect me to the site collection. I have verified that I am using correct UserName and Password.

But instead I am getting following errors

For command 1:

Connect-SPOService : Could not authenticate to SharePoint Online https://testdomain.sharepoint.com/sites/test1 using OAuth 2.0 At line:1 char:1 + Connect-SPOService -Url "https://testdomain.sharepoint.com/sites/test1" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Connect-SPOService], AuthenticationException + FullyQualifiedErrorId : Microsoft.Online.SharePoint.PowerShell.AuthenticationException,Microsoft.Online.SharePoint.PowerShell.ConnectSPOService

For command 2:

Connect-PnPOnline : The remote server returned an error: (403) Forbidden. At line:1 char:1 + Connect-PnPOnline -Url "https://testdomain.sharepoint.com/sites/test1 ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Connect-PnPOnline], WebException + FullyQualifiedErrorId : System.Net.WebException,SharePointPnP.PowerShell.Commands.Base.ConnectOnline

Unusual behavior: When I am running below command.

Connect-PnPOnline -Url https://testdomain.sharepoint.com/sites/test1 
-PnPO365ManagementShell -LaunchBrowser

i.e. with parameters "PnPO365ManagementShell" "LaunchBrowser", it works perfectly fine. I am not sure what's wrong.

Similar behavior Also I have installed the nuget package for SharePointPnPCoreOnline v3.1.1809 in my Provisioning solution. So when I am running my provisioning code I get same error while establishing connection.

Last thing: When I use same commands from other VMs or colleagues machine with same Creds, it works perfectly fine. Means something is wrong on my machine.

10
  • 1
    did it use to work earlier and after a couple of days stopped working ? or it was always broken ? Feb 1, 2019 at 11:07
  • Yes it used to work earlier on my projects and machine. Then I was on On-Prem project for 7 months. Since Oct2018 its the problem..Now I got chance to post it on SP.SE
    – Asad Refai
    Feb 1, 2019 at 11:10
  • @GautamSheth there were many failed windows upgrades on my local system. Could these be a problem? Although I got that all clean now, but still same issue.
    – Asad Refai
    Feb 1, 2019 at 11:11
  • can you try Remove-PnPStoredCredential -Name https://tenant.sharepoint.com -Force and then try to connect again ? Looks like those creds got stored , this command might help Feb 1, 2019 at 11:21
  • 2
    not sure, for command 1, would suggest that you connect to the admin site, i.e Connect-SPOService -Url "https://testdomain-admin.sharepoint.com/ because SPO commandlets only work with admin site urls Feb 1, 2019 at 11:36

1 Answer 1

0

This is what I use to connect myself to SPO (but also MSO,AZureAD, ...) :

$username = "[email protected]"
$password = "password"

$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force)

Connect-SPOService -Url https://testdomain-admin.sharepoint.com -Credential $cred 
#as said in the comments, you need to connect to the admin url of SPO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.