1

ruby-box gem を使用して box.com に接続しています。このコードを使用してアクセス トークンを取得するように指定されています。

session = RubyBox::Session.new({
  client_id: $boxClientID,
  client_secret: $boxClientSecret
})

authorize_url = session.authorize_url('https://redirect-url-in-app-settings')

@token = session.get_access_token('code-returned-to-redirect_url')

問題は、リダイレクト先の URL がなく、GUI も使用していないため、そこからコードを取得できないことです。私は何をしますか?

4

3 に答える 3

3

ベンです。私は ruby​​-box Gem の開発者の 1 人です。できることは次のとおりです。

  • app.box.com で、このコールバックを使用するようにアプリケーションをセットアップします

https://localhost/oauth2callback

  • IRB で、同じコールバックを使用して承認 URL を生成します。
    session = RubyBox::Session.new({
      client_id: $boxClientID,
      client_secret: $boxClientSecret
    }))

    authorize_url = session.authorize_url('https://localhost/oauth2callback')

  • この URL を Web ブラウザに貼り付けます。
  • Box で認証すると、次のような URL を持つコールバック ページが表示されます。

https://localhost/oauth2callback?state=&code=OQpfImZH35Ab9weUy61kthIvqZ2Dyz6

  • コードの値をコピーします。
  • irb に戻り、次のコマンドを実行します。

token = session.get_access_token('OQpfImZH35Ab9weUy61kthIvqZ2Dyz6')
access_token = token.token
refresh_token = token.refresh_token

  • アクセス トークンと更新トークンの両方をデータベース (またはナプキン、または適切と思われる場所) に保存します。
  • これからは、次のようにセッションをインスタンス化できます。

    session = RubyBox::Session.new({
      access_token: $storedAccessTokenValue,
      refresh_token: $storedRefreshTokenValue,
      client_id: $boxClientID,
      client_secret: $boxClientSecret
    })

    client = RubyBox::Client.new(session)

ユーザーごとに access_token と refresh_token を 1 回だけ取得する必要があります。

于 2013-08-06T01:46:27.023 に答える
1

1 つの問題は、アクセス トークンとリフレッシュ トークンの有効期限が切れることです。これらのトークンを無期限に保存して、RubyBox セッションを永遠にインスタンス化し続けることはできないと思います。(スクリプトがユーザー名とパスワードを保存するという観点から) 信じられないほど安全でないものが必要な場合でも、機能する場合は、Mechanize を使用して GUI のインタラクティブな部分をトラバースできます。同じ問題を解決するために今日書いたスクリプトを次に示します。oauth2 のリダイレクト URI が必要であり、完全に無効な URL を与えて Mechanize を存続させる方法がわかりませんでした。URI は https でなければならないので、ランダムにhttps://www.chase.comを選択しました。そのページの内容は問題ではありません。重要なのは、リダイレクトの URL に認証コードが追加されていることです。それが必要なことです。

ただし、これは、自分のパスワードを開示せずにアプリに権限を付与するという oauth2 の要点全体に完全に反しています。そのため、慎重に使用してください...

また、box.com の承認フォームのフォーム構造と変数名に依存しているため、いつでも変更される可能性があるため、これは少し壊れやすいと思います。

require 'ruby-box'
require 'mechanize'
require 'cgi'

# get as new box session
box_session = RubyBox::Session.new({
    client_id: 'your-client-id',
    client_secret: 'your-client-secret'
})

# Get the authorization URL from Box by specifying redirect URL
# as the arbitrary but working Chase bank home page
authorize_url = box_session.authorize_url('https://www.chase.com')

agent = Mechanize::new

# process the first login screen
login_page = agent.get(authorize_url)

# get the login form where you enter the username and password
login_form = login_page.form_with(name: 'login_form1')
login_form.login = 'your-box-username'
login_form.password = 'your-box-password'

# submit the form and get the allow/deny page back
allow_page = agent.submit(login_form)

# find the form that allows consent
consent_form = allow_page.form_with(name: 'consent_form')

# now find the button that submits the allow page with consent
accept_button = consent_form.button_with(name: 'consent_accept')

# Submit the form to cause the redirection with authentication code
redirpage = agent.submit(consent_form, accept_button)

# Use the CGI module to get a hash of the variables (stuff after ?)
# and then the authentication code is embedded in [" and "] so 
# strip those
code_query = CGI::parse(redirpage.uri.query)["code"].to_s
box_authentication_code = code_query[2,code_query.length-4]

# get the box access token using the authentication code
@token = box_session.get_access_token(box_authentication_code)

# print the tokens to show we have them
p @token.token 
p @token.refresh_token

# Create a new Box client based on the authenticated session
client = RubyBox::Client.new(box_session)

# prove it works by getting list of folders in root directory
folders = client.root_folder.folders
p folders
于 2013-08-17T00:03:44.463 に答える
0

私は Ruby ではなく Python を使用しているため、これをコーディングする方法を説明することはできませんが、この問題をどのように回避することにしたかを共有できます。

ボックスでイベント キューを監視し、ユーザーがアップロードした新しいファイルを処理する Python スクリプトを作成しています。

Python スクリプトは自動化されているため、heroku でホストされているボックス トークン ジェネレーター ( http://box-token-generator.herokuapp.com/ ) にアクセスし、それを使用してアプリの認証を開始します。リクエストを行うたびに、401 Unauthorized エラーが発生するかどうかを確認し、エラーが発生した場合はトークンを更新します。

于 2013-08-05T18:39:27.747 に答える