以编程方式将文件上传到Box.com的最佳方法 [英] Best way to upload files to Box.com programmatically

查看:183
本文介绍了以编程方式将文件上传到Box.com的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了整个Box.com开发人员的api指南,并在网络上花费了数小时研究这个特定问题,但是我似乎找不到确切的答案,如果我愿意,我也不想开始创建解决方案我走错了路。我们有一个生产环境,一旦完成文件处理,我们的生产软件系统就会将它们压缩并保存到本地服务器目录中,以备存档之用。此本地路径无法更改。我的问题是如何以编程方式将这些文件上传到我们的Box.com帐户,以便我们可以将它们归档到云上?我所读到的所有内容都涉及使用OAuth2来访问我们了解的帐户,但这也需要用户登录。由于这是内部程序,不会暴露给外部用户,因此我希望能够自动执行此程序,否则对我们来说是不可行的。在创建程序以每次保存新文件时触发我都没有问题,我所需要做的就是简化Box.com的访问。

I've read the whole Box.com developers api guide and spent hours on the web researching this particular question but I can't seem to find a definitive answer and I don't want to start creating a solution if I'm going down the wrong path. We have a production environment where as once we are finished working with files our production software system zips them up and saves them into a local server directory for archival purposes. This local path cannot be changed. My question is how can I programmatically upload these files to our Box.com account so we can archive these on the cloud? Everything I've read regarding this involves using OAuth2 to gain access to our account which I understand but it also requires the user to login. Since this is an internal process that is NOT exposed to outside users I want to be able to automate this otherwise it would not be feasable for us. I have no issues creating the programs to trigger everytime a new files gets saved all I need is to streamline the Box.com access.

推荐答案

我刚刚经历了完全相同的一组问题,发现当前您无法绕过OAuth流程。但是,它们的刷新令牌现在有效期为60天,这应该会使任何自定义设置更加坚固。不过,我仍然认为,必须使用OAuth进行企业设置是一种非常脆弱的实现-出于您所说的确切原因:某些中间件应用程序必须依靠OAuth身份验证过程是不可行的。

I just went through the exact same set of questions and found out that currently you CANNOT bypass the OAuth process. However, their refresh token is now valid for 60 days which should make any custom setup a bit more sturdy. I still think, though, that having to use OAuth for an Enterprise setup is a very brittle implementation -- for the exact reason you stated: it's not feasible for some middleware application to have to rely on an OAuth authentication process.

这就是我的想法。以下步骤与各种Box API文档和视频中概述的步骤相同:

Here's what I came up with. The following are the same steps as outlined in various box API docs and videos:


  1. 使用此URL https://www.box.com/api/oauth2 / authorize?response_type = code& client_id = [YOUR_CLIENT_ID]& state = [box-produced_state_security_token]
    (转到 https://developers.box.com/oauth/ 来找到原始的)

  2. 将该URL粘贴到浏览器和GO中
  3. >
  4. 认证并授予访问权限

  5. 获取所得到的URL: http://0.0.0.0/?state= [box-produced_state_security_token]& code = [SOME_CODE]
    并注意 code =

  6. 打开POSTMAN或Fiddler(或其他一些HTTP嗅探器)和e请执行以下操作:



    • URL: https://www.box.com/api/oauth2/token

    • 创建URL编码的帖子数据:



      • grant_type = authorization_code

      • client_id = [您的客户ID]

      • client_secret = [您的客户机密]

      • code =<输入步骤4中的代码>

  1. use this URL https://www.box.com/api/oauth2/authorize?response_type=code&client_id=[YOUR_CLIENT_ID]&state=[box-generated_state_security_token] (go to https://developers.box.com/oauth/ to find the original one)
  2. paste that URL into the browser and GO
  3. authenticate and grant access
  4. grab the resulting URL: http://0.0.0.0/?state=[box-generated_state_security_token]&code=[SOME_CODE] and note the "code=" value.
  5. open POSTMAN or Fiddler (or some other HTTP sniffer) and enter the following:
    • URL: https://www.box.com/api/oauth2/token
    • create URL encoded post data:
      • grant_type=authorization_code
      • client_id=[YOUR CLIENT ID]
      • client_secret=[YOUR CLIENT SECRET]
      • code= < enter the code from step 4 >

{
"access_token": "[YOUR SHINY NEW ACCESS TOKEN]",
"expires_in": 4255,
"restricted_to": [],
"refresh_token": "[YOUR HELPFUL REFRESH TOKEN]",
"token_type": "bearer"
}


在我的应用程序中,我将身份验证令牌和刷新令牌都保存为一种格式,如果出现问题,可以轻松地替换它们路。然后,每次调用API时都会检查身份验证。如果我收到授权异常,则会以编程方式刷新令牌,您可以执行!使用BoxApi.V2 .NET SDK时,会这样发生:

In my application I save both auth token and refresh token in a format where I can easily go and replace them if something goes awry down the road. Then, I check my authentication each time I call into the API. If I get an authorization exception back I refresh my token programmatically, which you can do! Using the BoxApi.V2 .NET SDK this happens like so:

var authenticator = new TokenProvider(_clientId, _clientSecret);
// calling the 'RefreshAccessToken' method in the SDK
var newAuthToken = authenticator.RefreshAccessToken([YOUR EXISTING REFRESH TOKEN]);
// write the new token back to my data store.
Save(newAuthToken);

希望有帮助!

这篇关于以编程方式将文件上传到Box.com的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆