azure-sdk-for-cpp/sdk/identity/azure-identity/samples/azure_cli_credential.cpp
Larry Osterman 90089ad326
Cleaned up Azure::Core::Context API surface (#5676)
* Deprecated Azure::Core::ApplicationContext because its use is confusing and inconsistent with the original design. 

---------

Co-authored-by: Rick Winter <rick.winter@microsoft.com>
Co-authored-by: Anton Kolesnyk <41349689+antkmsft@users.noreply.github.com>
Co-authored-by: Ahson Khan <ahkha@microsoft.com>
2024-07-17 12:38:30 -07:00

34 lines
951 B
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include <azure/identity/azure_cli_credential.hpp>
#include <azure/service/client.hpp>
#include <iostream>
int main()
{
try
{
// Step 1: Initialize Azure CLI Credential.
auto azureCliCredential = std::make_shared<Azure::Identity::AzureCliCredential>();
// Step 2: Pass the credential to an Azure Service Client.
Azure::Service::Client azureServiceClient("serviceUrl", azureCliCredential);
// Step 3: Start using the Azure Service Client.
azureServiceClient.DoSomething(Azure::Core::Context{});
std::cout << "Success!" << std::endl;
}
catch (const Azure::Core::Credentials::AuthenticationException& exception)
{
// Step 4: Handle authentication errors, if needed
// (Azure CLI invocation errors or process timeout).
std::cout << "Authentication error: " << exception.what() << std::endl;
return 1;
}
return 0;
}