bic-org / Boxtech

Boxtech API is a global container database containing technical and safety information, such as tare weight, size/type and max stacking capacity for containers.
https://www.bic-boxtech.org
25 stars 7 forks source link

How to get OAth Token with HttpClient? #15

Closed ThorstenBruegge closed 5 years ago

ThorstenBruegge commented 5 years ago

I need to use HttpClient for the request. But I don't know how to get the token.

Here is the way I tried it:

private async void GetBicDataAsync()
    {
        HttpClient _bicAothClient;

        _bicAothClient = new HttpClient();
        _bicAothClient.DefaultRequestHeaders.Add("Authorization", "Basic YmljYXBwOmJpY3NlY3JldGFwcA==");

        var content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("grant_type", "password"),
            new KeyValuePair<string, string>("username", Constants.BicAothUser),
            new KeyValuePair<string, string>("password", Constants.BicAothPassword)
        });

        var result = await _bicAothClient.PostAsync("http://app.bic-boxtech.org/oauth/token", content);
    }

But when I use

var contentString = await result.Content.ReadAsStringAsync();

the result is

{"statusCode":400,"status":400,"code":400,"message":"Invalid request: method must be POST","name":"invalid_request"}

What did I wrong. I thing it's a small stupid fail. Can someone help me please.

AltiusRupert commented 5 years ago

Hi Thorsten, Did you follow the instructions here ? https://github.com/bic-boxtech/BIC-BoxTech-API-Samples/wiki/Authentication

I hope they help, Rupert

ThorstenBruegge commented 5 years ago

Hey,

Did you see the code I posted? Yes, if read the wiki, but there is no example that could help me.


From: Rupert Barrow notifications@github.com Sent: Wednesday, August 1, 2018 8:48:01 AM To: bic-boxtech/BIC-BoxTech-API-Samples Cc: Thorsten Brügge; Author Subject: Re: [bic-boxtech/BIC-BoxTech-API-Samples] How to get OAth Token with HttpClient? (#15)

Hi Thorsten, Did you follow the instructions here ? https://github.com/bic-boxtech/BIC-BoxTech-API-Samples/wiki/Authentication

I hope they help, Rupert

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/bic-boxtech/BIC-BoxTech-API-Samples/issues/15#issuecomment-409468029, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AZoAqS3MVmPriyj0jFNlUNfOX7OFsIu7ks5uMU8hgaJpZM4Vo8Xx.

BernierSFCloud commented 5 years ago

Hi,

looking at your code, I would try requesting the login access token over https instead of http var result = await _bicAothClient.PostAsync("https://app.bic-boxtech.org/oauth/token", content);

I have updated the wiki accordingly

ThorstenBruegge commented 5 years ago

Oh I see. Thank you. I'll try it soon.


From: Emmanuel Bernier notifications@github.com Sent: Wednesday, August 1, 2018 10:38:53 AM To: bic-boxtech/BIC-BoxTech-API-Samples Cc: Thorsten Brügge; Author Subject: Re: [bic-boxtech/BIC-BoxTech-API-Samples] How to get OAth Token with HttpClient? (#15)

Hi,

looking at your code, I would try requesting the login access token over https instead of http var result = await _bicAothClient.PostAsync("https://app.bic-boxtech.org/oauth/token", content);

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/bic-boxtech/BIC-BoxTech-API-Samples/issues/15#issuecomment-409497693, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AZoAqW2RI-ZO3PDohlYRZUqka-akv2Okks5uMWkdgaJpZM4Vo8Xx.

ThorstenBruegge commented 5 years ago

Ok. The docomentation for the GET request is wrong too. Hope you'll fix it. Here is an example, if you want to use the HttpClient for the requests:

private async void GetBicDataAsync()
        {
            //  HttpClient to get the token
            HttpClient bicAothClient;
            bicAothClient = new HttpClient();

            //  Add header
            bicAothClient.DefaultRequestHeaders.Add("Authorization", "Basic YmljYXBwOmJpY3NlY3JldGFwcA==");

            //  Set parameters
            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("grant_type", "password"),
                new KeyValuePair<string, string>("username", <USERNAME>),
                new KeyValuePair<string, string>("password", <PASSWORD>)
            });

            //  POST async
            var result = await _bicAothClient.PostAsync("https://app.bic-boxtech.org/oauth/token/", content);

            //  Get content as string
            var contentString = await result.Content.ReadAsStringAsync();

            //  Parse content, to get the accessToken
            var r = JToken.Parse(contentString);
            var token = r["accessToken"].Value<string>();

            //  HttpClient to get container data
            HttpClient _bicDataClient;
            bicDataClient = new HttpClient();

            //  Add accessToken to header
            bicDataClient.DefaultRequestHeaders.Add("Authorization", ("Bearer " + token));

            //  Get container data
            var bicData = await bicDataClient.GetAsync("https://app.bic-boxtech.org/api/v2.0/container/" + <CONTAINERNUMBER>);

            //  Get content as string
            var containerDataString = await bicData.Content.ReadAsStringAsync();

            //  Do what you need with this content
        }
AltiusRupert commented 5 years ago

Thanks @tkX85 Thorsten, we will do so. Does this fix your issue ?

ThorstenBruegge commented 5 years ago

Hey @AltiusRupert ,

yes with the code above and the right URLs I get the content of the container.

AltiusRupert commented 5 years ago

Thanks Thorsten