fbrell
>_
☰
Examples
Docs
⚙
Log in with Facebook
Status:
waiting
Disconnect
▼
Facebook Login
1 - Basic Login
2 - Checking and Tracking Login Status
3 - Requesting Additional Permissions
4 - Putting it all together
5 - Requesting Business Asset Access Tokens
▼
Graph API
1 - Reading
2 - Reading Edges
3 - Page Admin
▼
Sharing
1 - Social Plugins
2 - FB.ui Dialogs
▼
examples
exampl
▼
viewexamples
exampl
Editor
Output
Log
examples · exampl
▶ Run
/** * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. * * You are hereby granted a non-exclusive, worldwide, royalty-free license to use, * copy, modify, and distribute this software in source code or binary form for use * in connection with the web services and APIs provided by Facebook. * * As with any software that integrates with the Facebook platform, your use of * this software is subject to the Facebook Developer Principles and Policies * [http://developers.facebook.com/policy/]. This copyright notice shall be * included in all copies or substantial portions of the software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ // Package examples provides stock and stored examples. package examples import ( "fmt" "hash/fnv" "log" "net/http" "net/url" "os" "path" "path/filepath" "strings" "github.com/fbsamples/fbrell/errcode" ) // Some categories are hidden from the listing. var hidden = map[string]bool{ "auth": true, "bugs": true, "fb.api": true, "fb.ui": true, "hidden": true, "secret": true, "tests": true, "xfbml": true, "canvas": true, "saved": true, } type Store struct { DB *DB } type Example struct { Name string `json:"-"` Content string `json:"-"` AutoRun bool `json:"autoRun"` Title string `json:"-"` URL string `json:"-"` } type Category struct { Name string Example []*Example Hidden bool } type DB struct { Category map[string]*Category Reverse map[string]*Example } var ( // Stock response for the index page. emptyExample = &Example{Title: "Welcome", URL: "/", AutoRun: true} classExample = &url.URL{Path: "classes/Example"} ) func MustMakeDB(dir string) *DB { db, err := MakeDB(dir) if err != nil { log.Fatal(err) } return db } // Loads a specific examples directory. func MakeDB(dir string) (*DB, error) { db := &DB{ Category: make(map[string]*Category), Reverse: make(map[string]*Example), } db.Reverse[ContentID(emptyExample.Content)] = emptyExample err := filepath.Walk( dir, func(exampleFile string, info os.FileInfo, err error) error { if err != nil { return err } if info.IsDir() { return nil } categoryName := filepath.Base(filepath.Dir(exampleFile)) exampleName := filepath.Base(exampleFile) category := db.Category[categoryName] if category == nil { category = &Category{ Name: categoryName, Hidden: hidden[categoryName], } db.Category[categoryName] = category } contentBytes, err := os.ReadFile(exampleFile) if err != nil { return fmt.Errorf("Failed to read example %s: %s", exampleFile, err) } autoRun := true if categoryName == "saved" { autoRun = false } content := string(contentBytes) cleanName := exampleName[:len(exampleName)-5] // drop .html example := &Example{ Name: cleanName, Content: content, AutoRun: autoRun, Title: categoryName + " · " + cleanName, URL: path.Join("/", categoryName, cleanName), } category.Example = append(category.Example, example) db.Reverse[ContentID(strings.TrimSpace(content))] = example return nil }, ) if err != nil { return nil, err } return db, nil } // Load an Example for a given version and path. func (s *Store) Load(path string) (*Example, error) { parts := strings.Split(path, "/") if len(parts) == 2 && parts[1] == "" { return emptyExample, nil } else if len(parts) != 3 { return nil, errcode.New(http.StatusNotFound, "Invalid URL: %s", path) } category := s.DB.FindCategory(parts[1]) if category == nil { return nil, errcode.New(http.StatusNotFound, "Could not find category: %s", parts[1]) } example := category.FindExample(parts[2]) if example == nil { return nil, errcode.New(http.StatusNotFound, "Could not find example: %s", parts[2]) } return example, nil } // Find a category by it's name. func (d *DB) FindCategory(name string) *Category { for _, category := range d.Category { if category.Name == name { return category } } return nil } // Find an example by it's name. func (c *Category) FindExample(name string) *Example { for _, example := range c.Example { if example.Name == name { return example } } return nil } func ContentID(content string) string { h := fnv.New128a() _, err := fmt.Fprint(h, content) if err != nil { log.Fatalf("Error computing hash: %s", err) } return fmt.Sprintf("%x", h.Sum(nil)) }
// Output will appear here. Press Cmd+Enter to run.
Log
🗑
📋
Loading SDK...
☼
GitHub
Settings
✕
App ID
Server
Version
Locale
en_US
af_ZA
ak_GH
am_ET
ar_AR
as_IN
ay_BO
az_AZ
be_BY
bg_BG
bm_ML
bn_IN
bp_IN
br_FR
bs_BA
bv_DE
ca_ES
cb_IQ
ck_US
co_FR
cs_CZ
cx_PH
cy_GB
da_DK
de_DE
eh_IN
el_GR
em_ZM
en_GB
en_IN
en_OP
en_PI
en_UD
en_XA
eo_EO
es_CL
es_CO
es_ES
es_LA
es_MX
es_VE
et_EE
eu_ES
fa_IR
fb_AA
fb_AC
fb_AR
fb_HA
fb_HX
fb_LL
fb_LS
fb_LT
fb_RL
fb_ZH
fbt_AC
ff_NG
fi_FI
fn_IT
fo_FO
fr_CA
fr_FR
fv_NG
fy_NL
ga_IE
gl_ES
gn_PY
gu_IN
gx_GR
ha_NG
he_IL
hi_FB
hi_IN
hr_HR
ht_HT
hu_HU
hy_AM
id_ID
ig_NG
ik_US
is_IS
it_IT
iu_CA
ja_JP
ja_KS
jv_ID
ka_GE
kk_KZ
km_KH
kn_IN
ko_KR
ks_IN
ku_TR
ky_KG
la_VA
lg_UG
li_NL
ln_CD
lo_LA
lr_IT
lt_LT
lv_LV
mg_MG
mi_NZ
mk_MK
ml_IN
mn_MN
mos_BF
mr_IN
ms_MY
mt_MT
my_MM
nb_NO
nd_ZW
ne_NP
nh_MX
nl_BE
nl_NL
nn_NO
nr_ZA
ns_ZA
ny_MW
om_ET
or_IN
pa_IN
pcm_NG
pl_PL
ps_AF
pt_BR
pt_PT
qb_DE
qc_GT
qe_US
qk_DZ
qr_GR
qs_DE
qt_US
qu_PE
qv_IT
qz_MM
rm_CH
rn_BI
ro_RO
ru_RU
rw_RW
sa_IN
sc_IT
se_NO
si_LK
sk_SK
sl_SI
sn_ZW
so_SO
sq_AL
sr_RS
ss_SZ
st_ZA
su_ID
sv_SE
sw_KE
sy_SY
sz_PL
ta_IN
te_IN
tg_TJ
th_TH
ti_ET
tk_TM
tl_PH
tl_ST
tn_BW
tq_AR
tpi_PG
tr_TR
ts_ZA
tt_RU
tz_MA
uk_UA
ur_PK
uz_UZ
ve_ZA
vi_VN
wo_SN
xh_ZA
yi_DE
yo_NG
zh_CN
zh_HK
zh_TW
zu_ZA
zz_TR
Log Level
debug
info
warn
error
View Mode
Website
Canvas
Page Tab
Auto Init SDK
Auto Status Ping
Frictionless Requests
Custom Login Button
FedCM
FedCM Auto Prompt
FedCM Context
(none)
signin
signup
use