如何从 Python 自动化 Dropbox OAuth 身份验证

在某个全自动化的应用程序中,我们使用 Dropbox,我们需要使用 “dropboxfs”(一个使用 “pyfilesystem” 的 Dropbox 文件系统层)自动登录到 Dropbox。构造函数
https://github.com/btimby/fs-dropbox/blob/master/dropboxfs.py#L336
从 oauth 进程中获取令牌密钥 + 密钥。

我们是否能以某种方式自动化 oauth 进程?我不希望有任何手动交互,应用程序在其中启动具有 oauth 窗口的浏览器,并且我必须确认 oauth 访问请求。

应用程序密钥 + 密钥不是问题。但我只想向应用程序提供 Dropbox 用户名 + 密码,以便直接访问 Dropbox。

有没有什么办法?

2. 解决方案

我们可以使用名为 Splinter 的 Web 应用程序测试框架来解决此问题。它允许我们自动化浏览器操作。请查看文档页面。

以下是我们使用的代码:

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()
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值