【问题标题】:Using the Citrix ShareFile API, how can I get the most recently uploaded files?使用 Citrix ShareFile API,如何获取最近上传的文件?
【发布时间】:2019-05-20 22:58:26
【问题描述】:

我想每小时运行一次或两次作业,以检索已上传到我们客户根文件夹的所有文件。这将包括嵌套在 2 或 3 个文件夹深处的文件。

目前,我们的客户正在让某人每小时通过仪表板界面手动运行一次报告,以生成列出他们的 CSV 文件。这很麻烦。

在谷歌搜索和嗅探ShareFile API docs 之后,我最好的想法是使用Items resource。具体来说,我打算利用高级搜索查询界面中的CreateStartDate 字段:

我有我的 API 密钥,我将开始做一些测试(在 Rails 应用程序中使用 Ruby)。正如我所做的那样,我想我会在这里询问是否有更直接或更有效的方法。

【问题讨论】:

    标签: rest citrix sharefile


    【解决方案1】:

    这是 Ruby 代码(在 Rails 应用程序中),它返回在过去一小时内发布到帐户主文件夹的文件。

    注意所需的秘密。我很失望必须包含用户名和密码。

    class ShareFileService
      include HTTParty
      base_uri 'https://YOUR-SUBDOMAIN.sharefile.com'
      attr_reader :auth_token, :response
    
      def initialize; end
    
      def authenticate
        return @auth_token if @auth_token.present?
    
        api_endpoint = '/oauth/token'
        params = {
          'grant_type'    => 'password',
          'client_id'     => Rails.application.secrets.sharefile_client_id,
          'client_secret' => Rails.application.secrets.sharefile_client_secret,
          'username'      => Rails.application.secrets.sharefile_user,
          'password'      => Rails.application.secrets.sharefile_pass
        }
        body = { body: params }
        @response = self.class.post(api_endpoint, body)
    
        # This is the auth token
        @auth_token = JSON.parse(@response.body)
        @auth_token
      end
    
      def home_folder
        # https://api.sharefile.com/rest/docs/resource.aspx?name=Items
        # "Returns: home folder for current user"
        token = authenticate
        api_uri = format('https://%s.sf-api.com/sf/v3/Items', token['subdomain'])
        auth_header = format('Bearer %s', token['access_token'])
    
        headers = { 'authorization' => auth_header }
        params = { '$expand' => 'Children' }
        options = {
          headers: headers,
          query: params
        }
        @response = self.class.get(api_uri, options)
    
        JSON.parse(@response.body)
      end
    
      def recent_files
        # http://api.sharefile.com/rest/docs/resource.aspx?name=Items#Advanced_Simple_Search
        # "Returns: home folder for current user"
        token = authenticate
        api_uri = format('https://%s.sf-api.com/sf/v3/Items/AdvancedSimpleSearch', token['subdomain'])
        auth_header = format('Bearer %s', token['access_token'])
        an_hour_ago = (Time.zone.now - 1.hour).iso8601
    
        # Based on https://stackoverflow.com/a/44565283/9381758
        params = {
          'Query': {
            'ItemType': '',
            'ParentID': '',
            'CreatorID': '',
            'SearchQuery': '',
            'CreateStartDate': an_hour_ago,
            'CreateEndDate': '',
            'ItemNameOnly': false
          },
          'Paging': {
            'Count': 100,
            'Skip': 0
          },
          'Sort': {
            'SortBy': 'CreateStartDate',
            'Ascending': false
          },
          'TimeoutInSeconds': 10
        }
        options = {
          headers: { 'authorization' => auth_header },
          body: params
        }
        @response = self.class.post(api_uri, options)
    
        JSON.parse(@response.body)
      end
    end
    

    示例用法:

    $ rails console
    > sharefile = ShareFileService.new
    > body = sharefile.recent_files
    => {"PartialResults"=>false, "Results"=> ...}
    > body.keys
    => ["PartialResults", "Results", "TimedOut", "odata.metadata", "odata.type", "url"]  
    > body['PartialResults']
    => false
    > body['Results'].length 
    => 100
    

    【讨论】:

      猜你喜欢
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多