Running Google Drive OAuth2.0 APIs without having to look for a verification code

The Google Drive API has the following OAuth2.0 procedure from quickstart to get the drive_service command at the end:

# Copy your credentials from the APIs Console CLIENT_ID = 'YOUR_CLIENT_ID' CLIENT_SECRET = 'YOUR_CLIENT_SECRET' # Check https://developers.google.com/drive/scopes for all available scopes OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive' # Redirect URI for installed apps REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob' # Path to the file to upload FILENAME = 'document.txt' # Run through the OAuth flow and retrieve credentials flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI) authorize_url = flow.step1_get_authorize_url() print 'Go to the following link in your browser: ' + authorize_url code = raw_input('Enter verification code: ').strip() credentials = flow.step2_exchange(code) # 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) 

Note that you will be provided with the authorize_url variable, which will be printed. You must visit it using a browser and then confirm that you allow Google Drive to access your information, which allows you to get a "verification code". Is there a way to avoid the manual intervention step and create a program that automates this step?

+4
source share
1 answer

Yes, you can use a web server to receive an OAuth callback that does not require any user interaction.

Basically, you set up your server to receive the oauth code and added uri redirection to the oauth stream so that oauth sends the code to the specified uri, instead of telling the user to enter the code in the text field.

Take a look at tools.run_flow () on google-api-python-client. It has quite convenient code for the local stream of the web server.

+2
source

Source: https://habr.com/ru/post/1493772/


All Articles