Powershell script to get the refresh token expiry

Devender 20 Reputation points
2023-03-16T13:35:57.1433333+00:00

Hi All,
I am currently working on a PS script to generate OAuth 2 access tokens(having shorter expiry) using refresh token(having longer expiry).
I do have the code figured out for this but would need help to have a code to be able to identify the expiry of the refresh token. Based on this expiry, the code should be able to generate emails(reminders) to generate a new refresh token.
I tried checking ‘expires_in’ property but it is a static value, i wanted more of countdown.
Any help would be appreciated.

Thanks,
Devender

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Sedat SALMAN 14,180 Reputation points MVP
    2023-03-17T14:54:55.45+00:00
    # Set the expiry time of the refresh token (in seconds)
    $refreshTokenExpiry = 3600
    
    # Generate the refresh token and store it in $refreshToken
    # ...
    
    # Calculate the expiry time of the refresh token
    $expiryTime = (Get-Date).AddSeconds($refreshTokenExpiry)
    
    # Schedule email reminders based on the remaining time until the token expires
    $reminderIntervals = @(1800, 600, 60)  # Remind 30 mins, 10 mins, and 1 min before expiry
    
    foreach ($interval in $reminderIntervals) {
        $reminderTime = $expiryTime.AddSeconds(-$interval)
        Register-ScheduledTask -TaskName "Refresh Token Expiry Reminder" `
            -Trigger (New-ScheduledTaskTrigger -Once -At $reminderTime) `
            -Action {
                # Send email reminder
                Send-MailMessage -To "user@example.com" -Subject "Refresh token will expire soon" `
                    -Body "Your refresh token will expire in $($interval / 60) minutes."
            }
    }
    
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.