POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Devices/IotHubs/{resourceName}/IotHubKeys/{keyName}/listkeys?api-version=2023-06-30
URI Parameters
Name
In
Required
Type
Description
keyName
path
True
string
The name of the shared access policy.
resourceGroupName
path
True
string
The name of the resource group that contains the IoT hub.
This is a synchronous operation. The body contains a JSON-serialized shared access policy, including keys, that you can use to access one or more IoT hub endpoints.
POST https://management.azure.com/subscriptions/91d12660-3dec-467a-be2a-213b5544ddc0/resourceGroups/myResourceGroup/providers/Microsoft.Devices/IotHubs/testHub/IotHubKeys/iothubowner/listkeys?api-version=2023-06-30
from azure.identity import DefaultAzureCredential
from azure.mgmt.iothub import IotHubClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-iothub
# USAGE
python iothub_getkey.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""defmain():
client = IotHubClient(
credential=DefaultAzureCredential(),
subscription_id="91d12660-3dec-467a-be2a-213b5544ddc0",
)
response = client.iot_hub_resource.get_keys_for_key_name(
resource_group_name="myResourceGroup",
resource_name="testHub",
key_name="iothubowner",
)
print(response)
# x-ms-original-file: specification/iothub/resource-manager/Microsoft.Devices/stable/2023-06-30/examples/iothub_getkey.jsonif __name__ == "__main__":
main()
package armiothub_test
import (
"context""log""github.com/Azure/azure-sdk-for-go/sdk/azidentity""github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/iothub/armiothub"
)
// Generated from example definition: https://github.com/Azure/azure-rest-api-specs/blob/32c178f2467f792a322f56174be244135d2c907f/specification/iothub/resource-manager/Microsoft.Devices/stable/2023-06-30/examples/iothub_getkey.jsonfuncExampleResourceClient_GetKeysForKeyName() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armiothub.NewClientFactory("<subscription-id>", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
res, err := clientFactory.NewResourceClient().GetKeysForKeyName(ctx, "myResourceGroup", "testHub", "iothubowner", nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.// res.SharedAccessSignatureAuthorizationRule = armiothub.SharedAccessSignatureAuthorizationRule{// KeyName: to.Ptr("iothubowner"),// PrimaryKey: to.Ptr("<primaryKey>"),// Rights: to.Ptr(armiothub.AccessRightsRegistryWriteServiceConnectDeviceConnect),// SecondaryKey: to.Ptr("<secondaryKey>"),// }
}
const { IotHubClient } = require("@azure/arm-iothub");
const { DefaultAzureCredential } = require("@azure/identity");
/**
* This sample demonstrates how to Get a shared access policy by name from an IoT hub. For more information, see: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-security.
*
* @summary Get a shared access policy by name from an IoT hub. For more information, see: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-security.
* x-ms-original-file: specification/iothub/resource-manager/Microsoft.Devices/stable/2023-06-30/examples/iothub_getkey.json
*/asyncfunctioniotHubResourceGetKeysForKeyName() {
const subscriptionId =
process.env["IOTHUB_SUBSCRIPTION_ID"] || "91d12660-3dec-467a-be2a-213b5544ddc0";
const resourceGroupName = process.env["IOTHUB_RESOURCE_GROUP"] || "myResourceGroup";
const resourceName = "testHub";
const keyName = "iothubowner";
const credential = new DefaultAzureCredential();
const client = new IotHubClient(credential, subscriptionId);
const result = await client.iotHubResource.getKeysForKeyName(
resourceGroupName,
resourceName,
keyName
);
console.log(result);
}
using Azure;
using Azure.ResourceManager;
using System;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager.IotHub.Models;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.IotHub;
// Generated from example definition: specification/iothub/resource-manager/Microsoft.Devices/stable/2023-06-30/examples/iothub_getkey.json// this example is just showing the usage of "IotHubResource_GetKeysForKeyName" operation, for the dependent resources, they will have to be created separately.// get your azure access token, for more details of how Azure SDK get your access token, please refer to https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);
// this example assumes you already have this IotHubDescriptionResource created on azure// for more information of creating IotHubDescriptionResource, please refer to the document of IotHubDescriptionResourcestring subscriptionId = "91d12660-3dec-467a-be2a-213b5544ddc0";
string resourceGroupName = "myResourceGroup";
string resourceName = "testHub";
ResourceIdentifier iotHubDescriptionResourceId = IotHubDescriptionResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, resourceName);
IotHubDescriptionResource iotHubDescription = client.GetIotHubDescriptionResource(iotHubDescriptionResourceId);
// invoke the operationstring keyName = "iothubowner";
SharedAccessSignatureAuthorizationRule result = await iotHubDescription.GetKeysForKeyNameAsync(keyName);
Console.WriteLine($"Succeeded: {result}");