It is important to read the error response that is returned by the Google Analytics API server. In a lot of cases they can tell you exactly what is wrong.
400 invalidParameter
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidParameter",
"message": "Invalid value '-1' for max-results. Value must be within the range: [1, 1000]",
"locationType": "parameter",
"location": "max-results"
}
],
"code": 400,
"message": "Invalid value '-1' for max-results. Value must be within the range: [1, 1000]"
}
}
In this case the message:
"message": "Invalid value '-1' for max-results. Value must be within the range: [1, 1000]",
Is telling us that we sent a -1 value for the parameter max-results which is not valid we can only send a value from 1-1000.