1

I want to download an excel file that's hosted on google docs. I have the file link (and thus the file id) and want to automate the process.
One thing to note is that I don't own the file, but my google account has permission to read it. I've seen some snippets of code from the google sheets api or pydrive but I still don't get the part about authentication, and have not been able to run the code successfully. I have 2 problems: (1) when setting the OAuth in the console I'm not sure what my URIs are supposed to be.
And (2) from what I've seen, this involves downloading a credentials json from the google console, which I've done but then running the script makes me go to a page to authenticate. This is not what I want to do, since this is an automated process that's going to run without user interaction. Is there a way that skip this step?

Edit: I've used http://localhost:8080 for the URI and http://localhost/ for the redirect, which seems to work for the given example but gives error 400 as soon as I change the scope to the link that I want

Joolz
  • 39
  • 5

1 Answers1

2
  1. Just use the defaults (or leave empty)

  2. You must authenticate once with your browser. After you do that, your credentials will be written to a file (token.json in the referenced example). Store that file safely to your local drive and use it in subsequent calls. You will not need to authenticate through UI again:

    if os.path.exists('token.json'):
             creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    
GoranK
  • 1,628
  • 2
  • 12
  • 22
  • I ended up using http://localhost:8080 for the URI and http://localhost/ for the redirect URI and it seems to work (though I don't know why). As you said, once the token.json file is saved there's no more interaction needed in subsequent runs. However, if I change the scope from the example to the file I need to read, it stops working and gives me Error 400: invalid_scope – Joolz Aug 16 '21 at 07:12
  • 1
    The score is not the url of the file, it is just a unique identifier. REF https://www.google.com/url?sa=t&source=web&rct=j&url=https://developers.google.com/identity/protocols/oauth2/scopes&ved=2ahUKEwjzoI_Eg7XyAhX5_7sIHeSaAkQQFnoECAgQAg&usg=AOvVaw0svrJI2wG6y6hmW8xeQ4no – GoranK Aug 16 '21 at 07:34
  • @Joolz The token is only granted for the original scopes. You need to delete the token.json file and reauthenticate after changing the scopes. – Rafa Guillermo Aug 17 '21 at 11:34
  • @RafaGuillermo I have left the scope as is from the quickstart and changed the sample spreadsheet id and range for the values in the document. I can reach the document, but have lost the ability to skip manual authentication after the first try. Now I have to delete the token.json file everytime I run this or I get ValueError: Authorized user info was not in the expected format, missing fields refresh_token. – Joolz Aug 18 '21 at 18:54
  • I also note that my token json has an expiry field – Joolz Aug 18 '21 at 18:59
  • 1
    [Check this answer out](https://stackoverflow.com/a/66857607/11551468). It sounds like you have an issue with the credentials file. – Rafa Guillermo Aug 20 '21 at 09:26