Write to google sheets

I was using api key to read from excel.
Same api key was used to write to an excel but an error is thrown.

Write Cell: Google.Apis.Requests.RequestError
Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. [401]
Errors [
Message[Login Required.] Location[Authorization - header] Reason[required] Domain[global]

how to write to an excel?