forked from justwatch/facebook-marketing-api-golang-sdk
/
page.go
141 lines (117 loc) · 4.2 KB
/
page.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package v16
import (
"context"
"fmt"
"github.com/JoshuaDietz/facebook-marketing-api-golang-sdk-v19-test/fb"
)
// PageService contains all methods for working on pages.
type PageService struct {
c *fb.Client
}
// SetPageAccessToken tries to retrieve the access token for a facebook page and includes it in the passed context so the fb.Client can use it for making requests.
func (ps *PageService) SetPageAccessToken(ctx context.Context, pageID string) (context.Context, error) {
tc := struct {
AccessToken string `json:"access_token"`
}{}
err := ps.c.GetJSON(ctx, fb.NewRoute(Version, "/%s", pageID).Fields("access_token").String(), &tc)
if err != nil {
return ctx, err
} else if tc.AccessToken == "" {
return ctx, fmt.Errorf("could not get page access token for '%s'", pageID)
}
return fb.SetPageAccessToken(ctx, tc.AccessToken), nil
}
// GetPageBackedInstagramAccounts returns the instagram actor associated with a facebook page.
func (ps *PageService) GetPageBackedInstagramAccounts(ctx context.Context, pageID string) (*InstagramActor, error) {
ctx, err := ps.SetPageAccessToken(ctx, pageID)
if err != nil {
return nil, err
}
fpiga := struct {
PageBackedInstagramAccounts struct {
Data []InstagramActor `json:"data"`
} `json:"page_backed_instagram_accounts"`
}{}
err = ps.c.GetJSON(ctx, fb.NewRoute(Version, "/%s", pageID).Fields("page_backed_instagram_accounts{id,username}").String(), &fpiga)
if err != nil {
return nil, err
}
if len(fpiga.PageBackedInstagramAccounts.Data) != 1 {
return nil, fmt.Errorf("could not get consistent page_backed_instagram_accounts data for facebook page with external id %s", pageID)
}
res := fpiga.PageBackedInstagramAccounts.Data[0]
if res.ID == "" {
return nil, fmt.Errorf("could not get page_backed_instagram_accounts ID for facebook page with external id %s", pageID)
}
if res.Username == "" {
return nil, fmt.Errorf("could not get page_backed_instagram_accounts username for facebook page with external id %s", pageID)
}
return &res, nil
}
// GetClientPages returns all client pages.
func (ps *PageService) GetClientPages(ctx context.Context, businessID string) ([]Page, error) {
res := []Page{}
route := fb.NewRoute(Version, "/%s/client_pages", businessID).Limit(1000).Fields(pageFields...)
err := ps.c.GetList(ctx, route.String(), &res)
if err != nil {
return nil, err
}
return res, nil
}
// GetOwnedPages returns all owned pages.
func (ps *PageService) GetOwnedPages(ctx context.Context, businessID string) ([]Page, error) {
res := []Page{}
route := fb.NewRoute(Version, "/%s/owned_pages", businessID).Limit(1000).Fields(pageFields...)
err := ps.c.GetList(ctx, route.String(), &res)
if err != nil {
return nil, err
}
return res, nil
}
// GetInstagramActors returns all instagram accounts.
func (ps *PageService) GetInstagramActors(ctx context.Context, businessID string) ([]InstagramActor, error) {
res := []InstagramActor{}
route := fb.NewRoute(Version, "/%s/instagram_accounts", businessID).Limit(1000).Fields(instagramActorFields...)
err := ps.c.GetList(ctx, route.String(), &res)
if err != nil {
return nil, err
}
return res, nil
}
// Get returns a single page.
func (ps *PageService) Get(ctx context.Context, id string) (*Page, error) {
res := &Page{}
route := fb.NewRoute(Version, "/%s", id).Fields(pageFields...)
err := ps.c.GetJSON(ctx, route.String(), res)
if err != nil {
if fb.IsNotFound(err) {
return nil, nil
}
return nil, err
}
return res, nil
}
// GetInstagramActor returns a single instagram actor.
func (ps *PageService) GetInstagramActor(ctx context.Context, id string) (*InstagramActor, error) {
res := &InstagramActor{}
route := fb.NewRoute(Version, "/%s", id).Fields(instagramActorFields...)
err := ps.c.GetJSON(ctx, route.String(), res)
if err != nil {
return nil, err
}
return res, nil
}
var (
pageFields = []string{"id", "global_brand_page_name"}
instagramActorFields = []string{"id", "username"}
)
// Page represents a facebook page.
type Page struct {
ID string `json:"id"`
GlobalBrandPageName string `json:"global_brand_page_name"`
}
// InstagramActor represents an instagram actor.
type InstagramActor struct {
ID string `json:"id"`
Username string `json:"username"`
}