Search code examples
c#asp.netasp.net-mvc-4amadeus

Amadeus for Developers, don't know how to configure?


So, for my homework, I need to make an application that will not use SQL(as I used to), but the rest api. Problem is that I never done this, and I don't know how to configure it.

So far I got this:

string strUrlTest = String.Format("https://test.api.amadeus.com/v1/shopping/flight-offers");
WebRequest requestObjGet = WebRequest.Create(strUrlTest);

requestObjGet.Method = "GET";
requestObjGet.Headers.Add("API KEY", 
"API SECRET);

HttpWebResponse responseObjGet = null;
responseObjGet = (HttpWebResponse)requestObjGet.GetResponse();

string strResultTest = null;
using(Stream stream = responseObjGet.GetResponseStream())
{
    StreamReader sr = new StreamReader(stream);
    strResultTest = sr.ReadToEnd();
    sr.Close();
}

I just wanted to see with debugger if I got my all my data, but my program crashes at

responseObjGet = (HttpWebResponse)requestObjGet.GetResponse();

Could you help me figure it out?


Solution

  • I wrote a code example in C#, replace apikey and apisecret by the ones you get on the portal by creating an application. You can find a guide here.

    using System;
    using System.Collections.Generic;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text;
    using System.Threading.Tasks;
    using Newtonsoft.Json.Linq;
    
    namespace testApp
    {
        public class AmadeusTest
        {
            static void Main()
            {
                const string URL = "https://test.api.amadeus.com/v1/shopping/flight-destinations?origin=BOS";
                string token = getToken();
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(URL);
                client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Bearer " + token);
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "");
    
                Task<HttpResponseMessage> response = client.SendAsync(request);
                string myJsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(response.Result.Content.ReadAsStringAsync().Result).ToString();
                JObject jsonObject = JObject.Parse(myJsonResponse);
    
                Console.WriteLine(myJsonResponse);
                Console.WriteLine(jsonObject["data"][0]["destination"]);
                client.Dispose();
        }
    
        private static string getToken()
        {
            const string apikey = "";
            const string apisecret = "";
            const string tokenURL = "https://test.api.amadeus.com/v1/security/oauth2/token";
    
            string postData = $"grant_type=client_credentials&client_id={apikey}&client_secret={apisecret}";
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(tokenURL);
    
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "");
            request.Content = new StringContent(postData,
                                    Encoding.UTF8,
                                    "application/x-www-form-urlencoded");
    
            Task<HttpResponseMessage> response = client.SendAsync(request);
            if (response.Result.IsSuccessStatusCode)
            {
                string myJsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(response.Result.Content.ReadAsStringAsync().Result).ToString();
                JObject jsonObject = JObject.Parse(myJsonResponse);
                client.Dispose();
    
                string token = (string)jsonObject["access_token"];
                return token;
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.Result.StatusCode, response.Result.ReasonPhrase);
                return response.Result.ReasonPhrase;
            }
          }
       }
    }
    

    The getToken method is in charge of doing the authorization process (as explained here).

    This token is used in the API call (in the Main method). It is added to the Authorization header with the value: Bearer {token}