Go Instagram Basic Display API
Docs: Instagram Basic Display API
Installation
go get -u github.com/rostyslavio/go-instagram-basic-display-api
Get Access Tokens and Permissions
Step 1: Get Authorization
package main
import (
gogram "github.com/rostyslavio/go-instagram-basic-display-api"
"log"
)
func main() {
config := gogram.Config{
ClientId: "INSTAGRAM_BASIC_DISPLAY_APP_ID",
ClientSecret: "INSTAGRAM_BASIC_DISPLAY_APP_SECRET",
RedirectUri: "INSTAGRAM_BASIC_DISPLAY_REDIRECT_URI",
}
redirect, _ := gogram.NewGogram().Config(config).GetAuthorizeRedirect()
}
Step 2: Exchange the Code For a Token
package main
import (
"encoding/json"
gogram "github.com/rostyslavio/go-instagram-basic-display-api"
"log"
)
type AccessToken struct {
AccessToken string `json:"access_token"`
UserId int `json:"user_id"`
}
func main() {
config := gogram.Config{
ClientId: "INSTAGRAM_BASIC_DISPLAY_APP_ID",
ClientSecret: "INSTAGRAM_BASIC_DISPLAY_APP_SECRET",
RedirectUri: "INSTAGRAM_BASIC_DISPLAY_REDIRECT_URI",
}
response, err := gogram.NewGogram().Config(config).GetAccessToken("Query('code')")
if err != nil {
log.Fatal(err)
}
var accessToken AccessToken
json.Unmarshal([]byte(response), &accessToken)
}
Get User Profiles and User Media
Get a User’s Profile
package main
import (
"encoding/json"
gogram "github.com/rostyslavio/go-instagram-basic-display-api"
"log"
)
type User struct {
Id string `json:"id"`
Username int `json:"username"`
}
func main() {
config := gogram.Config{
ClientId: "INSTAGRAM_BASIC_DISPLAY_APP_ID",
ClientSecret: "INSTAGRAM_BASIC_DISPLAY_APP_SECRET",
RedirectUri: "INSTAGRAM_BASIC_DISPLAY_REDIRECT_URI",
}
response, err := gogram.NewGogram().Config(config).GetUserProfile("ACCESS_TOKEN", []string{"id", "username"})
if err != nil {
log.Fatal(err)
}
var user User
json.Unmarshal([]byte(response), &user)
}
Get a User’s Media
package main
import (
"encoding/json"
gogram "github.com/rostyslavio/go-instagram-basic-display-api"
"log"
)
type MediaData struct {
Id string `json:"id"`
MediaType string `json:"media_type"`
MediaUrl string `json:"media_url"`
ThumbnailUrl *string `json:"thumbnail_url"`
}
type Medias struct {
Data []MediaData `json:"data"`
}
func main() {
config := gogram.Config{
ClientId: "INSTAGRAM_BASIC_DISPLAY_APP_ID",
ClientSecret: "INSTAGRAM_BASIC_DISPLAY_APP_SECRET",
RedirectUri: "INSTAGRAM_BASIC_DISPLAY_REDIRECT_URI",
}
accessToken := "ACCESS_TOKEN"
response, err := gogram.NewGogram().Config(config).GetUsersMedia(accessToken, []string{"id", "media_type", "media_url", "thumbnail_url"})
if err != nil {
log.Fatal(err)
}
var medias Medias
json.Unmarshal([]byte(response), &medias)
}
Get a User’s Media (Paging)
package main
import (
"encoding/json"
gogram "github.com/rostyslavio/go-instagram-basic-display-api"
"log"
)
type MediaData struct {
Id string `json:"id"`
MediaType string `json:"media_type"`
MediaUrl string `json:"media_url"`
ThumbnailUrl string `json:"thumbnail_url"`
}
type Medias struct {
Data []MediaData `json:"data"`
Paging Paging `json:"paging"`
}
type Paging struct {
gogram.Next `json:"next"`
}
func main() {
config := gogram.Config{
ClientId: "INSTAGRAM_BASIC_DISPLAY_APP_ID",
ClientSecret: "INSTAGRAM_BASIC_DISPLAY_APP_SECRET",
RedirectUri: "INSTAGRAM_BASIC_DISPLAY_REDIRECT_URI",
}
accessToken := "ACCESS_TOKEN"
response, err := gogram.NewGogram().Config(config).GetUsersMedia(accessToken, []string{"id", "media_type", "media_url", "thumbnail_url"})
if err != nil {
log.Fatal(err)
}
var medias Medias
json.Unmarshal([]byte(response), &medias)
response, err = medias.Paging.Next.GetUsersMedia()
if err != nil {
log.Fatal(err)
}
json.Unmarshal([]byte(response), &medias)
}
Get Album Contents
package main
import (
"encoding/json"
gogram "github.com/rostyslavio/go-instagram-basic-display-api"
"log"
)
type MediaData struct {
Id string `json:"id"`
MediaType string `json:"media_type"`
MediaUrl string `json:"media_url"`
ThumbnailUrl *string `json:"thumbnail_url"`
}
type Album struct {
Data []MediaData `json:"data"`
}
func main() {
config := gogram.Config{
ClientId: "INSTAGRAM_BASIC_DISPLAY_APP_ID",
ClientSecret: "INSTAGRAM_BASIC_DISPLAY_APP_SECRET",
RedirectUri: "INSTAGRAM_BASIC_DISPLAY_REDIRECT_URI",
}
accessToken := "ACCESS_TOKEN"
response, err := gogram.NewGogram().Config(config).GetAlbumContents(
17909521286425942,
accessToken,
[]string{"id", "media_type", "media_url", "thumbnail_url"},
)
if err != nil {
log.Fatal(err)
}
var album Album
json.Unmarshal([]byte(response), &album)
}
Long-Lived Access Tokens
Get a Long-Lived Token
package main
import (
"encoding/json"
gogram "github.com/rostyslavio/go-instagram-basic-display-api"
"log"
)
type LongLivedToken struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
}
func main() {
config := gogram.Config{
ClientId: "INSTAGRAM_BASIC_DISPLAY_APP_ID",
ClientSecret: "INSTAGRAM_BASIC_DISPLAY_APP_SECRET",
RedirectUri: "INSTAGRAM_BASIC_DISPLAY_REDIRECT_URI",
}
shortLivedAccessToken := "ACCESS_TOKEN"
response, err := gogram.NewGogram().Config(config).GetLongLivedToken(shortLivedAccessToken)
if err != nil {
log.Fatal(err)
}
var longLivedToken LongLivedToken
json.Unmarshal([]byte(response), &longLivedToken)
}
Refresh a Long-Lived Token
package main
import (
"encoding/json"
gogram "github.com/rostyslavio/go-instagram-basic-display-api"
"log"
)
type LongLivedToken struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
}
func main() {
config := gogram.Config{
ClientId: "INSTAGRAM_BASIC_DISPLAY_APP_ID",
ClientSecret: "INSTAGRAM_BASIC_DISPLAY_APP_SECRET",
RedirectUri: "INSTAGRAM_BASIC_DISPLAY_REDIRECT_URI",
}
longLivedAccessToken := "ACCESS_TOKEN"
response, err := gogram.NewGogram().Config(config).RefreshLongLivedToken(longLivedAccessToken)
if err != nil {
log.Fatal(err)
}
var longLivedToken LongLivedToken
json.Unmarshal([]byte(response), &longLivedToken)
}
Deauthorize & Data Deletion Requests
Using a Signed Request
app.Post("/instagram/deauthorize", instagram.Logout())
app.Post("/instagram/deletion", instagram.Logout())
...
type SignedRequest struct {
SignedRequest string `json:"signed_request" xml:"signed_request" form:"signed_request"`
}
func Logout() fiber.Handler {
return func(c *fiber.Ctx) error {
sr := new(SignedRequest)
if err := c.BodyParser(sr); err != nil {
return err
}
config := gogram.Config{
ClientId: src.Getenv("INSTAGRAM_BASIC_DISPLAY_APP_ID"),
ClientSecret: src.Getenv("INSTAGRAM_BASIC_DISPLAY_APP_SECRET"),
RedirectUri: src.Getenv("INSTAGRAM_BASIC_DISPLAY_REDIRECT_URI"),
}
data, _ := gogram.NewGogram().Config(config).ParseSignedRequest(sr.SignedRequest)
return c.SendString("ok")
}
}