Fulfilling Google Drive API OAuth2.0 Procedure w/o needing to find a verification code

匿名 (未验证) 提交于 2019-12-03 08:52:47

问题:

The Google Drive API has the following OAuth2.0 procedure from their quickstart to receive the drive_service 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)

Notice that you will be given the variable authorize_url which is printed out. You are supposed to visit it using a browser and then confirm that you allow Google Drive to access your information, which then allows you get a "verification code." Is there any way that I can avoid the step of manual intervention and create a program that automates this step?

回答1:

Yes, you can use web server to get OAuth callback which doesn't require any user interaction.

Basically, you set up your server to retrieve oauth code and add redirect uri to oauth flow so that oauth sends code to given uri instead of telling user to put code into the textbox.

Take a look at tools.run_flow() method at google-api-python-client. It has pretty handy code of local webserver oauth flow.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!