我正嘗試將照片上傳到Twitter。我創建了一個多部分作家並使用該名爲媒體創建了一個文件字段,但是當我向Twitter發送我的請求時,它一直在響應缺少的媒體字段。 我錯過了什麼嗎? 這裏是我的代碼多部分文件字段不可讀
f, err := os.Open("/Users/nikos/Desktop/test.png")
errored:
if nil != err {
fmt.Println(err)
return
}
var img = new(bytes.Buffer)
enc := base64.NewEncoder(base64.StdEncoding, img)
_, err = io.Copy(enc, f)
if nil != err {
goto errored
}
body := new(bytes.Buffer)//Multipart body
writer := multipart.NewWriter(body)
cl, err := twitter.OauthClient.MakeHttpClient(&oauth.AccessToken{
Token: "xxx",
Secret: "yyy",
})
err = writer.WriteField("media_data", img.String())//base64 version of the image (i tried both binary and base64 versions neither will work)
if nil != err {
goto errored
}
part, err := writer.CreateFormFile("media", "test.png")//actual binary file multiparted and it is named media.
if nil != err {
goto errored
}
_, err = io.Copy(part, f)
if nil != err {
goto errored
}
req, err := http.NewRequest("POST",
"https://upload.twitter.com/1.1/media/upload.json",
body)
if nil != err {
goto errored
}
res, err := cl.Do(req)
if nil != err {
goto errored
}
//and twitter responds that there is no field attached named media
_, err = io.Copy(os.Stdout, res.Body)
fmt.Println(res)
if nil != err {
goto errored
}
你看到了什麼錯誤? –
@CeriseLimón我一次發送一個,只是在這裏我寫在一起,但生病嘗試關閉作家,如果我設置內容類型我得到一個錯誤的請求錯誤,讓我試試關閉作家 – nikoss
@YandryPozo'{「errors」: [{「code」:38,「message」:「media parameter is missing。」}]}&{400 Bad Request 400'這是我得到的錯誤 – nikoss