angular - 点击 linkedin 的 http get 请求,在 Angular 2 中给出错误

标签 angular angular2-services linkedin-api

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

CORS 问题已解决:

我发现 localhost:4200 没有“/”,包含它后,CORS 错误已解决,但点击我的 http.get 时出现问题 组件:

  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

最佳答案

编辑:

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

所以应该是这样的:

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

CORS相关的原始答案:

正如 answer 中所建议的那样,尝试从后端 api 而不是直接从浏览器调用 linkedIn api。这将解决您的跨源相关问题。

Linkedin 将直接不允许任何第三方应用程序执行其 api。它必须来自您的后端 api 本身,而不是浏览器。

关于angular - 点击 linkedin 的 http get 请求,在 Angular 2 中给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52179560/

相关文章:

angular - 如何使用 ng2-file-upload 在 base64 中发送图像?

javascript - 在 Angular 1 应用程序中使用 Angular 2 组件,无需升级插件

angular - 如何在angular2的一个组件中有多个 View

angular - 如何在 Angular 2 中绑定(bind) HTML 中组件的静态变量?

Linkedin Oauth2 客户端凭据授予类型

javascript - 为 Linkedin 公司简介插件设置响应宽度

html - 如何将div作为输入

javascript - 尝试通过 Angular 服务 POST 请求时出现错误请求 404

javascript - 通过字符串注入(inject)和解析 Angular2 提供程序

ruby - 使用 linkedin ruby​​ gem 的 OAuth 问题