Mac OS X中使用Python的Google Drive自动授权
我已经为GoogleDrive编写了Python代码,它将图像文件上传到我的硬盘应用程序中。我有三个问题。这是我的密码:Mac OS X中使用Python的Google Drive自动授权,python,google-drive-api,google-oauth,google-api-client,google-api-python-client,Python,Google Drive Api,Google Oauth,Google Api Client,Google Api Python Client,我已经为GoogleDrive编写了Python代码,它将图像文件上传到我的硬盘应用程序中。我有三个问题。这是我的密码: #!/usr/bin/python import httplib2 import pprint from apiclient.discovery import build from apiclient.http import MediaFileUpload from oauth2client.client import OAuth2WebServerFlow from a
#!/usr/bin/python
import httplib2
import pprint
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow
from apiclient import errors
import sys
CLIENT_ID = 'CLIENT_ID'
CLIENT_SECRET = 'CLIENT_SECRET'
OAUTH_SCOPE = ['https://www.googleapis.com/auth/drive.readonly', 'https://www.googleapis.com/auth/drive.file']
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
FILENAME = "filepath/filename.png"
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
flow.params['access_type'] = 'offline'
flow.params['approval_prompt'] = 'force'
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)
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
media_body = MediaFileUpload(FILENAME, mimetype='image/png', resumable=True)
body = {
'title': 'Screen Shot 2013-11-03 at 3.54.08 AM',
'description': 'A test screenshot',
'mimeType': 'image/png'
}
file = drive_service.files().insert(body=body, media_body=media_body).execute()
new_permission = {
'type': 'anyone',
'role': 'reader'
}
try:
drive_service.permissions().insert(
fileId=file['id'], body=new_permission).execute()
except errors.HttpError, error:
print 'An error occurred: %s' % error
pprint.pprint(file)
我的问题是:
我如何让用户使用我的应用程序并将他们的图像上传到他们自己的谷歌硬盘
那么,刷新令牌是否用于此目的?如果是,那么如何在我的程序中实现它?
如果没有,那么我如何确保一旦我的应用程序被加载,该特定文件就可以直接上传到google drive上,而无需任何授权,或者以任何自动授权的方式,这样用户交互就完全停止了
嘿,乔,谢谢你回复!它对我很有效。。。。不幸的是,由于缺乏声誉,我无法投票支持你的答案。。。再次感谢……)嘿,Joe,我们有没有办法消除用户复制粘贴授权码的干预?我们可以用编程的方式来做吗?与中一样,一旦用户登录到他/她的帐户并接受并允许我们的应用程序访问和连接他的帐户,下一个,即生成的身份验证代码,将以编程方式读取。可能吗?是的,run_flow()代码将启动本地web服务器并接受包含代码的重定向,不应涉及任何剪切粘贴。如果不能在端口8080或8090上启动web服务器,代码将返回到剪切n粘贴。那么,是否100%不可能跳过第一次授权?