400 Bad Request occurred when requesting a new token


#1

I followed “OAuth 2.0 Authentication and Authorization” to request a token for my account both in Postman and Java program. I filled in all the four fields but failed to get token. Below error message was responded.
It looks like the input fields filled with wrong values, but there is same problem when I try to refresh my other account. I used same method generated token to my previous account before.
Anyone could help me to resolve the problem?

{
“error”: “input_error”,
“error_description”: “code must exist, client_id must exist, client_secret must exist, grant_type is not in list of valid values: authorization_code”,
“instance_id”: “2739a8b4-e7f2-44e2-8773-7685e69a2ac3”,
“errors”: [
{
“label”: “code”,
“format”: “%s must exist”,
“params”: [
“code”
]
},
{
“label”: “client_id”,
“format”: “%s must exist”,
“params”: [
“client_id”
]
},
{
“label”: “client_secret”,
“format”: “%s must exist”,
“params”: [
“client_secret”
]
},
{
“label”: “grant_type”,
“format”: “%s is not in list of valid values: %s”,
“params”: [
“grant_type”,
“authorization_code”
]
}
]
}

Thanks for you help!


#2

Resolved. Do not use JsonParameters, use url parameters. The document need to be corrected.


#3

Hmm, the request should work as documented, without the need for URL parameters (which we don’t recommend). I just tested it as documented and it worked for me.

I haven’t seen that specific error before so I’m not sure what’s wrong. Maybe double-check the Headers tab in Postman has a Content-Type parameter of “application/x-www-form-urlencoded”.


#4

Thank you for the update. I also tried again just now, but unfortunately, same error occurred.
Anyway, I can get token with json parameter.


#5

Greenpowerlabs, you should use x-www-form-urlencoded in the body, you are using form-data which will not work.
I made the same mistake but noticed the difference in jbumgardner’s screenshot.


#6

Hi Johan, Thank you so much, it works while using x-www-form-urlencoded.
Regards,


#7

I am having the same error, I am posting with ESP8266 using the following code:

const char* host = “api.home.nest.com”;
const int httpsPort = 443;
String url = “/oauth2/access_token”;

void loop() {

if (client.connect(host, httpsPort))
{

client.print(String("POST ") + url + " HTTP/1.1\r\n" +
             "Host: " + host + "\r\n" +
             "Content-Type: application/x-www-form-urlencoded\r\n" + 
             "client_id=c29xxxxxxxxxxxx1c5-4cbxxxxxxx209&client_secret=oUPJd4LBxxxxxxxxxxloN&grant_type=authorization_code&code=3SJxxxxxN\r\n\r\n"   

}

Any ideas what the error could be?
Thanks!