[#5588] replaced deprecated Instagram Basic Display provider with a new Instagram Login

Co-authored-by: Pedro Costa <550684+pnmcosta@users.noreply.github.com>
This commit is contained in:
Gani Georgiev
2024-11-07 13:51:21 +02:00
parent 57f615a58c
commit 0daa584461
31 changed files with 62 additions and 47 deletions
+21 -11
View File
@@ -6,7 +6,6 @@ import (
"github.com/pocketbase/pocketbase/tools/types"
"golang.org/x/oauth2"
"golang.org/x/oauth2/instagram"
)
func init() {
@@ -16,9 +15,9 @@ func init() {
var _ Provider = (*Instagram)(nil)
// NameInstagram is the unique name of the Instagram provider.
const NameInstagram string = "instagram"
const NameInstagram string = "instagram2" // "2" suffix to avoid conflicts with the old deprecated version
// Instagram allows authentication via Instagram OAuth2.
// Instagram allows authentication via Instagram Login OAuth2.
type Instagram struct {
BaseProvider
}
@@ -29,16 +28,16 @@ func NewInstagramProvider() *Instagram {
ctx: context.Background(),
displayName: "Instagram",
pkce: true,
scopes: []string{"user_profile"},
authURL: instagram.Endpoint.AuthURL,
tokenURL: instagram.Endpoint.TokenURL,
userInfoURL: "https://graph.instagram.com/me?fields=id,username,account_type",
scopes: []string{"instagram_business_basic"},
authURL: "https://www.instagram.com/oauth/authorize",
tokenURL: "https://api.instagram.com/oauth/access_token",
userInfoURL: "https://graph.instagram.com/me?fields=id,username,account_type,user_id,name,profile_picture_url,followers_count,follows_count,media_count",
}}
}
// FetchAuthUser returns an AuthUser instance based on the Instagram's user api.
// FetchAuthUser returns an AuthUser instance based on the Instagram Login user api response.
//
// API reference: https://developers.facebook.com/docs/instagram-basic-display-api/reference/user#fields
// API reference: https://developers.facebook.com/docs/instagram-platform/instagram-api-with-instagram-login/get-started#fields
func (p *Instagram) FetchAuthUser(token *oauth2.Token) (*AuthUser, error) {
data, err := p.FetchRawUserInfo(token)
if err != nil {
@@ -50,9 +49,18 @@ func (p *Instagram) FetchAuthUser(token *oauth2.Token) (*AuthUser, error) {
return nil, err
}
// include list of granted permissions to RawUser's payload
if _, ok := rawUser["permissions"]; !ok {
if permissions := token.Extra("permissions"); permissions != nil {
rawUser["permissions"] = permissions
}
}
extracted := struct {
Id string `json:"id"`
Username string `json:"username"`
Id string `json:"user_id"`
Name string `json:"name"`
Username string `json:"username"`
AvatarURL string `json:"profile_picture_url"`
}{}
if err := json.Unmarshal(data, &extracted); err != nil {
return nil, err
@@ -61,6 +69,8 @@ func (p *Instagram) FetchAuthUser(token *oauth2.Token) (*AuthUser, error) {
user := &AuthUser{
Id: extracted.Id,
Username: extracted.Username,
Name: extracted.Name,
AvatarURL: extracted.AvatarURL,
RawUser: rawUser,
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,