Golang代码将文件上传到个人邮箱帐户

时间:2015-03-12 13:11:00

标签: go

当我在收到访问令牌后在golang程序中提供这段代码时:

f, err := ioutil.ReadFile("C:\\Users\\vembu\\Desktop\\hi.txt")
ioutil.WriteFile("hi.txt", f, 0x777)
r, _ := http.NewRequest("POST", urlStr, bytes.NewBuffer(f)) 
r.Header.Add("Authorization", "Bearer "+accessobj.Access_token)
r.Header.Add("attributes", "{\"name\":\"hi.txt\",\"parent\":{\"id\":\"3098791209\"}}")r.Header.Add("file", "hi.txt")

我收到此错误:

I face &{405 Method Not Allowed 405 HTTP/1.1 1 1 map[Allow:[GET, OPTIONS, HEAD] Content-Type:[text/html;charset=UTF-8] Content-Length:[0] Date:[Thu, 12 Mar 2015 13:07:32 GMT] Age:[0] Connection:[keep-alive] Server:[ATS]] 0xc08200b8c0 0 [] false map[] 0xc08201f2b0 0xc082060980}

任何人都可以帮我解决添加属性和文件名的方法上传到邮箱帐户吗?

1 个答案:

答案 0 :(得分:0)

urlStr应为https://upload.box.com/api/2.0/files/content

https://developers.box.com/docs/#files-upload-a-file

正如他们在文档中所示,您可能希望首先使用curl尝试查询:

curl https://upload.box.com/api/2.0/files/content \
  -H "Authorization: Bearer ACCESS_TOKEN" -X POST \
  -F attributes='{"name":"tigers.jpeg", "parent":{"id":"11446498"}}' \
  -F file=@myfile.jpg