我正在研究ruby应用程序,并且正在尝试将文件上传到box.net。我让它使用curl调用
curl https://www.box.com/api/2.0/files/data -H "Authorization: BoxAuth api_key=<API_KEY>&auth_token=<AUTH_TOKEN>" -F folder_id=0 -F filename=@test.txt --trace ~/Desktop/log.txt
我试图将其转换为ruby,并尝试了以下
request = RestClient::Request.new(:method => :post,:url => "https://www.box.com/api/2.0/files/data",:authorization => "BoxAuth api_key=<API_KEY>&auth_token=<AUTH_TOKEN>",:filename => "test.txt", :payload => { :multipart => true, :file => File.new("test.txt"))
request.execute
但我一直收到“401:未经授权”的回复。我也尝试过使用box-api gem,但这似乎只适用于API的1.0版本,我正在尝试与2.0接口。
答案 0 :(得分:2)
尝试在通话中使用:headers => {:authorization => "BoxAuth api_key=<API_KEY>&auth_token=<AUTH_TOKEN>"}
。这应该修复丢失的授权标题。
完整的请求将是:
request = RestClient::Request.new(:method => :post,:url => "https://www.box.com/api/2.0/files/data",:headers => {:authorization => "BoxAuth api_key=<API_KEY>&auth_token=<AUTH_TOKEN>"},:filename => "test.txt", :payload => { :multipart => true, :file => File.new("test.txt")})