Created
April 5, 2020 00:27
-
-
Save neil-sabol/3ceab9221500261bbda76e0893b8734e to your computer and use it in GitHub Desktop.
See https://blog.neilsabol.site/post/onetimesecret-api-powershell-cli-basic-example-one-time-secret/ . This snippet uses Invoke-RestMethod and the OneTimeSecret API to generate a random password then output the password and secret link to retrieve it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Your OneTimeSecret Username and API key from https://onetimesecret.com/account#apikey-tab | |
$apiUsername = "username@example.com" | |
$apiPassword = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
# Invoke-RestMethod is a little "tempermental" with Basic Authentication - the "-Credential" parameter specifically | |
# See https://stackoverflow.com/questions/24672760/powershells-invoke-restmethod-equivalent-of-curl-u-basic-authentication - thanks Borek Bernard | |
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $apiUsername,$apiPassword))) | |
# Specify parameters for random password generation - in this case, a Time to Live in seconds (7 days) | |
$Body = @{ | |
ttl = 604800 | |
} | |
# Call the OneTimeSecret API to generate a random password | |
$oneTimeSecret=Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method 'Post' -Uri "https://onetimesecret.com/api/v1/generate" -Body $body | |
# Print the generated secret and a URL you can share | |
write-host "$($oneTimeSecret.value)" | |
write-host "https://onetimesecret.com/secret/$($oneTimeSecret.secret_key)" | |
# Clear variables | |
$oneTimeSecret = "" | |
$apiPassword = "" | |
$base64AuthInfo = "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I see, Neil, thank you for the explanation.