// UploadPhoto post image from io.Reader to instagram.
UploadPhoto post image from io.Reader to instagram.
// UploadPhoto post image from io . Reader to instagram .
func (insta *Instagram) UploadPhoto(photo io.Reader, photoCaption string, quality int, filterType int) (Item, error) {
	out := Item{}
	uploadID := time.Now().Unix()
	photoName := fmt.Sprintf("pending_media_%d.jpg", uploadID)
	var b bytes.Buffer
	w := multipart.NewWriter(&b)
	w.WriteField("upload_id", strconv.FormatInt(uploadID, 10))
	w.WriteField("_uuid", insta.uuid)
	w.WriteField("_csrftoken", insta.token)
	var compression = map[string]interface{}{
		"lib_name":    "jt",
		"lib_version": "1.3.0",
		"quality":     quality,
	}
	cBytes, _ := json.Marshal(compression)
	w.WriteField("image_compression", toString(cBytes))
	fw, err := w.CreateFormFile("photo", photoName)
	if err != nil {
		return out, err
	}
	var buf bytes.Buffer
	rdr := io.TeeReader(photo, &buf)
	if _, err = io.Copy(fw, rdr); err != nil {
		return out, err
	}
	if err := w.Close(); err != nil {
		return out, err
	}
	req, err := http.NewRequest("POST", goInstaAPIUrl+"upload/photo/", &b)
	if err != nil {
		return out, err
	}
	req.Header.Set("X-IG-Capabilities", "3Q4=")
	req.Header.Set("X-IG-Connection-Type", "WIFI")
	req.Header.Set("Cookie2", "$Version=1")
	req.Header.Set("Accept-Language", "en-US")
	req.Header.Set("Accept-Encoding", "gzip, deflate")
	req.Header.Set("Content-type", w.FormDataContentType())
	req.Header.Set("Connection", "close")
	req.Header.Set("User-Agent", goInstaUserAgent)
	resp, err := insta.c.Do(req)
	if err != nil {
		return out, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return out, err
	}
	if resp.StatusCode != 200 {
		return out, fmt.Errorf("invalid status code, result: %s", resp.Status)
	}
	var result struct {
		UploadID       string      `json:"upload_id"`
		XsharingNonces interface{} `json:"xsharing_nonces"`
		Status         string      `json:"status"`
	}
	err = json.Unmarshal(body, &result)
	if err != nil {
		return out, err
	}
	if result.Status != "ok" {
		return out, fmt.Errorf("unknown error, status: %s", result.Status)
	}
	width, height, err := getImageDimensionFromReader(&buf)
	if err != nil {
		return out, err
	}
	config := map[string]interface{}{
		"media_folder": "Instagram",
		"source_type":  4,
		"caption":      photoCaption,
		"upload_id":    strconv.FormatInt(uploadID, 10),
		"device":       goInstaDeviceSettings,
		"edits": map[string]interface{}{
			"crop_original_size": []int{width * 1.0, height * 1.0},
			"crop_center":        []float32{0.0, 0.0},
			"crop_zoom":          1.0,
			"filter_type":        filterType,
		},
		"extra": map[string]interface{}{
			"source_width":  width,
			"source_height": height,
		},
	}
	data, err := insta.prepareData(config)
	if err != nil {
		return out, err
	}
	body, err = insta.sendRequest(&reqOptions{
		Endpoint: "media/configure/?",
		Query:    generateSignature(data),
		IsPost:   true,
	})
	if err != nil {
		return out, err
	}
	var uploadResult struct {
		Media    Item   `json:"media"`
		UploadID string `json:"upload_id"`
		Status   string `json:"status"`
	}
	err = json.Unmarshal(body, &uploadResult)
	if err != nil {
		return out, err
	}
	if uploadResult.Status != "ok" {
		return out, fmt.Errorf("invalid status, result: %s", resp.Status)
	}
	return uploadResult.Media, nil
}
func ( insta * Instagram ) UploadPhoto ( photo io . Reader , photoCaption string , quality int , filterType int ) ( Item , error ) { out := Item { } uploadID := time . Now ( ) . Unix ( ) photoName := fmt . Sprintf ( " " , uploadID ) var b bytes . Buffer w := multipart . NewWriter ( & b ) w . WriteField ( " " , strconv . FormatInt ( uploadID , 10 ) ) w . WriteField ( " " , insta . uuid ) w . WriteField ( " " , insta . token ) var compression = map [ string ] interface { } { " " : " " , " " : " " , " " : quality , } cBytes , _ := json . Marshal ( compression ) w . WriteField ( " " , toString ( cBytes ) ) fw , err := w . CreateFormFile ( " " , photoName ) if err != nil { return out , err } var buf bytes . Buffer rdr := io . TeeReader ( photo , & buf ) if _ , err = io . Copy ( fw , rdr ) ; err != nil { return out , err } if err := w . Close ( ) ; err != nil { return out , err } req , err := http . NewRequest ( " " , goInstaAPIUrl + " " , & b ) if err != nil { return out , err } req . Header . Set ( " " , " " ) req . Header . Set ( " " , " " ) req . Header . Set ( " " , " " ) req . Header . Set ( " " , " " ) req . Header . Set ( " " , " " ) req . Header . Set ( " " , w . FormDataContentType ( ) ) req . Header . Set ( " " , " " ) req . Header . Set ( " " , goInstaUserAgent ) resp , err := insta . c . Do ( req ) if err != nil { return out , err } defer resp . Body . Close ( ) body , err := ioutil . ReadAll ( resp . Body ) if err != nil { return out , err } if resp . StatusCode != 200 { return out , fmt . Errorf ( " " , resp . Status ) } var result struct { UploadID string `json:"upload_id"` XsharingNonces interface { } `json:"xsharing_nonces"` Status string `json:"status"` } err = json . Unmarshal ( body , & result ) if err != nil { return out , err } if result . Status != " " { return out , fmt . Errorf ( " " , result . Status ) } width , height , err := getImageDimensionFromReader ( & buf ) if err != nil { return out , err } config := map [ string ] interface { } { " " : " " , " " : 4 , " " : photoCaption , " " : strconv . FormatInt ( uploadID , 10 ) , " " : goInstaDeviceSettings , " " : map [ string ] interface { } { " " : [ ] int { width * 1.0 , height * 1.0 } , " " : [ ] float32 { 0.0 , 0.0 } , " " : 1.0 , " " : filterType , } , " " : map [ string ] interface { } { " " : width , " " : height , } , } data , err := insta . prepareData ( config ) if err != nil { return out , err } body , err = insta . sendRequest ( & reqOptions { Endpoint : " " , Query : generateSignature ( data ) , IsPost : true , } ) if err != nil { return out , err } var uploadResult struct { Media Item `json:"media"` UploadID string `json:"upload_id"` Status string `json:"status"` } err = json . Unmarshal ( body , & uploadResult ) if err != nil { return out , err } if uploadResult . Status != " " { return out , fmt . Errorf ( " " , resp . Status ) } return uploadResult . Media , nil }
UploadPhoto post image from io.Reader to instagram.
UploadPhoto post image from io . Reader to instagram .