Skip to content

Commit 6f2ab0b

Browse files
author
Jill Grant
authored
Merge pull request #284331 from der3318/fixups-for-face-sdk-migration
Fixups for Face SDK Migration
2 parents 9c52ceb + db85edd commit 6f2ab0b

File tree

3 files changed

+33
-41
lines changed

3 files changed

+33
-41
lines changed

articles/ai-services/computer-vision/how-to/identity-access-token.md

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -112,61 +112,51 @@ curl -X POST 'https://<client-endpoint>/face/v1.0/identify' \
112112
113113
#### [C#](#tab/csharp)
114114

115-
The following code snippets show you how to use an access token with the [Face SDK for C#](https://www.nuget.org/packages/Microsoft.Azure.CognitiveServices.Vision.Face).
115+
The following code snippets show you how to use an access token with the [Face SDK for C#](https://aka.ms/azsdk-csharp-face-pkg).
116116

117-
The following class uses an access token to create a **ServiceClientCredentials** object that can be used to authenticate a Face API client object. It automatically adds the access token as a header in every request that the Face client will make.
117+
The following class uses an access token to create a **HttpPipelineSynchronousPolicy** object that can be used to authenticate a Face API client object. It automatically adds the access token as a header in every request that the Face client will make.
118118

119119
```csharp
120-
public class LimitedAccessTokenWithApiKeyClientCredential : ServiceClientCredentials
120+
public class LimitedAccessTokenPolicy : HttpPipelineSynchronousPolicy
121121
{
122-
/// <summary>
123-
/// Creates a new instance of the LimitedAccessTokenWithApiKeyClientCredential class
124-
/// </summary>
125-
/// <param name="apiKey">API Key for the Face API or CognitiveService endpoint</param>
126-
/// <param name="limitedAccessToken">LimitedAccessToken to bypass the limited access program, requires ISV sponsership.</param>
127-
128-
public LimitedAccessTokenWithApiKeyClientCredential(string apiKey, string limitedAccessToken)
129-
{
130-
this.ApiKey = apiKey;
131-
this.LimitedAccessToken = limitedAccessToken;
122+
/// <summary>
123+
/// Creates a new instance of the LimitedAccessTokenPolicy class
124+
/// </summary>
125+
/// <param name="limitedAccessToken">LimitedAccessToken to bypass the limited access program, requires ISV sponsership.</param>
126+
public LimitedAccessTokenPolicy(string limitedAccessToken)
127+
{
128+
_limitedAccessToken = limitedAccessToken;
132129
}
133130

134-
private readonly string ApiKey;
135-
private readonly string LimitedAccesToken;
136-
137-
/// <summary>
138-
/// Add the Basic Authentication Header to each outgoing request
139-
/// </summary>
140-
/// <param name="request">The outgoing request</param>
141-
/// <param name="cancellationToken">A token to cancel the operation</param>
142-
public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
143-
{
144-
if (request == null)
145-
throw new ArgumentNullException("request");
146-
request.Headers.Add("Ocp-Apim-Subscription-Key", ApiKey);
147-
request.Headers.Add("LimitedAccessToken", $"Bearer {LimitedAccesToken}");
148-
149-
return Task.FromResult<object>(null);
150-
}
151-
}
131+
private readonly string _limitedAccessToken;
132+
133+
/// <summary>
134+
/// Add the authentication header to each outgoing request
135+
/// </summary>
136+
/// <param name="message">The outgoing message</param>
137+
public override void OnSendingRequest(HttpMessage message)
138+
{
139+
message.Request.Headers.Add("LimitedAccessToken", $"Bearer {_limitedAccessToken}");
140+
}
141+
}
152142
```
153143

154144
In the client-side application, the helper class can be used like in this example:
155145

156146
```csharp
157-
static void Main(string[] args)
158-
{
147+
static void Main(string[] args)
148+
{
159149
// create Face client object
160-
var faceClient = new FaceClient(new LimitedAccessTokenWithApiKeyClientCredential(apiKey: "<client-face-key>", limitedAccessToken: "<token>"));
161-
162-
faceClient.Endpoint = "https://mytest-eastus2.cognitiveservices.azure.com";
150+
var clientOptions = new AzureAIVisionFaceClientOptions();
151+
clientOptions.AddPolicy(new LimitedAccessTokenPolicy("<token>"), HttpPipelinePosition.PerCall);
152+
FaceClient faceClient = new FaceClient(new Uri("<client-endpoint>"), new AzureKeyCredential("<client-face-key>"), clientOptions);
163153

164154
// use Face client in an API call
165-
using (var stream = File.OpenRead("photo.jpg"))
155+
using (var stream = File.OpenRead("photo.jpg"))
166156
{
167-
var result = faceClient.Face.DetectWithStreamAsync(stream, detectionModel: "Detection_03", recognitionModel: "Recognition_04", returnFaceId: true).Result;
157+
var response = faceClient.Detect(BinaryData.FromStream(stream), FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, returnFaceId: true);
168158

169-
Console.WriteLine(JsonConvert.SerializeObject(result));
159+
Console.WriteLine(JsonConvert.SerializeObject(response.Value));
170160
}
171161
}
172162
```

articles/ai-services/computer-vision/how-to/specify-detection-model.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ var faces = response.Value;
7373

7474
The Face service can extract face data from an image and associate it with a **Person** object through the [Add Person Group Person Face] API. In this API call, you can specify the detection model in the same way as in [Detect].
7575

76-
See the following code example for the .NET client library.
76+
See the following .NET code example.
7777

7878
```csharp
7979
// Create a PersonGroup and add a person with face detected by "detection_03" model
@@ -110,7 +110,7 @@ This code creates a **PersonGroup** with ID `mypersongroupid` and adds a **Perso
110110
111111
## Add face to FaceList with specified model
112112

113-
You can also specify a detection model when you add a face to an existing **FaceList** object. See the following code example for the .NET client library.
113+
You can also specify a detection model when you add a face to an existing **FaceList** object. See the following .NET code example.
114114

115115
```csharp
116116
using (var content = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new Dictionary<string, object> { ["name"] = "My face collection", ["recognitionModel"] = "recognition_04" }))))
@@ -139,6 +139,7 @@ In this article, you learned how to specify the detection model to use with diff
139139

140140
* [Face .NET SDK](../quickstarts-sdk/identity-client-library.md?pivots=programming-language-csharp%253fpivots%253dprogramming-language-csharp)
141141
* [Face Python SDK](../quickstarts-sdk/identity-client-library.md?pivots=programming-language-python%253fpivots%253dprogramming-language-python)
142+
* [Face Java SDK](../quickstarts-sdk/identity-client-library.md?pivots=programming-language-java%253fpivots%253dprogramming-language-java)
142143
* [Face JavaScript SDK](../quickstarts-sdk/identity-client-library.md?pivots=programming-language-javascript%253fpivots%253dprogramming-language-javascript)
143144

144145
[Detect]: /rest/api/face/face-detection-operations/detect

articles/ai-services/computer-vision/how-to/specify-recognition-model.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ In this article, you learned how to specify the recognition model to use with di
132132

133133
* [Face .NET SDK](../quickstarts-sdk/identity-client-library.md?pivots=programming-language-csharp%253fpivots%253dprogramming-language-csharp)
134134
* [Face Python SDK](../quickstarts-sdk/identity-client-library.md?pivots=programming-language-python%253fpivots%253dprogramming-language-python)
135+
* [Face Java SDK](../quickstarts-sdk/identity-client-library.md?pivots=programming-language-java%253fpivots%253dprogramming-language-java)
135136
* [Face JavaScript SDK](../quickstarts-sdk/identity-client-library.md?pivots=programming-language-javascript%253fpivots%253dprogramming-language-javascript)
136137

137138
[Detect]: /rest/api/face/face-detection-operations/detect

0 commit comments

Comments
 (0)