【发布时间】:2013-09-30 13:14:39
【问题描述】:
我正在用 Python 编写一个简单的 Dropbox 应用来上传/下载文件。这是我正在使用的特定代码,它创建了与 Dropbox 帐户的连接。
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
# Have the user sign in and authorize this token
authorize_url = flow.start()
print '1. Go to: ' + authorize_url
print '2. Click "Allow" (you might have to log in first)'
print '3. Copy the authorization code.'
code = raw_input("Enter the authorization code here: ").strip()
# This will fail if the user enters an invalid authorization code
access_token, user_id = flow.finish(code)
client = dropbox.client.DropboxClient(access_token)
print 'linked account: ', client.account_info()
f = open('data.txt')
response = client.put_file('/magnum-opus.txt', f)
print 'uploaded: ', response
folder_metadata = client.metadata('/')
print 'metadata: ', folder_metadata
f, metadata = client.get_file_and_metadata('/magnum-opus.txt')
out = open('magnum-opus.txt', 'w')
out.write(f.read())
out.close()
print metadata
它似乎工作正常,但我对检查身份验证的方式持谨慎态度。上面的代码将在用户的默认浏览器中打开一个指向他/她的 Dropbox 帐户的链接,以允许访问此应用程序。但我想跳过这些步骤并获得明确的授权代码。我读过doc,但它对我没有帮助。有谁知道如何获取此代码?
【问题讨论】:
-
你能澄清你的问题吗?调用 Dropbox API 的唯一方法是使用 OAuth 令牌,这需要用户授权您的应用。
-
我想跳过浏览器中的授权过程,只想在脚本中获取授权码
-
这是不可能的。用户必须先授权您的应用,然后您才能访问他或她的帐户。
标签: python authentication oauth-2.0 dropbox dropbox-api