这是 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