【问题标题】:Hitting the http get request for linkedin giving error in angular 2点击链接的http get请求在角度2中给出错误
【发布时间】:2019-02-10 06:29:24
【问题描述】:

我正在使用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(){

       }
    }

【问题讨论】:

  • 你能试着删除这个 .pipe(map(res => res.json())) 看看你得到什么回应吗?
  • 如果我删除它,我会收到此错误“(res:响应)=> void”类型的参数不可分配给“(值:响应)=> void”类型的参数。参数“res”和“value”的类型不兼容。类型 'import("f:/recipeBook/node_modules/@angular/http/src/static_response").Response' 不可分配给类型 'Response'。 “响应”类型中缺少“重定向”属性。

标签: angular angular2-services linkedin-api


【解决方案1】:

编辑:

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

所以应该是这样的:

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

CORS相关的原答案:

按照answer 的建议,尝试从您的后端 api 调用 linkedIn api,而不是直接从浏览器调用。这将解决您的Cross origin 相关问题。

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

【讨论】:

  • 您好,问题是由于我的重定向而发生的,我没有给出 localhost:4200/ ,/ is missing ,因为我遇到了 CORS 问题。但是现在在 Response.push ../node_modules/@angular/http/fesm5/http.js.Body.json 的 JSON.parse () 的位置 0 处的 JSON 中的意外令牌
  • 好的,从行中删除 json,因为 angular6 http 会自动为您执行此操作。所以现在也不需要mapthis.http.get(authURL).subscribe((res:Response)=>{ console.log("Http Get " + res); })
  • 我试过上面的评论。我收到了这个错误信息。 '(res: Response) => void' 类型的参数不可分配给'(value: Response) => void' 类型的参数。参数“res”和“value”的类型不兼容。类型 'import("f:/recipeBook/node_modules/@angular/http/src/static_response").Response' 不可分配给类型 'Response'。 “响应”类型中缺少属性“重定向”。
  • 如果你不确定它的类型,你可以添加任何类型。 this.http.get<T>(authURL).subscribe((res:any)=>{ console.log("Http Get " + res); })
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-18
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
相关资源
最近更新 更多