Alex
Alex

Reputation: 1453

Gettings 404 when calling linkedin API for access token after user authentication

I'm trying to setup Sign with linkedin in my Angular 15 app (google, microsoft and facebook are done and working).

I have my app configured and verified in linkedin with the following product:

Sign In with LinkedIn using OpenID Connect

I redirect the user successfully to log in at the following url:

private redirectUri = 'https://localhost:44319/';
private linkedInAuthScope = 'openid profile email'
private clientId = 'MYID'
private clientSecret = 'MYSECRET'
private linkedInAuthUrl = `https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=${this.clientId}&redirect_uri=${encodeURIComponent(this.redirectUri)}&scope=${encodeURIComponent(this.linkedInAuthScope)}&state=asdfasdfas`;

That works and redirects back to my app with the "code" and "state" parameters in the url. I grab the code from the url and make the following call to exchange the code for the access token. But it always returns back a 404 error.

getAccessToken(code: string): Observable<any> {
    
    const url = 'https://www.linkedin.com/oauth/v2/accessToken';

    const body = new HttpParams()
        .set('grant_type', 'authorization_code')
        .set('code', code)
        .set('client_id', this.clientId)
        .set('client_secret', this.clientSecret)
        .set('redirect_uri', this.redirectUri)

    const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');

    return this.http.post<any>(
        url,
        body,
        { headers });
}

Upvotes: 0

Views: 80

Answers (1)

Alex
Alex

Reputation: 1453

It turns on that you can't call https://www.linkedin.com/oauth/v2/accessToken from a browser. It has to be done from the back end. There is no documentation stating this.

Upvotes: 0

Related Questions