1
我想通過官方的Dropbox API登錄到Dropbox並上傳文件到我的Dropbox。在Dropbox API中自動化OAuth - 點擊提交按鈕登錄
代碼似乎沒有點擊提交按鈕才能登錄到Dropbox。代碼不會阻止它掛起或凍結。我沒有遇到錯誤,所以沒有回溯。
奇怪的是,當我註釋掉填寫電子郵件或密碼(或兩者)時,單擊提交按鈕的作品。
我不想手動訪問Dropbox身份驗證鏈接,然後單擊允許按鈕。所以我試圖通過使用工具(Splinter)讓我自動執行瀏覽器操作來自動執行該任務。
對於我的代碼我使用Splinter和瀏覽器類型我使用PhantomJS
下面的代碼:
from splinter import *
from dropbox import client, rest, session
# 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)
# Start Splinter login code
# Assumes you are not logged in to Dropbox
# Target url
print 'Target url: ', urlDropbox
browser = Browser('phantomjs')
print 'Starting 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('login_email').first.fill(emailDropbox)
# 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)
# Login submit button
print 'Is the submit button present?', browser.is_element_present_by_name('login_submit_dummy')
# Click submit button
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_id('allow_access')
browser.find_by_id('allow_access').click()
print 'The "Allow" button is successfully clicked'
# Quit the browser
browser.quit()
# 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 = client.DropboxClient(sess)
print "linked account:", client.account_info()
f = open('working-draft.txt')
response = client.put_file('/magnum-opus.txt', f)
print "uploaded:", response
有沒有人有什麼錯誤的想法,我怎麼能解決它?
謝謝。