M365 / SharePoint Online – PowerShell to renew expiring client secret key for SharePoint App
Hi All,
Greetings for the day 🙂 LIFE IS BEAUTIFUL 🙂
Today something new 🙂 sharing small PowerShell script to renew client secret key of SharePoint app
Background :
- In one of our application we were using SharePoint hosted app and client secret key
- Suddenly our application get stopped working
- We started scanning the logs and found following exception
“Error:Token request failed., InnerMessage:System.Net.WebException: The remote server returned an error: (401) Unauthorized.”
- So we started looking into app details and found that client secret key of app is expired
Details :
- By default SharePoint App secret key expires in 1 year.
- We have option – PowerShell CMDLET to renew it for next 3 years or create new secret key
- We no need to create new SharePoint App
- We no need to again give the permissions for our App
- Only thing we need to change – wherever we have specified client secret we need to update the secret key
PowerShell script :
# First, we install the SharePoint Online cmdlets using Install-Module in case we don’t have them already
Install-Module MSOnline
#M 365 global administrator credentials – we need here Global Administrator
$msolcred = Get-Credential
#once we have credentials, we will connect to M365 using Connect-MsolService
Connect-MsolService -credential $msolcred
#SharePoint App client id for which we need to regenerate the client secret
$clientId = "27c5b286-62a6-45c7-beda-abbaea6eecf2"
# Get credentials associated with a service principal
$keys = Get-MsolServicePrincipalCredential -AppPrincipalId $clientId
Remove-MsolServicePrincipalCredential -KeyIds $keys.KeyId -AppPrincipalId $clientId
#to create secret key we must need to remove all existing secret keys – Remove a credential from a service #principal => Generally it takes 24 hrs. for reflecting the changes so in some cases we may need to wait
#creating new secret key
$bytes = New-Object Byte[] 32
$rand = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rand.GetBytes($bytes)
$rand.Dispose()
$newClientSecret = [System.Convert]::ToBase64String($bytes)
#start date from now
$dtStart = [System.DateTime]::Now
#enddate – 3 years
$dtEnd = $dtStart.AddYears(3)
#generating new secret key for 3 years
New-MsolServicePrincipalCredential -AppPrincipalId $clientId -Type Symmetric -Usage Sign -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
New-MsolServicePrincipalCredential -AppPrincipalId $clientId -Type Symmetric -Usage Verify -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
New-MsolServicePrincipalCredential -AppPrincipalId $clientId -Type Password -Usage Verify -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
Copy the new client secret key
Thanks for reading 🙂 If its worth at least reading once, kindly please like and share 🙂 SHARING IS CARING 🙂
Enjoy the beautiful life 🙂 Have a FUN 🙂 HAVE A SAFE LIFE 🙂 TAKE CARE 🙂
You must log in to post a comment.