给出了角度2的错误

给出了角度2的错误

本文介绍了到达http的linkedin的获取请求,给出了角度2的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用linkedin进行注册,需要获取访问代码和基本个人资料详细信息.但是,当我发布请求时,我在控制台中收到CORS错误,当我直接在浏览器中单击URL时,它将带我正确进入登录页面.达到要求会是什么问题?

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问题已解决:

CORS issue resolved:

我发现localhost:4200没有'/'并且在包含它之后,CORS错误已解决,但是击中我的http.get时遇到了问题组件:

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

  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(){

       }
    }

推荐答案

您不需要从http.get()map您的响应对象.它是由新的angular6 http自动完成的.

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

所以应该这样:

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

CORS相关的原始答案:

Original answer related to CORS :

根据 answer 中的建议,尝试从后端调用linkedIn api api,而不是直接从浏览器中获取.这将解决您与Cross origin相关的问题.

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将直接不允许任何其他应用程序执行其api.它必须来自您的后端api本身,而不是来自浏览器.

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.

这篇关于到达http的linkedin的获取请求,给出了角度2的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:01