2

I am doing signup with linkedin and need to fetch access code and basic profile details. But when I post the request, I am getting CORS error in my console, when I hit the URL directly in browser, it takes me to the signin page correctly. What would be the problem hitting the request?

CORS issue resolved:

I found localhost:4200 does not had '/' and after including it , CORS error was resolved , but hitting my http.get is having a problem component:

  import { Injectable } from '@angular/core';
    import {Http} from '@angular/http';
    import {Observable} from 'rxjs';
    import {map} from 'rxjs/operators'
    import { mapToExpression } from '../../../node_modules/@angular/compiler/src/render3/view/util';
    @Injectable({
      providedIn: 'root'
    })
    export class LinkedInService {

      constructor(private http:Http) {
        console.log("In Linked in constructor");
       }

       public linkedInSignIn(){
          console.log ("Linked in Sign in");
          let authURL ="https://www.linkedin.com/oauth/v2/authorization?"+// "https://www.linkedin.com/uas/oauth2/authorization?"+
          "response_type=code"+
          "&client_id=781872"+
          "&state=xys"+
          "&redirect_uri=http://localhost:4200/";
          console.log("auth rul" +authURL);
          this.http.get(authURL).pipe(map(res => res.json()))
          .subscribe((res:Response)=>{
            console.log("Http Get " + res);
          });//people => this.people = people);
       }    

      public linkedInSignOut(){

       }
    }

enter image description here

Nicoleta Wilskon
  • 627
  • 1
  • 6
  • 22
  • Can you please try removing this .pipe(map(res => res.json())) and see what are you getting in response? – Ritesh Sep 05 '18 at 08:03
  • I get this error if I remove it Argument of type '(res: Response) => void' is not assignable to parameter of type '(value: Response) => void'. Types of parameters 'res' and 'value' are incompatible. Type 'import("f:/recipeBook/node_modules/@angular/http/src/static_response").Response' is not assignable to type 'Response'. Property 'redirected' is missing in type 'Response'. – Nicoleta Wilskon Sep 05 '18 at 08:18

1 Answers1

2

EDIT :

You need not require to map your response object from http.get(). It is automatically done by new angular6 http.

So it should be this way :

this.http.get(authURL).subscribe((res:any)=> {console.log("Http Get " + res); });

Original answer related to CORS :

As suggested in that answer, try to invoke linkedIn api from your backend api and not from the browser directly. This will solve your Cross origin related problem.

Linkedin will directly not allow any third application to execute its api. It has to be from your backend api itself and not the browser.

Amit Chigadani
  • 22,665
  • 10
  • 67
  • 83
  • Hi the issue happened because of my redirecturi , I didnt given localhost:4200/ ,/ was missing , because of that I got CORS issue. But now Unexpected token < in JSON at position 0 at JSON.parse () at Response.push../node_modules/@angular/http/fesm5/http.js.Body.json this is error I am getting. – Nicoleta Wilskon Sep 05 '18 at 07:53
  • Ok remove `json` from line, because angular6 http automatically does that for you. So no need of `map` too now. `this.http.get(authURL).subscribe((res:Response)=>{ console.log("Http Get " + res); })` – Amit Chigadani Sep 05 '18 at 08:03
  • i tried above comment.I got this error message. Argument of type '(res: Response) => void' is not assignable to parameter of type '(value: Response) => void'. Types of parameters 'res' and 'value' are incompatible. Type 'import("f:/recipeBook/node_modules/@angular/http/src/static_response").Response' is not assignable to type 'Response'. Property 'redirected' is missing in type 'Response'. – Nicoleta Wilskon Sep 05 '18 at 08:14
  • 1
    You could add type as any, if you are not sure about its type. `this.http.get(authURL).subscribe((res:any)=>{ console.log("Http Get " + res); }) ` – Amit Chigadani Sep 05 '18 at 08:29