Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 16 additions & 28 deletions scripts/get_face_access_token.ps1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given the context, might be better to add new script called create_ or do something smart with the returned expiry

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
param (
[string]$subscriptionId,
[string]$tokenId
[Parameter(Mandatory = $true)]
[string]$subscriptionId
)

# do az login if needed
try {
$account = az account show --output none 2>$null
if ($?) {
Expand All @@ -25,32 +26,19 @@ try {
}
}

if (-not $subscriptionId) {
Write-Host "Info: Subscription ID is not provided."
Write-Host "Info: If you want to provide the subscription ID manually, please run the script as below."
Write-Host "Usage: .\get_face_access_token.ps1 -subscriptionId <subscriptionId> [-tokenId <tokenId>]"
Write-Host "Info: Valid token IDs are 0 or 1"
try {
$subscriptionId = az account show --query id -o tsv
if (-not $subscriptionId) {
throw "Failed to retrieve the subscription ID."
}
Write-Host "Info: Now using the default subscription ID $subscriptionId"
} catch {
Write-Host "Error: Failed to retrieve the subscription ID."
exit 1
}
}
# Set tokenId
$tokenId = 0

if (-not $tokenId) {
Write-Host "Info: Token ID is not provided."
Write-Host "Info: If you want to provide the token ID manually, please run the script as below."
Write-Host "Usage: .\get_face_access_token.ps1 -subscriptionId <subscriptionId> [-tokenId <tokenId>]"
Write-Host "Info: Valid token IDs are 0 or 1"
$tokenArg = ""
} else {
$tokenArg = "id=$tokenId"
# Get the bearer token
try {
$bearerToken = $(az account get-access-token -s $subscriptionId -o tsv).split()[0]
} catch {
Write-Host "Failed to retrieve the bearer token."
exit 1
}

Write-Host "Fetching a token..."
az rest --method get --resource "https://management.core.windows.net/" --uri "https://face-sdk-gating-helper-2-staging.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?$tokenArg"
# Generate the token
Write-Host "Generating a new token..."
$response = Invoke-RestMethod -Uri "https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?id=${tokenId}" -Method POST -Headers @{"Authorization"="Bearer ${bearerToken}"}
Write-Host "Token generated successfully."
$response | Format-List
45 changes: 20 additions & 25 deletions scripts/get_face_access_token.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@

#!/bin/bash

# Check if the required parameter is provided
if [ -z "$1" ]; then
echo "Usage: $0 <subscriptionId>"
exit 1
fi

# do az login if needed
if az account show --output none 2>/dev/null; then
echo "User is already logged in."
else
Expand All @@ -11,31 +19,18 @@ else
fi
fi

if [ -z "$1" ]; then
echo "Info: Subscription ID is not provided."
echo "Info: If you want to provide the subscription ID manually, please run the script as below."
echo "Usage: $0 <subscriptionId> [tokenId]"
echo "Info: Valid token IDs are 0 or 1"
subscriptionId=$(az account show --query id -o tsv)
if [ -z "$subscriptionId" ]; then
echo "Error: Failed to retrieve the subscription ID."
exit 1
fi
echo "Info: Now using the default subscription ID $subscriptionId"
else
subscriptionId=$1
fi
subscriptionId=$1
tokenId=0

if [ -z "$2" ]; then
echo "Info: Token ID is not provided."
echo "Info: If you want to provide the token ID manually, please run the script as below."
echo "Usage: $0 <subscriptionId> [tokenId]"
echo "Info: Valid token IDs are 0 or 1"
tokenArg=""
else
tokenId=$2
tokenArg="id=${tokenId}"
# Get the bearer token
bearerToken=$(az account get-access-token -s $subscriptionId -o tsv | cut -f1)

# Check if bearerToken was successfully retrieved
if [ -z "$bearerToken" ]; then
echo "Error: Failed to retrieve the bearer token."
exit 1
fi

echo "Fetching a token..."
az rest --method get --resource "https://management.core.windows.net/" --uri "https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?${tokenArg}"
# Generate the token
echo "Generating a new token..."
curl -s -X POST --header "Authorization: Bearer ${bearerToken}" "https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?id=${tokenId}"