I couldn't find a single python example on the web that explain how to get an access token to connect to google APIs using a pre-created refresh token.
Note that this is a sample code I wrote so it is far from being production ready. Don't forget to add values to REFRESH_TOKEN, CLIENT_ID, CLIENT_SECRET, REDIRECT_URI and OAUTH_SCOPE variables based on your app settings.
feel free to comment on the code below, Specially on my novice python coding skills.
#!/usr/bin/python
import httplib2
import pprint
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.client import OAuth2Credentials
from oauth2client import GOOGLE_AUTH_URI
from oauth2client import GOOGLE_REVOKE_URI
from oauth2client import GOOGLE_TOKEN_URI
#Refresh token
REFRESH_TOKEN = ""
# Copy your credentials from the console
CLIENT_ID = ''
CLIENT_SECRET = ''
## Check https://developers.google.com/drive/scopes for all available scopes
OAUTH_SCOPE = ['https://www.googleapis.com/auth/drive','http://localhost:5001/storage/getGDriveAuth']
## Redirect URI for installed apps
REDIRECT_URI = ''
credentials = OAuth2Credentials(None, CLIENT_ID,
CLIENT_SECRET, REFRESH_TOKEN, None,
GOOGLE_TOKEN_URI, None,
revoke_uri=GOOGLE_REVOKE_URI,
id_token=None,
token_response=None)
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
file = drive_service.files().list().execute()
pprint.pprint(file)
exit();
No comments:
Post a Comment