0

What is the easiest way to delete a sheet created by an API service account? Using the gspread API, I was able to create a sheet, and then share back to myself as editor. Now I can't delete because I am not the owner.

    name = 'mysheet'
    gc = gspread.service_account(filename=path_to_credentials)

    try:
        spreadsheet = gc.open(name)
    except gspread.SpreadsheetNotFound:
        spreadsheet = gc.create(name)
        sharewith = 'me@myedu.edu'
        spreadsheet.share(sharewith, perm_type='user',
                          notify=True, role='editor')

I want to reuse the service account credentials, but I am having trouble thinking about how to get the credentials in a Google OAuth object suitable to use with gspread.Client(). Below kind of resembles what I am thinking, but I feel I am missing something.

    oa = gspread.oauth(credentials_filename=path_to_credentials)
    c = gspread.Client(auth=oa)
    c.del_spreadsheet('<spreadsheetId>')

If anyone has insights to offer, I would listen.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • I have to apologize for my poor English skill. Unfortunately, I cannot understand `I want to reuse the service account credentials, but I am having trouble thinking about how to get the credentials in a Google OAuth object suitable to use with gspread.Client().`. Can I ask you about the detail of your goal? – Tanaike Jul 05 '22 at 22:57
  • I wish to use my service account credentials and gspread.oauth fails with **ValueError: Client secrets must be for a web or installed app.** File "~/venv/lib/python3.10/site-packages/google_auth_oauthlib/flow.py", line 162, in from_client_config raise ValueError("Client secrets must be for a web or installed app.") ValueError: Client secrets must be for a web or installed app. – Timothy Peiffer Jul 06 '22 at 23:27
  • It seems that the issue has already been identified in another issue. It looks like an answer has been provided, but I can't understand the answer that has been given. Ick. https://stackoverflow.com/questions/56445257/valueerror-client-secrets-must-be-for-a-web-or-installed-app – Timothy Peiffer Jul 06 '22 at 23:37
  • Thank you for replying. From your reply, for example, is this information useful? https://docs.gspread.org/en/v5.4.0/oauth2.html#for-bots-using-service-account – Tanaike Jul 06 '22 at 23:38
  • Sadly, no. Recall, I wish to start out with the json encoded service account. I would like to be able to delete a spreadsheet using gspread.Client(). ` oa = gspread.oauth(credentials_filename=path_to_credentials) c = gspread.Client(auth=oa) c.del_spreadsheet('') ` What do I need to do to the service account in order to use it within gspread.oauth, with the end goal of using gspread.Client() ? – Timothy Peiffer Jul 10 '22 at 22:53
  • Thank you for replying. I deeply apologize that my comment was not useful for your situation. This is due to my poor English skill. I deeply apologize for my poor English skill, again. About your reply, you want to use the hard copied JSON data of the service account instead of the JSON data is retrieved from a file. Is my understanding correct? – Tanaike Jul 10 '22 at 23:01
  • The json service encoded service account credentials are coming from a file. In the example cited, the file name is assigned to variable path_to_credentials. The json encode credentials were collected when the service account was created from the API console https://console.cloud.google.com/apis/credentials?project=myproject – Timothy Peiffer Jul 12 '22 at 09:39
  • Thank you for replying. I would like to support you. But, I have to apologize for my poor English skill, again. Unfortunately, I cannot still understand your question. But I would like to try to understand it. When I could correctly understand it, I would like to think of a solution. I would be grateful if you can forgive my poor English skill. – Tanaike Jul 12 '22 at 11:48
  • Thanks for trying. I am going to see if I can approach it another way. The reference below addresses the ***ValueError: Client secrets must be for a web or installed app***. https://stackoverflow.com/questions/56445257/valueerror-client-secrets-must-be-for-a-web-or-installed-app – Timothy Peiffer Jul 13 '22 at 13:23

0 Answers0