-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Support AAD Authentication in Form Recognizer #11156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
71154c6
added AAD support
f90c671
resolve conflict
1f744ab
address feedbacks
aacc161
resolve conflict
dd1786e
resolve conflict
083b52d
address iscai feedbacks
d04924d
refactor
68d1868
remove non-used json
bc653df
add api key back
1472dbe
add aad to training client
e887003
add license title
4ee0910
validate null of account properoty values instead of real value
70d6222
hide model id
3c6a265
minor feedbacks
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
133 changes: 133 additions & 0 deletions
133
.../azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/Authentication.java
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.ai.formrecognizer; | ||
|
|
||
| import com.azure.ai.formrecognizer.models.AccountProperties; | ||
| import com.azure.ai.formrecognizer.models.OperationResult; | ||
| import com.azure.ai.formrecognizer.models.RecognizedReceipt; | ||
| import com.azure.ai.formrecognizer.models.USReceipt; | ||
| import com.azure.ai.formrecognizer.training.FormTrainingClient; | ||
| import com.azure.ai.formrecognizer.training.FormTrainingClientBuilder; | ||
| import com.azure.core.credential.AzureKeyCredential; | ||
| import com.azure.core.util.polling.SyncPoller; | ||
| import com.azure.identity.DefaultAzureCredentialBuilder; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Samples for two supported methods of authentication in Form Recognizer and Form Training clients: | ||
| * 1) Use a Form Recognizer API key with AzureKeyCredential from azure.core.credentials | ||
| * 2) Use a token credential from azure-identity to authenticate with Azure Active Directory | ||
| */ | ||
| public class Authentication { | ||
| /** | ||
| * Main method to invoke this demo. | ||
| * | ||
| * @param args Unused arguments to the program. | ||
| * | ||
| * @throws IOException Exception thrown when there is an error in reading all the bytes from the File. | ||
| */ | ||
| public static void main(String[] args) { | ||
| /* | ||
| Set the environment variables with your own values before running the sample: | ||
|
|
||
| 1) AZURE_FORM_RECOGNIZER_ENDPOINT - the endpoint to your Form Recognizer resource. | ||
| 2) AZURE_FORM_RECOGNIZER_KEY - your Form Recognizer API key | ||
| 3) AZURE_CLIENT_ID - the client ID of your active directory application. | ||
| 4) AZURE_TENANT_ID - the tenant ID of your active directory application. | ||
| 5) AZURE_CLIENT_SECRET - the secret of your active directory application. | ||
| */ | ||
| // Form recognizer client: Key credential | ||
| authenticationWithKeyCredentialFormRecognizerClient(); | ||
| // Form recognizer client: Azure Active Directory | ||
| authenticationWithAzureActiveDirectoryFormRecognizerClient(); | ||
| // Form training client: Key credential | ||
| authenticationWithKeyCredentialFormTrainingClient(); | ||
| // Form training client: Azure Active Directory | ||
| authenticationWithAzureActiveDirectoryFormTrainingClient(); | ||
| } | ||
|
|
||
| private static void authenticationWithKeyCredentialFormRecognizerClient() { | ||
| FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder() | ||
| .credential(new AzureKeyCredential("{key}")) | ||
| .endpoint("{endpoint}") | ||
| .buildClient(); | ||
| beginRecognizeCustomFormsFromUrl(formRecognizerClient); | ||
| } | ||
|
|
||
| private static void authenticationWithAzureActiveDirectoryFormRecognizerClient() { | ||
| FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder() | ||
| .credential(new DefaultAzureCredentialBuilder().build()) | ||
| .endpoint("{endpoint}") | ||
| .buildClient(); | ||
| beginRecognizeCustomFormsFromUrl(formRecognizerClient); | ||
| } | ||
|
|
||
| private static void authenticationWithKeyCredentialFormTrainingClient() { | ||
| FormTrainingClient formTrainingClient = new FormTrainingClientBuilder() | ||
| .credential(new AzureKeyCredential("{key}")) | ||
| .endpoint("{endpoint}") | ||
| .buildClient(); | ||
| getAccountProperties(formTrainingClient); | ||
| } | ||
|
|
||
| private static void authenticationWithAzureActiveDirectoryFormTrainingClient() { | ||
| FormTrainingClient formTrainingClient = new FormTrainingClientBuilder() | ||
| .credential(new DefaultAzureCredentialBuilder().build()) | ||
| .endpoint("{endpoint}") | ||
| .buildClient(); | ||
| getAccountProperties(formTrainingClient); | ||
| } | ||
|
|
||
| private static void beginRecognizeCustomFormsFromUrl(FormRecognizerClient formRecognizerClient) { | ||
| String receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/sample-forms/receipts/contoso-allinone.jpg"; | ||
|
|
||
| SyncPoller<OperationResult, List<RecognizedReceipt>> recognizeReceiptPoller = | ||
| formRecognizerClient.beginRecognizeReceiptsFromUrl(receiptUrl); | ||
|
|
||
| List<RecognizedReceipt> receiptPageResults = recognizeReceiptPoller.getFinalResult(); | ||
|
|
||
| for (int i = 0; i < receiptPageResults.size(); i++) { | ||
| final RecognizedReceipt recognizedReceipt = receiptPageResults.get(i); | ||
| System.out.printf("----------- Recognized Receipt page %s -----------", i); | ||
| USReceipt usReceipt = ReceiptExtensions.asUSReceipt(recognizedReceipt); | ||
| System.out.printf("Merchant Name: %s, confidence: %.2f%n", usReceipt.getMerchantName().getFieldValue(), | ||
| usReceipt.getMerchantName().getConfidence()); | ||
| System.out.printf("Merchant Address: %s, confidence: %.2f%n", usReceipt.getMerchantAddress().getName(), | ||
| usReceipt.getMerchantAddress().getConfidence()); | ||
| System.out.printf("Merchant Phone Number %s, confidence: %.2f%n", | ||
| usReceipt.getMerchantPhoneNumber().getFieldValue(), usReceipt.getMerchantPhoneNumber().getConfidence()); | ||
| System.out.printf("Total: %s confidence: %.2f%n", usReceipt.getTotal().getName(), | ||
| usReceipt.getTotal().getConfidence()); | ||
| System.out.printf("Receipt Items: %n"); | ||
| usReceipt.getReceiptItems().forEach(receiptItem -> { | ||
| if (receiptItem.getName() != null) { | ||
| System.out.printf("Name: %s, confidence: %.2f%n", receiptItem.getName().getFieldValue(), | ||
| receiptItem.getName().getConfidence()); | ||
| } | ||
| if (receiptItem.getQuantity() != null) { | ||
| System.out.printf("Quantity: %s, confidence: %.2f%n", receiptItem.getQuantity().getFieldValue(), | ||
| receiptItem.getQuantity().getConfidence()); | ||
| } | ||
| if (receiptItem.getPrice() != null) { | ||
| System.out.printf("Price: %s, confidence: %.2f%n", receiptItem.getPrice().getFieldValue(), | ||
| receiptItem.getPrice().getConfidence()); | ||
| } | ||
| if (receiptItem.getTotalPrice() != null) { | ||
| System.out.printf("Total Price: %s, confidence: %.2f%n", | ||
| receiptItem.getTotalPrice().getFieldValue(), receiptItem.getTotalPrice().getConfidence()); | ||
| } | ||
| }); | ||
| System.out.print("-----------------------------------"); | ||
| } | ||
| } | ||
|
|
||
| private static void getAccountProperties(FormTrainingClient formTrainingClient) { | ||
| AccountProperties accountProperties = formTrainingClient.getAccountProperties(); | ||
| System.out.printf("Max number of models that can be trained for this account: %s%n", | ||
| accountProperties.getCustomModelLimit()); | ||
| System.out.printf("Current count of trained custom models: %d%n", accountProperties.getCustomModelCount()); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.