问题
Like others, I'm trying to get the Google refresh token to work in order to run scheduled tasks that copy and rename files.
When I first manually authenticate within a terminal, my url ends in &access_type=offline. However, when I go in and try to manually use gauth.Refresh() in ipython, it fails with the same error as when my credentials file expires:
pydrive.auth.RefreshError: No refresh_token found.Please set access_type of OAuth to offline.
How do I actually set access_type to offline? Any suggestions are greatly appreciated.
I have been here, here, and here trying to troubleshoot this.
My script:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
# Try to load saved client credentials
gauth.LoadCredentialsFile("GoogleDriveCredentials.txt")
if gauth.credentials is None:
# Authenticate if they're not there
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
# Refresh them if expired
print "Google Drive Token Expired, Refreshing"
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("GoogleDriveCredentials.txt")
drive = GoogleDrive(gauth)
My settings.yaml file:
client_config_backend: settings
client_config:
client_id: ###actual client_id###
client_secret: ###actual client_secret###
save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json
get_refresh_token: True
oauth_scope:
- https://www.googleapis.com/auth/drive
回答1:
I have been there and below works for me.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
gauth.LoadCredentialsFile(gauth)
Create a new folder with above code as quickstart.py in root dir.
Download client_sercrets.json from google drive api credentials and put it in root
Put your settings.yaml file in root folder
Run the quickstart.py from your console and it will open a browser ask you to authorise app.
Once this process is completed, it will create a credentials.json file in your root directory.
Access token should be able to refresh itself now.
Few things to note for your Drive API settings, it should be Web application type, in Authorize Javascript "http://localhost:8080" and in Authorized redirect URIs "http://localhost:8080/"
If this is successful, carry over "credentials.json", "client_secrets.json", "settings.yaml" files to your production root dir and it should work.
Hope this helps!
来源:https://stackoverflow.com/questions/32863213/pydrive-guath-refresh-and-refresh-token-issues