C# Windows UWP programming, how to start?

The Netatmo API is a set of webservices allowing developers and third parties to access Netatmo device's data.
Post Reply
Bumbum
Posts: 1
Joined: 12 Aug 2018, 16:59

C# Windows UWP programming, how to start?

Post by Bumbum » 12 Aug 2018, 17:11

Hello,

I want to request my Netatmo weather data in a Windows 10 UWP app. But I have problems with my first request, getting the access_token. Here is my code:

Code: Select all

            using (var client = new HttpClient())
            {
                //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

                string JSON_parameters = "{\"grant_type\": \"password\", "
                                       + "\"client_id\": \"" + client_id + "\", "
                                       + "\"client_secret\": \"" + client_secret + "\", "
                                       + "\"username\": \"" + user_email + "\", "
                                       + "\"password\": \"" + user_password + "\"}";

                //HttpResponseMessage response = await client.PostAsync(new Uri("https://api.netatmo.com/oauth2/token"), new StringContent(JSON_parameters, System.Text.Encoding.UTF8, "application/json"));
                HttpResponseMessage response = await client.PostAsync(new Uri("https://api.netatmo.com/oauth2/token"), new StringContent(JSON_parameters, System.Text.Encoding.UTF8));
                string JSON_response = await response.Content.ReadAsStringAsync();
            }
        }
I have tried both Requestheaders and poth PostAsync methods. My JSON-string looks good, I think. I always get the following response:

{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}

Can anybode help?

best regards,
Bumbum

rudolph.io
Posts: 4
Joined: 27 Jul 2018, 20:04

Re: C# Windows UWP programming, how to start?

Post by rudolph.io » 13 Aug 2018, 22:35

Hey,

your request is invalid. The OAuth2 protocol defines this request must be form-urlencoded (https://tools.ietf.org/html/rfc6749#section-4.1.3 The client makes a request to the token endpoint by sending the following parameters using the "application/x-www-form-urlencoded" format). So JSON-Body will not work on this request.

Hope that helps

Post Reply

Return to “Netatmo API”