Skip to content

Commit

Permalink
Bump Facebook API version and unmarshal user email
Browse files Browse the repository at this point in the history
  • Loading branch information
dghubble committed Jul 9, 2017
1 parent bc94e7c commit 0acc881
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/facebook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func New(config *Config) *http.ServeMux {
ClientSecret: config.FacebookClientSecret,
RedirectURL: "http://localhost:8080/facebook/callback",
Endpoint: facebookOAuth2.Endpoint,
Scopes: []string{"email"},
}
// state param cookies require HTTPS by default; disable for localhost development
stateConfig := gologin.DebugOnlyCookieConfig
Expand Down
4 changes: 2 additions & 2 deletions facebook/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
)

func TestFacebookHandler(t *testing.T) {
jsonData := `{"id": "54638001", "name": "Ivy Crimson"}`
expectedUser := &User{ID: "54638001", Name: "Ivy Crimson"}
jsonData := `{"id": "54638001", "name": "Ivy Crimson", "email": "ivy@harvard.edu"}`
expectedUser := &User{ID: "54638001", Name: "Ivy Crimson", Email: "ivy@harvard.edu"}
proxyClient, server := newFacebookTestServer(jsonData)
defer server.Close()
// oauth2 Client will use the proxy client's base Transport
Expand Down
2 changes: 1 addition & 1 deletion facebook/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// responds with the given json data. The caller must close the server.
func newFacebookTestServer(jsonData string) (*http.Client, *httptest.Server) {
client, mux, server := testutils.TestServer()
mux.HandleFunc("/v2.4/me", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2.9/me", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, jsonData)
})
Expand Down
9 changes: 5 additions & 4 deletions facebook/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"github.com/dghubble/sling"
)

const facebookAPI = "https://graph.facebook.com/v2.4/"
const facebookAPI = "https://graph.facebook.com/v2.9/"

// User is a Facebook user.
//
// Note that user ids are unique to each app.
type User struct {
ID string `json:"id"`
Name string `json:"name"`
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}

// client is a Facebook client for obtaining the current User.
Expand All @@ -35,6 +36,6 @@ func (c *client) Me() (*User, *http.Response, error) {
// Facebook returns JSON as Content-Type text/javascript :(
// Set Accept header to receive proper Content-Type application/json
// so Sling will decode into the struct
resp, err := c.sling.New().Set("Accept", "application/json").Get("me").ReceiveSuccess(user)
resp, err := c.sling.New().Set("Accept", "application/json").Get("me?fields=name,email").ReceiveSuccess(user)
return user, resp, err
}

0 comments on commit 0acc881

Please sign in to comment.