Skip to content

Commit d9b5565

Browse files
authored
Update authentication.md
update get-token with a working example
1 parent ed10834 commit d9b5565

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

articles/ai-services/authentication.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -239,32 +239,30 @@ In this sample, a password is used to authenticate the service principal. The to
239239
```
240240

241241
2. Get a token:
242-
> [!NOTE]
243-
> If you're using Azure Cloud Shell, the `SecureClientSecret` class isn't available.
244-
245-
#### [PowerShell](#tab/powershell)
246242
```powershell-interactive
247-
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList "https://login.windows.net/<TENANT_ID>"
248-
$secureSecretObject = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.SecureClientSecret" -ArgumentList $SecureStringPassword
249-
$clientCredential = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential" -ArgumentList $app.ApplicationId, $secureSecretObject
250-
$token=$authContext.AcquireTokenAsync("https://cognitiveservices.azure.com/", $clientCredential).Result
251-
$token
252-
```
243+
$tenantId = $context.Tenant.Id
244+
$clientId = $app.ApplicationId
245+
$clientSecret = "<YOUR_PASSWORD>"
246+
$resourceUrl = "https://cognitiveservices.azure.com/"
253247
254-
#### [Azure Cloud Shell](#tab/azure-cloud-shell)
255-
```Azure Cloud Shell
256-
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList "https://login.windows.net/<TENANT_ID>"
257-
$clientCredential = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential" -ArgumentList $app.ApplicationId, <YOUR_PASSWORD>
258-
$token=$authContext.AcquireTokenAsync("https://cognitiveservices.azure.com/", $clientCredential).Result
259-
$token
260-
```
248+
$tokenEndpoint = "https://login.microsoftonline.com/$tenantId/oauth2/token"
249+
$body = @{
250+
grant_type = "client_credentials"
251+
client_id = $clientId
252+
client_secret = $clientSecret
253+
resource = $resourceUrl
254+
}
255+
256+
$responseToken = Invoke-RestMethod -Uri $tokenEndpoint -Method Post -Body $body
257+
$accessToken = $responseToken.access_token
258+
```
261259

262260
---
263261

264262
3. Call the Computer Vision API:
265263
```powershell-interactive
266264
$url = $account.Endpoint+"vision/v1.0/models"
267-
$result = Invoke-RestMethod -Uri $url -Method Get -Headers @{"Authorization"=$token.CreateAuthorizationHeader()} -Verbose
265+
$result = Invoke-RestMethod -Uri $url -Method Get -Headers @{"Authorization"="Bearer $accessToken"} -Verbose
268266
$result | ConvertTo-Json
269267
```
270268

0 commit comments

Comments
 (0)