microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
python/ta/auth.py
24lines · modecode
| 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # Licensed under the MIT License. |
| 4 | |
| 5 | """Utility to set AZURE_OPENAI_API_KEY to a valid token. |
| 6 | |
| 7 | Usage: eval `./auth.py` |
| 8 | |
| 9 | NOTE: The token is only valid for a short time. |
| 10 | """ |
| 11 | |
| 12 | import sys |
| 13 | |
| 14 | from azure.identity import DeviceCodeCredential |
| 15 | |
| 16 | save_stdout = sys.stdout |
| 17 | sys.stdout = sys.stderr |
| 18 | |
| 19 | # TODO: Do something non-interactive. |
| 20 | credential = DeviceCodeCredential() |
| 21 | token = credential.get_token("https://cognitiveservices.azure.com/.default") |
| 22 | |
| 23 | sys.stdout = save_stdout |
| 24 | print(f"export AZURE_OPENAI_API_KEY={token.token}") |
| 25 | |