[Golang] Get Instagram Stories of Specific User


Interesting small program to get the JSON format data of stories of a specific Instagram user. The whole picture of getting the JSON comes from the post of Chrome IG Story [1]. Please read the post first. In this program only Go standard library is used, no third-party packages.

To access the Instagram API via local Go program, you need to login Instagram and get the following information from your browser:

  • ds_user_id
  • sessionid
  • csrftoken

Please see this SO answer to get above values on Chrome browser.

Moreover, you need to know the user id of the specific user, please read my previous post to get the id of the user. [3]

After you get the values, you can get the JSON response from the following code:

userstories.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package igstory

// Get all stories of a specific user

import (
	"errors"
	"io/ioutil"
	"net/http"
	"strconv"
	"strings"
)

const UrlUserStories = `https://i.instagram.com/api/v1/feed/user/{{USERID}}/reel_media/`

// id: the id of user whose stories to be retrieved
// userid: your user id
// sessionid: your session id
// csrftoken: your csrftoken
//
// b: the JSON bytes of user stories
func GetUserStories(id int64, userid, sessionid, csrftoken string) (b []byte, err error) {
	url := strings.Replace(UrlUserStories, "{{USERID}}", strconv.FormatInt(id, 10), 1)
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return
	}

	req.AddCookie(&http.Cookie{Name: "ds_user_id", Value: userid})
	req.AddCookie(&http.Cookie{Name: "sessionid", Value: sessionid})
	req.AddCookie(&http.Cookie{Name: "csrftoken", Value: csrftoken})

	req.Header.Set("User-Agent", "Instagram 10.3.2 (iPhone7,2; iPhone OS 9_3_3; en_US; en-US; scale=2.00; 750x1334) AppleWebKit/420+")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		err = errors.New("resp.StatusCode: " + strconv.Itoa(resp.StatusCode))
		return
	}

	return ioutil.ReadAll(resp.Body)
}

How to parse the JSON data to get URL and timestamp of stories, please see my GitHub repo [2].


Tested on: Ubuntu Linux 17.10, Go 1.9.3.


References:

[1]Chrome IG Story — Bribing the Instagram Story API with cookies 🍪🍪🍪
[2]GitHub - siongui/goigstorylink: Get Links (URL) of Instagram Stories in Go
[3][Golang] Get Instagram User ID