-1

I want to write in Angular Typescript this request:

http://localhost:8080/engine/oauth/authorize?client_id=admin&redirect_uri=http://localhost:9000/callback&response_type=code&scope=read_profile

When I open this link into the web browser I get redirection response:

http://localhost:9000/callback?code=7CLwgh 

I tried to write this Typescript code:

authCodeRequest(name: string, password: string): Observable<any> {
    const body = new HttpParams()
      .set('client_id', 'admin')
      .set('redirect_uri', 'http://localhost:9000/callback')
      .set('response_type', 'code')
      .set('scope', 'read_profile');
    const headers = new HttpHeaders({
      'Content-Type': 'application/x-www-form-urlencoded',
    });

    return this.httpClient
      .get(environment.api.urls.auth.token, body.toString(), {
        headers
      })
      .pipe(
        map((response: Authorize) => {
          console.log(data)
          return true;
        })
      );
  }

I get this error for the get request: TS2554: Expected 1-2 arguments, but got 3.

What is the proper way to make a request and get the result from the redirected link param code?

Gregor Albert
  • 819
  • 1
  • 11
  • 23
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

1

You are trying to execute a POST, but you are actually executing a GET. Replace httpClient.get with httpClient.post.

If you really want to do a GET, you cannot use x-www-form-urlencoded. Your request parameters will be added to the request url.

let params= new HttpParams()
  .set('client_id', 'admin')
  .set('redirect_uri', 'http://localhost:9000/callback');
let options = { params: params };
return this.httpClient
  .get(environment.api.urls.auth.token, options)
  .pipe();
corradolab
  • 718
  • 3
  • 16
  • I want to make a GET. How I need to implement this? – Peter Penzov Aug 03 '20 at 21:26
  • Thank you for the update. Last question: When I receive as a response `http://localhost:9000/callback?code=7CLwgh` how I can get the result into `pipe()` and get the value of `code`? – Peter Penzov Aug 03 '20 at 22:05
  • Not sure that you can, until the response body contains the same value. See https://stackoverflow.com/questions/36885556/angular2-watch-for-302-redirect-when-fetching-resource – corradolab Aug 03 '20 at 23:22