Skip to content
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
Expand Down
46 changes: 22 additions & 24 deletions providers/instagram/instagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
var (
authURL = "https://api.instagram.com/oauth/authorize/"
tokenURL = "https://api.instagram.com/oauth/access_token"
endPointProfile = "https://api.instagram.com/v1/users/self/"
endPointProfile = "https://graph.instagram.com/me"
)

// New creates a new Instagram provider, and sets up important connection details.
Expand Down Expand Up @@ -76,18 +76,19 @@ func (p *Provider) BeginAuth(state string) (goth.Session, error) {
// FetchUser will go to Instagram and access basic information about the user.
func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
sess := session.(*Session)

user := goth.User{
AccessToken: sess.AccessToken,
Provider: p.Name(),
}

if user.AccessToken == "" {
// data is not yet retrieved since accessToken is still empty
return user, fmt.Errorf("%s cannot get user information without accessToken", p.providerName)
}

response, err := p.Client().Get(endPointProfile + "?access_token=" + url.QueryEscape(sess.AccessToken))
requestURL := endPointProfile + "?fields=id,username,account_type,media_count&access_token=" + url.QueryEscape(sess.AccessToken)

response, err := p.Client().Get(requestURL)
if err != nil {
return user, err
}
Expand All @@ -101,39 +102,36 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
if err != nil {
return user, err
}

err = json.NewDecoder(bytes.NewReader(bits)).Decode(&user.RawData)
if err != nil {
return user, err
}

err = userFromReader(bytes.NewReader(bits), &user)
return user, err
if err != nil {
return user, err
} else {
return user, nil
}
}

func userFromReader(reader io.Reader, user *goth.User) error {
u := struct {
Data struct {
ID string `json:"id"`
UserName string `json:"username"`
FullName string `json:"full_name"`
ProfilePicture string `json:"profile_picture"`
Bio string `json:"bio"`
Website string `json:"website"`
Counts struct {
Media int `json:"media"`
Follows int `json:"follows"`
FollowedBy int `json:"followed_by"`
} `json:"counts"`
} `json:"data"`
ID string `json:"id"`
UserName string `json:"username"`
AccountType string `json:"account_type"`
MediaCount int64 `json:"media_count"`

// Add other fields as needed
}{}
err := json.NewDecoder(reader).Decode(&u)
if err != nil {
return err
}
user.UserID = u.Data.ID
user.Name = u.Data.FullName
user.NickName = u.Data.UserName
user.AvatarURL = u.Data.ProfilePicture
user.Description = u.Data.Bio
user.UserID = u.ID
user.NickName = u.UserName
// Set other fields as needed
return err
}

Expand All @@ -147,11 +145,11 @@ func newConfig(p *Provider, scopes []string) *oauth2.Config {
TokenURL: tokenURL,
},
Scopes: []string{
"basic",
"instagram_business_basic",
},
}
defaultScopes := map[string]struct{}{
"basic": {},
"instagram_business_basic": {},
}

for _, scope := range scopes {
Expand Down
Loading