locked
How to create API using Azure API Management REST API in C# RRS feed

  • Question

  • Can Anyone suggest me for creating API that uses Azure API Management REST API in C#. I Tried but not able to call create an API Thanks in Advance

     

     private static async Task<string> CreateApiTask(string apiId)
    {
      string requestUrl = string.Format("{0}/apis/{1}?api-version={2}", baseUrl, Guid.NewGuid(), apiVersion);
      using (HttpClient httpClient = new HttpClient())
      {
       httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
       var request = new HttpRequestMessage(HttpMethod.Put, requestUrl);             
       request.Headers.Authorization = new AuthenticationHeaderValue(
         "SharedAccessSignature",
           sharedAccessSignature);
       // var model = new RequestBody();
       string str = JsonConvert.SerializeObject(new RequestBody());
       equest.Content = new StringContent(str, Encoding.UTF8, "application/json");              
       HttpResponseMessage response = await httpClient.PutAsync(requestUrl, request.Content); //// (requestUrl, model);
       string responseBody = await response.Content.ReadAsStringAsync();
       return responseBody;
       }
    }

    Error:
    {StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    {
      Date: Wed, 19 Aug 2015 07:19:34 GMT
      WWW-Authenticate: SharedAccessSignature realm=""
      Content-Length: 0
    }}


    Wednesday, August 19, 2015 7:07 AM

Answers

  • Hi, It looks like you are using an invalid SAS in the header. 
    • Marked as answer by Asha99 Wednesday, August 26, 2015 10:16 AM
    Monday, August 24, 2015 8:25 PM
  • Hi,

    You need to set the header name as Authorization, See https://msdn.microsoft.com/en-us/library/azure/dn776326.aspx i.e. for instance 

     // Define the required headers to specify the API version and operation type.
    request.Method = "GET";
    request.Headers.Add("Authorization", sharedAccessSignature);

    Header name SharedAccessSignature only works for resources like apis.

    Kind Regards,

    Steef-Jan Wiggers (Microsoft Azure MVP)


    BizTalk

    Tuesday, March 15, 2016 1:23 PM

All replies

  • Hello swapnil,

    I am not sure about this but I have doubt in SharedAccessSignature basic authentication. I suggest you to cross check credentials passed to shared Access Signature.

    In the meanwhile I am researching more with regards to your request and I will get back to you with update.

    Thank you for understanding.

    Regards,
    Sunil.HP

    Wednesday, August 19, 2015 6:18 PM
  • Hi, It looks like you are using an invalid SAS in the header. 
    • Marked as answer by Asha99 Wednesday, August 26, 2015 10:16 AM
    Monday, August 24, 2015 8:25 PM
  • Hi,

    You need to set the header name as Authorization, See https://msdn.microsoft.com/en-us/library/azure/dn776326.aspx i.e. for instance 

     // Define the required headers to specify the API version and operation type.
    request.Method = "GET";
    request.Headers.Add("Authorization", sharedAccessSignature);

    Header name SharedAccessSignature only works for resources like apis.

    Kind Regards,

    Steef-Jan Wiggers (Microsoft Azure MVP)


    BizTalk

    Tuesday, March 15, 2016 1:23 PM