[Golang] Get Instagram Highlight Stories of Specific User


[Update]: the method in this post can only get partial story highlights now. Please see my another new post for how to get the rest of story highlights.

Interesting small program to get the JSON format data of highlight stories of a specific Instagram user.

When I used Chrome extension InstaG Downloader version 2.1.0, I found it will show highlight stories of users when you visit the page of the user. I am curious how it works so I installed another extension Network Sniffer to see the API URL of highlight stories, and found that API URL is

https://i.instagram.com/api/v1/highlights/{{USERID}}/highlights_tray/

The procedure of reading JSON data is the same as the original story API [1]. Please read the post if you are not familiar with the API.

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:

config.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
package igstory

// Cookies information for accessing stories

// See the following Stack Overflow answer to get the values:
// https://stackoverflow.com/a/44773079
var config = map[string]string{
	"ds_user_id": "",
	"sessionid":  "",
	"csrftoken":  "",
	"User-Agent": "Instagram 10.3.2 (iPhone7,2; iPhone OS 9_3_3; en_US; en-US; scale=2.00; 750x1334) AppleWebKit/420+",
}

// Login Instagram first and then you can obtain *ds_user_id*
// in Chrome Developer Tools.
//
// See
// https://stackoverflow.com/a/44773079
// Or
// https://github.com/siongui/goigstorylink#obtain-cookies
func SetUserId(s string) {
	config["ds_user_id"] = s
}

// Login Instagram first and then you can obtain *sessionid*
// in Chrome Developer Tools.
//
// See
// https://stackoverflow.com/a/44773079
// Or
// https://github.com/siongui/goigstorylink#obtain-cookies
func SetSessionId(s string) {
	config["sessionid"] = s
}

// Login Instagram first and then you can obtain *csrftoken*
// in Chrome Developer Tools.
//
// See
// https://stackoverflow.com/a/44773079
// Or
// https://github.com/siongui/goigstorylink#obtain-cookies
func SetCsrfToken(s string) {
	config["csrftoken"] = s
}
userhighlightstories.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
package igstory

// Get highlight stories of a specific user

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

const UrlUserHighlightStories = `https://i.instagram.com/api/v1/highlights/{{USERID}}/highlights_tray/`

func GetUserHighlightStories(id int64) (b []byte, err error) {
	url := strings.Replace(
		UrlUserHighlightStories,
		"{{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: config["ds_user_id"]})
	req.AddCookie(&http.Cookie{Name: "sessionid", Value: config["sessionid"]})
	req.AddCookie(&http.Cookie{Name: "csrftoken", Value: config["csrftoken"]})

	req.Header.Set("User-Agent", config["User-Agent"])

	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)
}

Example:

userhighlightstories_test.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package igstory

import (
	"os"
	"testing"
)

func ExampleGetUserHighlightStories(t *testing.T) {
	SetUserId(os.Getenv("IG_DS_USER_ID"))
	SetSessionId(os.Getenv("IG_SESSIONID"))
	SetCsrfToken(os.Getenv("IG_CSRFTOKEN"))
	// id of user *instagram* is 25025320
	b, err := GetUserHighlightStories(25025320)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log(string(b))
}

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.4.


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