Azure EventHubs REST API 401 未经授权
Azure EventHubs REST API 401 Unauthorized
我正在尝试使用 curl 执行 eventhubs restapi,但它不起作用。
卷曲
curl --proxy $PROXY -i -X "GET" "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.EventHub/namespaces/$NAME_SPACE/eventhubs/$EVENTHUB?api-version=2017-04-01" -H "Authorization: $TOKEN"
生成 SAS (C#)
var resourceuri = "https://myspacename.servicebus.windows.net/myeventhub";
var key = "mykey";
var keyname = "mykeyname";
TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
var week = 60 * 60 * 24 * 7;
var expiry = Convert.ToString((int)sinceEpoch.TotalSeconds + week);
string stringToSign = Uri.EscapeDataString(resourceuri) + "\n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
var sasToken = String.Format("SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", Uri.EscapeDataString(resourceuri), Uri.EscapeDataString(signature), expiry, keyname);
return sasToken;
回应
{"error":{"code":"AuthenticationFailedInvalidHeader","message":"Authentication failed. The 'Authorization' header is provided in an invalid format."}}
我参考了此文档 (https://docs.microsoft.com/ja-jp/rest/api/eventhub/Generate-SAS-token?redirectedfrom=MSDN)。
此外,我尝试使用此工具 (https://github.com/sandrinodimattia/RedDog/releases/tag/0.2.0.1),但结果也是一样。
你有什么想法吗?
API(https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.EventHub/namespaces/$NAME_SPACE/eventhubs/$EVENTHUB?api-version=2017-04-01
) 是一个 Azure Rest API。您可以获得如下令牌:
curl -X "POST" "https://login.microsoftonline.com/$TENANTID/oauth2/token" \
-H "Cookie: flight-uxoptin=true; stsservicecookie=ests; x-ms-gateway-slice=productionb; stsservicecookie=ests" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "client_id=$APPID" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "client_secret=$PASSWORD" \
--data-urlencode "resource=https://management.azure.com/"
当你请求API时,在Header中,你应该使用-H "Authorization: Bearer $token"
。所以,你应该像下面这样修改:
curl --proxy $PROXY -i -X "GET" "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.EventHub/namespaces/$NAME_SPACE/eventhubs/$EVENTHUB?api-version=2017-04-01" -H "Authorization: Bearer $token"
我正在尝试使用 curl 执行 eventhubs restapi,但它不起作用。
卷曲
curl --proxy $PROXY -i -X "GET" "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.EventHub/namespaces/$NAME_SPACE/eventhubs/$EVENTHUB?api-version=2017-04-01" -H "Authorization: $TOKEN"
生成 SAS (C#)
var resourceuri = "https://myspacename.servicebus.windows.net/myeventhub";
var key = "mykey";
var keyname = "mykeyname";
TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
var week = 60 * 60 * 24 * 7;
var expiry = Convert.ToString((int)sinceEpoch.TotalSeconds + week);
string stringToSign = Uri.EscapeDataString(resourceuri) + "\n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
var sasToken = String.Format("SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", Uri.EscapeDataString(resourceuri), Uri.EscapeDataString(signature), expiry, keyname);
return sasToken;
回应
{"error":{"code":"AuthenticationFailedInvalidHeader","message":"Authentication failed. The 'Authorization' header is provided in an invalid format."}}
我参考了此文档 (https://docs.microsoft.com/ja-jp/rest/api/eventhub/Generate-SAS-token?redirectedfrom=MSDN)。 此外,我尝试使用此工具 (https://github.com/sandrinodimattia/RedDog/releases/tag/0.2.0.1),但结果也是一样。
你有什么想法吗?
API(https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.EventHub/namespaces/$NAME_SPACE/eventhubs/$EVENTHUB?api-version=2017-04-01
) 是一个 Azure Rest API。您可以获得如下令牌:
curl -X "POST" "https://login.microsoftonline.com/$TENANTID/oauth2/token" \
-H "Cookie: flight-uxoptin=true; stsservicecookie=ests; x-ms-gateway-slice=productionb; stsservicecookie=ests" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "client_id=$APPID" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "client_secret=$PASSWORD" \
--data-urlencode "resource=https://management.azure.com/"
当你请求API时,在Header中,你应该使用-H "Authorization: Bearer $token"
。所以,你应该像下面这样修改:
curl --proxy $PROXY -i -X "GET" "https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.EventHub/namespaces/$NAME_SPACE/eventhubs/$EVENTHUB?api-version=2017-04-01" -H "Authorization: Bearer $token"