【问题标题】:Automating the Dropbox OAuth authentication from Python从 Python 自动化 Dropbox OAuth 身份验证
【发布时间】:2013-05-09 10:21:38
【问题描述】:

对于我们使用 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。

有什么选择吗?

【问题讨论】:

    标签: python oauth dropbox dropbox-api


    【解决方案1】:

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

    这是我使用的代码:

    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()
    

    【讨论】:

    • 这似乎是一个合理的答案。 TS 有没有使用这种方法?我也很想在我的集成测试中自动化这一步
    【解决方案2】:

    Dropbox API 当前需要 OAuth,并且这些条款禁止人们尝试将其自动化的方式:

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

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

    【讨论】:

    • 我认为在集成测试中,这是相当合理的。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 2015-02-02
      • 1970-01-01
      • 2023-03-22
      • 2018-07-10
      相关资源
      最近更新 更多