How can I automated getting Oauth2.0 bearer token from google api?

I am trying to automated the google gmail api, which I can read and get messages. However the oauth token expires and I can only seem to authenticate with ‘browser’ I have tried to post the the details to the endpoints but it wont work. How can I automate this progress so google can get the refresh token?

I am posting my details to - https://accounts.google.com/o/oauth2/token

but i am getting this in the response body -

{

"error": "invalid_request",

"error_description": "Missing required parameter: code"

}

any help would be amazing.