简体   繁体   English

自动化Google Drive SDK授权

[英]Automate Google Drive SDK Authorization

I am stuck with a problem. 我遇到了问题。

#!/usr/bin/python

import httplib2
import pprint

from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow


# Copy your credentials from the 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)

# Insert a file
media_body = MediaFileUpload(FILENAME, mimetype='text/plain', resumable=True)
body = {
  'title': 'My document',
  'description': 'A test document',
  'mimeType': 'text/plain'
}

file = drive_service.files().insert(body=body, media_body=media_body).execute()
pprint.pprint(file)

The above code asks user to copy the url to the browser, and then authorize their account, and then again, copy-paste the code and paste it on the terminal. 上面的代码要求用户将URL复制到浏览器,然后授权他们的帐户,然后再复制粘贴代码并将其粘贴到终端上。 I know storing the credentials and using the refresh tokens, users will have to do this just for once. 我知道存储凭据并使用刷新令牌,用户必须只执行一次。

But, I don't want so much of user-interactions. 但是,我不想要那么多用户交互。 Is it possible that user authorizes by just logging in to their gmail account? 用户是否可以通过登录他们的Gmail帐户进行授权? As in, from my code itself, the authorization link should get open in a web browser without user doing it, and just signs in to his/her account, and that's it, authorization is done, and this login should also happen for just one time, as in, one time authorization, so that whatever is uploaded, gets uploaded on his Google Drive account and maintained. 从我的代码本身来看,授权链接应该在Web浏览器中打开而无需用户执行,只需登录到他/她的帐户,就是这样,授权就完成了,这个登录也应该只发生一个时间,如同一次授权,以便上传的内容,上传到他的Google云端硬盘帐户并进行维护。 The authorization code should be directly retrieved, and these credentials should be stored as usual and be used and tokens should also be refreshed. 应直接检索授权代码,并且这些凭证应像往常一样存储并使用,并且还应刷新令牌。

I came across Google Drive Service Account, good thing is that user-intervention is gone completely, but bad thing is that, it doesn't allow the account where the file is to be uploaded. 我遇到了Google云端硬盘服务帐户,好消息是用户干预完全消失了,但不好的是,它不允许上传文件的帐户。 It uploads the file on that drive who has created the app. 它会在创建应用程序的驱动器上传文件。

Can anyone pls help me out with this? 任何人都可以帮我解决这个问题吗? If going with the above code, then what should I be doing to automate the task? 如果使用上面的代码,那么我应该做些什么来自动完成任务呢? If going with the Service Account, what should I be doing to make the app upload the data to the user's own drive account? 如果使用服务帐户,我应该怎么做才能让应用程序将数据上传到用户自己的驱动器帐户?

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks! 谢谢!

I'm guessing that you have a local Python app (not web app) that you want to be able to access user's Drive. 我猜你有一个本地Python应用程序(不是网络应用程序),你希望能够访问用户的驱动器。 There is no reason why the authorisation needs to be done in your Python app. 没有理由需要在Python应用程序中完成授权。 So for example you could write a small web app that walks the user through the authentication process, then emails you or the user the appropriate strings to paste into the python app. 例如,您可以编写一个小型Web应用程序,引导用户完成身份验证过程,然后通过电子邮件向您或用户发送适当的字符串以粘贴到python应用程序中。

Did the same for DropBox. 对DropBox也一样。 You can integrate PyQt's or PySide QWebView (Webkit) where in you can put the auto open the URL. 您可以集成PyQt或PySide QWebView(Webkit),您可以在其中自动打开URL。 A mix of javascript and python can get the job done. 混合使用javascript和python可以完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM