400 bad request

martin

Dear All,

by using postman, I am doing the following post request

https://api.netatmo.com/oauth2/token?grant_type=authorization_code&client_id=6xxxxxxxxx&client_secret=Yxxxxxx&code=fxxxxxxxxxxxxxxx8e68e3&redirect_uri=https%3A%2F%2Fwww.myurl.de%2Fdashboard%2Fmy.aspx&scope=read_station+read_bubendorff+write_bubendorff

 

this works for a very long time, unfortunately it stops working a while ago and I am not able to figure out why it has stopped working the answer is always

 

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

 

what has changed?

0

Comentarios

3 comentarios

  • Comment author
    Leslie Community moderator

    Hello Martin,

    You must not use URL parameters for the /token request (it's only for the /authorize one). You must put them in the Body of the request. I'm surprised if it worked previously as it's not the way it works in OAuth2 protocol

    Have a good day,

    Leslie - Community Manager

    0
  • Comment author
    martin
    • Editado

    Hi Leslie thanks for the answer of course Postman is sending the Data in the body.

    As an example here a code snippet I am using to create the tokenrequest

     

            private const string TokenUrl = "https://api.netatmo.com/oauth2/token";
            private const string TokenFile = "netatmo_token.json";

            private const string ClientId = "68xxxx";
            private const string ClientSecret = "I3xxxx";
            private const string RedirectUri = "https://www.xxxxxx.de/dashboard/Wetter.aspx";
            private const string Scope = "read_station read_bubendorff write_bubendorff";
            private const string InitialCode = "6d46e5e731384564d8e5c851da72f709";

            private JObject FetchTokenWithAuthCode()
            {
                using (var client = new HttpClient())
                {
                    var content = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair<string, string>("grant_type", "authorization_code"),
                        new KeyValuePair<string, string>("client_id", ClientId),
                        new KeyValuePair<string, string>("client_secret", ClientSecret),
                        new KeyValuePair<string, string>("code", InitialCode),
                        new KeyValuePair<string, string>("redirect_uri", RedirectUri),
                        new KeyValuePair<string, string>("scope", Scope)
                    });

                    content.Headers.ContentType.CharSet = "UTF-8";

                    var response = client.PostAsync(TokenUrl, content).Result;
                    response.EnsureSuccessStatusCode();

                    string json = response.Content.ReadAsStringAsync().Result;
                    JObject token = JObject.Parse(json);
                    token["timestamp_utc"] = DateTime.UtcNow;
                    return token;
                }
            }

     

    Initial code is created by calling this url, and the copy pasted into the code

    https://api.netatmo.com/oauth2/authorize?client_id=xxxxxxxxxxxxxxxx&redirect_uri=https://www.die-xxxxxxx.de/dashboard/Wetter.aspx&scope=read_station read_bubendorff write_bubendorff&state=code

     

    by executing I still get 400 Bad request, maybe you can ive me a hint in which direction I have to look.

    thanks

    Martin

     

    0
  • Comment author
    martin

    I fixed it by resetting the keys and husing the new credentials

     

    Martin

    0

Iniciar sesión para dejar un comentario.