首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Python自动化Dropbox OAuth身份验证

从Python自动化Dropbox OAuth身份验证
EN

Stack Overflow用户
提问于 2013-05-09 18:21:39
回答 3查看 1.8K关注 0票数 0

对于一个使用Dropbox的全自动化应用程序,我们需要使用'dropboxfs‘(一个使用’pyfilesystem‘的Dropbox FS层)自动登录到Dropbox。构造函数期望

https://github.com/btimby/fs-dropbox/blob/master/dropboxfs.py#L336

来自oauth进程的令牌密钥+密码。

我们能以某种方式自动化oauth过程吗?在应用程序使用oauth窗口启动浏览器以及必须确认oauth访问请求的情况下,我不会进行任何手动交互。

app key + secret不是问题所在。但我只想提供Dropbox用户名+密码给应用程序,以便直接访问Dropbox。

有什么选择吗?

EN

回答 3

Stack Overflow用户

发布于 2013-07-04 21:11:22

我面临着同样的挑战,我通过使用一个名为Splinter的web应用程序测试框架解决了这个问题。它允许我自动执行浏览器操作。查看documentation页面。

下面是我使用的代码:

代码语言:javascript
复制
from splinter import *
from dropbox import rest, session
from dropbox import client as dbclient
import time

# Initiate Dropbox API
APP_KEY = '###'
APP_SECRET = '###'
ACCESS_TYPE = 'dropbox'
sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
emailDropbox = '###'
passwordDropbox = '###'

request_token = sess.obtain_request_token()

urlDropbox = sess.build_authorize_url(request_token)

def phantomjsOAuth():
    # Target url
    print 'Target url: ', urlDropbox

    browser = Browser('phantomjs')
    print 'Starting phantomjs browser'
    print 'Visiting url'
    browser.visit(urlDropbox)

    # Email form
    print 'Is the email form present? ', browser.is_element_present_by_id('login_email')
    print 'Fill email form'
    browser.find_by_id('email-field').first.find_by_id('login_email').first.fill(emailDropbox)
    print 'Email form successfully filled'

    # Password form
    print 'Is the password form present? ', browser.is_element_present_by_id('login_password')
    print 'Fill password form'
    browser.find_by_id('login_password').first.fill(passwordDropbox)
    print 'Password form successfully filled'

    # Find login submit button
    print 'Is the "Submit" button present?', browser.is_element_present_by_name('login_submit_dummy')
    submitButton = browser.is_element_present_by_name('login_submit_dummy')

    if submitButton == True:
        print 'Pauzing for 5 seconds to avoid clicking errors'
        time.sleep(5)
        print 'Attempting to click the "Submit" button in order to login'
        browser.find_by_name('login_submit_dummy').first.click()
        print '"Submit" button successfully clicked'

        # Allow connection with Dropbox
        print 'Is the "Allow" button present?', browser.is_element_present_by_css('.freshbutton-blue')
        allowButton = browser.is_element_present_by_css('.freshbutton-blue')

        if allowButton == True:
            print 'The "Allow" button is present, attempting to click..'
            browser.find_by_css('.freshbutton-blue').click()
            print 'The "Allow" button is successfully clicked, access to Dropbox is granted.'

            dropboxCode()

        else:
            print 'The "Allow" button is not present, quitting.'
            browser.quit()

    else:
        print 'The "Submit" button was not present, quitting.'
        browser.quit()

def dropboxCode():
    # The rest of the Dropbox code
    # This will fail if the user didn't visit the above URL and hit 'Allow'
    access_token = sess.obtain_access_token(request_token)

    client = dbclient.DropboxClient(sess)
    print "linked account:", client.account_info()

    f = open('working-draft.txt')
    response = client.put_file('/magnum-opus.txt', f)
    print "uploaded:", response

phantomjsOAuth()
票数 1
EN

Stack Overflow用户

发布于 2013-05-10 00:42:18

Dropbox API目前需要OAuth,而这些术语禁止人们尝试自动化它的方式:

https://www.dropbox.com/terms#acceptable_use https://www.dropbox.com/developers/reference/bestpractice

(而且你真的不应该像那样存储你的用户名/密码。)

票数 0
EN

Stack Overflow用户

发布于 2013-05-10 00:01:17

OAuth是Dropbox Core API提供的唯一身份验证,您不能使用用户名+密码来访问。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16459504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档