![](/img/trans.png)
[英]Azure Service Management API access via AD Oauth fails
[英]Create a user via the Azure API management Rest service
我是Azure API Management Rest Service的新手。 我創建了一個新的API管理,帶有sharedaccesstoken
。
using (HttpClient httpClient = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
request.Headers.Authorization =
new AuthenticationHeaderValue("SharedAccessSignature", sharedAccessSignature);
request.Content = new StringContent("{\"accountEnabled\": true,\"creationType\": \"LocalAccount\",\"displayName\": \"Alex Wu\",\"passwordProfile\": {\"password\": \"Test1234\",\"forceChangePasswordNextLogin\": false},\"signInNames\": [{\"type\": \"userName\",\"value\": \"AlexW\"},{\"type\": \"emailAddress\",\"value\": \"AlexW@example.com\"}]}");
HttpResponseMessage response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
responseBody = await response.Content.ReadAsStringAsync();
}
當我執行上面的代碼時,我得到一個錯誤:
{StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{ Date: Wed, 30 Mar 2016 19:38:15 GMT Content-Length: 73 Allow: GET Content-Type: application/json; charset=utf-8}}
有人可以幫助我前進,以便我能夠通過REST服務創建新用戶。
我只是閱讀了https://docs.microsoft.com/zh-cn/rest/api/apimanagement/user/createorupdate中指定的API參考。
對於初學者,您應該使用HttpMethod.Put而不是Post。 請求路徑應為基本url + / users / {unique uid}。 另外,據我所知,您嘗試傳遞的屬性不適用於此事務。
如果您實際上要完成其他工作,請創建一個Azure API管理用戶實體,請提出建議,我將嘗試進一步指導您。
您的回答確實幫助了我。 下面的實現使我完成了任務。
string requestUrl = string.Format("{0}/users/{1}?api-version={2}", baseUrl, userGuid, apiVersion);
string responseBody = null;
try
{
using (HttpClient httpClient = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Put, requestUrl);
request.Headers.Authorization =
new AuthenticationHeaderValue("SharedAccessSignature", sharedAccessSignature);
request.Content = new StringContent("{\"firstName\": \"MyFirstName\",\"lastName\": \"MyLastName\",\"email\": \"example@mail\",\"password\": \"Password;\",\"state\": \"active\"}", Encoding.UTF8,"application/json");
HttpResponseMessage response = await httpClient.SendAsync(request);`enter code here`
response.EnsureSuccessStatusCode();
responseBody = await response.Content.ReadAsStringAsync();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.