Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 自动执行Google Drive SDK授权_Python_Google Api_Google Drive Api_Google Oauth_Google Api Python Client - Fatal编程技术网

Python 自动执行Google Drive SDK授权

Python 自动执行Google Drive SDK授权,python,google-api,google-drive-api,google-oauth,google-api-python-client,Python,Google Api,Google Drive Api,Google Oauth,Google Api Python Client,我被一个问题困住了 #!/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 = 'Y

我被一个问题困住了

#!/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)
上面的代码要求用户将url复制到浏览器,然后授权他们的帐户,然后再次复制粘贴代码并粘贴到终端上。我知道存储凭证和使用刷新令牌,用户只需这样做一次

但是,我不想要那么多的用户交互。用户是否可以通过登录他们的gmail帐户进行授权?从我的代码本身来看,授权链接应该在web浏览器中打开,用户不必这样做,只需登录到他/她的帐户,就这样,授权完成了,并且此登录也应该只发生一次,就像一次授权一样,以便上传任何内容,上传到他的Google Drive帐户并进行维护。应该直接检索授权代码,这些凭证应该像往常一样存储并使用,令牌也应该刷新

我遇到了谷歌硬盘服务帐户,好的是用户干预完全消失了,但坏的是,它不允许上传文件的帐户。它将文件上载到创建应用程序的驱动器上

谁能帮我解决这个问题吗?如果使用上述代码,那么我应该做些什么来自动化任务?如果使用服务帐户,我应该如何让应用程序将数据上传到用户自己的驱动器帐户

任何帮助都将不胜感激


谢谢

对DropBox也做了同样的操作。您可以集成PyQt或PySide QWebView(Webkit),在其中可以自动打开URL。javascript和python的混合可以完成这项工作。

我猜您有一个本地python应用程序(而不是web应用程序),希望能够访问用户的驱动器。没有理由需要在Python应用程序中进行授权。例如,您可以编写一个小型web应用程序,引导用户完成身份验证过程,然后通过电子邮件向您或用户发送适当的字符串以粘贴到python应用程序中。

嘿,感谢您还原。。。但是,我怎样才能直接获得身份验证代码呢?您可以访问其中的完整页面和数据。您可以使整个过程自动化。只需尝试PyQt/Pyside的简单QWebView示例。。用户将在其系统上安装此应用程序…它类似于新的dropbox测试版。。。。。