Refresh token implementation in angular 2 by catching 401
<p>I am implementing refresh token in my angular 2 app and here is my code. My refresh token post call is not being called.I don't see any error while post call for refresh token either, to locate issue. Help is appreciated </p>

<pre><code>refreshToken(): Observable&lt;any&gt; {
        console.log("refreshing token");

        let body: string = 'grant_type=refresh_token&amp;refresh_token=' + localStorage.getItem("refresh_token");
        let headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded');
        let options = new RequestOptions({ headers: headers });

        return Observable.create(
            (observer: Observer&lt;any&gt;) =&gt; {
                this.http.post('https://fedqa.test.com/as/token.oauth2', body, options)
                    .map(res =&gt; res.json()).subscribe(
                    (data) =&gt; {
                        localStorage.removeItem("access_token");
                        localStorage.removeItem("refresh_token");
                        localStorage.setItem("access_token", data.access_token);
                        localStorage.setItem("refresh_token", data.refresh_token);
                        localStorage.setItem("token_type", data.token_type);
                        localStorage.setItem("expires_in", data.expires_in);
                    },
                    (error) =&gt; {
                        Observable.throw(error);
                    }
                    );
            });
    }
</code></pre>

<p>Here is my API call where I check for 401 and execute refreshToken() function and call the same function after I get new access token.</p>

<pre><code>getInspections(statusesinput: string[]): Observable&lt;IInspection[]&gt; {


        let body = JSON.stringify({ "Statuses": statusesinput });
        let headers = new Headers({ 'Content-Type': 'application/json' });
        headers.append('Authorization', 'Bearer' + localStorage.getItem("access_token"));
        headers.append('Access-Control-Allow-Origin', '*');
        let options = new RequestOptions({ headers: headers });

        return this.http.post(this._baseUrl + '/Inspections/ListInspections', body, options)
            .map((res: Response) =&gt; {
                this.inspections = res.json();
                return this.inspections;
            })
            .catch(initialError =&gt; {
                if (initialError &amp;&amp; initialError.status === 401) {
                    this.refreshToken().flatMap((data) =&gt; {
                        if (localStorage.getItem("access_token") != null) {
                            // retry with new token

                            return this.getInspections(statusesinput);
                        } else {
                            return Observable.throw(initialError);
                        }
                    });
                } else {
                    return Observable.throw(initialError);
                }
            })

    }
</code></pre>