动态获取Box应用程序的开发人员令牌

时间:2017-04-28 08:18:23

标签: ruby-on-rails ruby box-api box

@model PEMCOLoan.Web.Models.Ledger

@{ 
    ViewBag.Title = "Ledger";
}

<h2>View @Model.ledger.FullName's Ledger</h2>
<h3>Loan Name: @Model.ledger.LoanName</h3>
<h3>Total Loan Amount: @Model.ledger.TotalLoanAmount</h3>

@if (Model.enumledger.Any())
{
    <table class="table table-responsive">
        <tr>
            <th>Date Paid</th>
            <th>Principal Amount</th>
            <th>Interest</th>
            <th>Total</th>
        </tr>
        @foreach(var item in Model.enumledger)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.PaymentDate)</td>
                <td>@Html.DisplayFor(modelItem => item.TotalLoanAmount)</td>
                <td>@Html.DisplayFor(modelItem => item.Interest)</td>
                <td>@Html.DisplayFor(modelItem => item.Total)</td>
            </tr>
        }
    </table>
}
<h3>Balance: @Model.ledger.Balance</h3>

我创建了一个Ruby脚本,它将文件上传到我的Box并生成其共享链接。问题是开发人员令牌在30或60分钟后到期。所以,我想使用任何gem / api动态创建它并在require 'boxr' client = Boxr::Client.new('my_developer_token') folder = client.folder_from_path("path/to/a/folder") file = client.upload_file('filename.ext', folder) updated_file = client.create_shared_link_for_file(file, access: :open) puts "Shared Link: #{updated_file.shared_link.url}"

中使用它

我该怎么做?

2 个答案:

答案 0 :(得分:0)

如果您使用jwt,则会处理令牌的到期。

https://github.com/cburnette/boxr/blob/master/examples/jwt_auth.rb

答案 1 :(得分:0)

如果我是正确的,并且遵循@kendomen的上述回答,则可以使用JWT来解决此问题。

token = Boxr::get_enterprise_token(private_key: ENV.fetch('JWT_PRIVATE_KEY'), private_key_password: ENV.fetch('JWT_PRIVATE_KEY_PASSWORD'), public_key_id: ENV.fetch('JWT_PUBLIC_KEY_ID'), enterprise_id: ENV.fetch('BOX_ENTERPRISE_ID'), client_id: ENV.fetch('BOX_CLIENT_ID'), client_secret: ENV.fetch('BOX_CLIENT_SECRET'))

然后,您将在生成客户时传递此令牌:

client = Boxr::Client.new(token)
folder = client.folder_from_path("path/to/a/folder")
file = client.upload_file('filename.ext', folder)
updated_file = client.create_shared_link_for_file(file, access: :open)
puts "Shared Link: #{updated_file.shared_link.url}"