Skip to content

Commit edd5abc

Browse files
committed
Fix LinkedIn API error Mutiple authentication
The error returned was 401 "Mutiple authentication schemes detected" and it was caused because in the API request we were adding the parameter &oauth2_access_token= as suggested by Caserta (2015). http://pierrecaserta.com/go-oauth-facebook-github-twitter-google-plus/ Once that parameter was removed, the API returned the user data. See: ashwinks/PHP-LinkedIn-SDK#25
1 parent ea23840 commit edd5abc

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

petfind/postgres/pets.go

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func (db *store) GetPet(petID int64) (*petfind.Pet, error) {
7070
&p.PlaceID,
7171
&u.ID,
7272
&u.GithubID,
73+
&u.LinkedinID,
7374
&u.Name,
7475
&u.Login,
7576
&u.Email,
@@ -131,6 +132,7 @@ func (db *store) GetFeaturedPets() ([]*petfind.Pet, error) {
131132
&p.PlaceID,
132133
&u.ID,
133134
&u.GithubID,
135+
&u.LinkedinID,
134136
&u.Name,
135137
&u.Login,
136138
&u.Email,
@@ -168,6 +170,7 @@ func (db *store) GetAllPets() ([]petfind.Pet, error) {
168170
p.place_id,
169171
u.id,
170172
u.github_id,
173+
u.linkedin_id,
171174
u.name,
172175
u.login,
173176
u.email,
@@ -213,6 +216,7 @@ func (db *store) GetAllPets() ([]petfind.Pet, error) {
213216
&p.PlaceID,
214217
&u.ID,
215218
&u.GithubID,
219+
&u.LinkedinID,
216220
&u.Name,
217221
&u.Login,
218222
&u.Email,
@@ -429,6 +433,7 @@ func (db *store) SearchPets(s petfind.Search) ([]*petfind.Pet, error) {
429433
&p.PlaceID,
430434
&u.ID,
431435
&u.GithubID,
436+
&u.LinkedinID,
432437
&u.Name,
433438
&u.Login,
434439
&u.Email,

petfind/postgres/postgres.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ func (db *store) MakeSchema() error {
3131
const users = `CREATE TABLE IF NOT EXISTS users (
3232
id bigserial PRIMARY KEY,
3333
github_id bigint DEFAULT 0,
34-
linkedin_id varchar(100) NOT NULL,
34+
linkedin_id varchar(100) NOT NULL DEFAULT '',
3535
name varchar(70),
36-
login varchar(70),
37-
email varchar(70),
36+
login varchar(70) NOT NULL DEFAULT '',
37+
email varchar(70) NOT NULL DEFAULT '',
3838
created timestamptz,
3939
updated timestamptz
4040
)`

petfind/postgres/users.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (db *store) PutLinkedinUser(llu *petfind.LinkedinUser) (u *petfind.User, er
159159
UPDATE users SET
160160
name = $2,
161161
updated = now()
162-
WHERE github_id = $1
162+
WHERE linkedin_id = $1
163163
RETURNING id, github_id, linkedin_id, login, name, email, created, updated
164164
`
165165
userInsertStmt = `
@@ -197,6 +197,7 @@ func (db *store) PutLinkedinUser(llu *petfind.LinkedinUser) (u *petfind.User, er
197197
if err != nil {
198198
return nil, err
199199
}
200+
200201
return u, nil
201202
}
202203

petfind/postgres/users_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,63 @@ func TestPutGithubUser_emptyName(t *testing.T) {
157157
t.Errorf("PutGithubUser with empty name -> user.Name=%q, expected %q", got, want)
158158
}
159159
}
160+
func TestPutLinkedinbUser(t *testing.T) {
161+
s := setup(t)
162+
defer teardown(t, s)
163+
164+
// We Put the LinkedIn user for the first time. The user does not exist so we
165+
// expect Put to create the user.
166+
llu := &petfind.LinkedinUser{
167+
ID: "JANEDOE",
168+
FirstName: "Jane",
169+
LastName: "Doe",
170+
}
171+
got, err := s.PutLinkedinUser(llu)
172+
if err != nil {
173+
t.Fatal("PutLinkedinUser for non existent user returned err:", err)
174+
}
175+
176+
// Save created time to check it was the same when we Put for a second time
177+
// below.
178+
created := got.Created
179+
// Ignore time values.
180+
got.Created = time.Time{}
181+
got.Updated = time.Time{}
182+
183+
want := &petfind.User{
184+
ID: 1, // A newly created user should get ID 1 from Postgres.
185+
LinkedinID: "JANEDOE",
186+
Name: "Jane Doe",
187+
}
188+
if !reflect.DeepEqual(got, want) {
189+
t.Fatalf("PutLinkedinUser first run \nhave: %#v\nwant: %#v", got, want)
190+
}
191+
192+
// Attempt to Put again the github User. The LinkedIn user should have been
193+
// already been created from the previous run and we now expect the values
194+
// to be updated.
195+
llu = &petfind.LinkedinUser{
196+
ID: "JANEDOE",
197+
FirstName: "Michaella", // changed
198+
LastName: "Neirou", // changed
199+
}
200+
got, err = s.PutLinkedinUser(llu)
201+
if err != nil {
202+
t.Fatal("PutLinkedinUser for existing user returned err:", err)
203+
}
204+
205+
// Ignore updated.
206+
got.Updated = time.Time{}
207+
208+
want = &petfind.User{
209+
ID: 1, // ID stays the same as we are doing an update.
210+
LinkedinID: "JANEDOE",
211+
Name: "Michaella Neirou",
212+
Created: created,
213+
}
214+
// This time we expect the values to be updated but the created time should
215+
// be the same as the first run.
216+
if !reflect.DeepEqual(got, want) {
217+
t.Fatalf("PutLinkedinUser second run \nhave: %#v\nwant: %#v", got, want)
218+
}
219+
}

web/linkedin.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ func (s *server) handleLoginLinkedInCallback(w http.ResponseWriter, r *http.Requ
124124
}
125125

126126
func getLinkedinUser(client *http.Client, accessToken string) (*petfind.LinkedinUser, error) {
127-
fmt.Println("accessToken:", accessToken)
128-
resp, err := client.Get("https://api.linkedin.com/v1/people/~?format=json&oauth2_access_token=" + accessToken)
127+
resp, err := client.Get("https://api.linkedin.com/v1/people/~?format=json")
129128

130129
if err != nil {
131130
return nil, err

0 commit comments

Comments
 (0)