0

i am trying to create a bot in python and i want it to sum up the info in google sheets but when i am using api key i can only read and cant edit is there something like api key that will work all the time and will do that i dont need to log in manually? (the error it gives me is error 401)

def main():
    key = My_API_Key
    discoveryUrl = 'https://sheets.googleapis.com/$discovery/rest?version=v4'
    service = discovery.build('sheets', 'v4', http=httplib2.Http(), discoveryServiceUrl=discoveryUrl, developerKey=key)

    spreadsheetId = '1z0jzLs0-OXu0I2Cw2BIPEdVzjgGZ_BPx0OcKimmfAhY'

    clear_values_request_body = {}
    service.spreadsheets().values().update(spreadsheetId=spreadsheetId, range='Sheet1', body=clear_values_request_body).execute()

this is what i have done before and it didnt work

1 Answers1

1

Yes, you can use Google Credentials to create an Service Account. Here are some instructions on how to use Service Accounts. When you create one, you also get an email address. Simply share your sheet with that address, and it will be able to authenticate using the key.

EDIT: Scroll to the bottom of this page for more detailed API info.

ameddin73
  • 146
  • 6
  • i tried doing this but when i share with this email adress its sending me email and tell me there was a problem – ofer kogman May 05 '20 at 03:54
  • This is the correct answer. Although it looks like an email you cannot send neither receive mail there, just take that into account. There are multiple examples on how to do that ([1](https://medium.com/@denisluiz/python-with-google-sheets-service-account-step-by-step-8f74c26ed28e), [2](https://developers.google.com/sheets/api/guides/authorizing), [3](https://stackoverflow.com/questions/27067825/how-to-access-google-spreadsheets-with-a-service-account-credentials)). Also you can take a look at [this page](https://developers.google.com/identity/protocols/oauth2/service-account) for completion. – Raserhin May 05 '20 at 07:32
  • but using oath2 isnt for all the time i will need to give new access code once a month i want something like api key that will work all the time without getting new one bc its for a bot that will run online – ofer kogman May 05 '20 at 13:14