【问题标题】:Unable to catch response from rest API无法捕获来自 REST API 的响应
【发布时间】:2017-11-06 15:52:53
【问题描述】:

我正在尝试在 Angular 2 应用程序中调用 linkedIn 访问令牌 API。 我能够在 chrome 调试网络中看到响应。 API 正在完美地返回数据,但我的问题是在我的应用程序中我无法捕捉到该响应。 以下是我的代码。

 getProfileData() {

    //IN.API.Raw("https://api.linkedin.com/v1/people/~?oauth2_access_token=" + this.code + "&format=json").result(this.displayProfiles).error(this.displayProfilesErrors);
    this.apiclass.GetTokenAPILink(this.code).subscribe(
        x => {
            this.datalink = x;
            console.log("Linkedin",x)
        });

}
  public GetTokenAPILink(code: any): Observable<any> {
    return this.LinkedinAPIWithHttpInfo(code)
        .map((response: Response) => {
            if (response.status === 204) {
                return undefined;
            } else {
                return response.json() || {};
            }
        });
}
public LinkedinAPIWithHttpInfo(code:any): Observable<Response> {
    const path = "https://www.linkedin.com/oauth/v2/accessToken?grant_type=authorization_code&code=" + code + "&redirect_uri=http://localhost:4200/ProfileLogin&client_id=--clientID--&client_secret=--key--";

    let queryParameters = new URLSearchParams();
    let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845


    // to determine the Content-Type header
    let consumes: string[] = [
        'application/json',
        'text/json',
        'application/xml',
        'text/xml',
        'application/x-www-form-urlencoded'
    ];

    // to determine the Accept header
    let produces: string[] = [
    ];

    headers.set('Content-Type', 'application/x-www-form-urlencoded');

    let requestOptions: RequestOptionsArgs = new RequestOptions({
        method: RequestMethod.Post,
        headers: headers,
        body: "Hi"
    });
    // https://github.com/swagger-api/swagger-codegen/issues/4037


    return this.http.request(path, requestOptions);
}

以下部分未捕获来自 API 的响应

return this.LinkedinAPIWithHttpInfo(code)
        .map((response: Response) => {
            if (response.status === 204) {
                return undefined;
            } else {
                return response.json() || {};
            }
        });

【问题讨论】:

  • 如果您在if (response.status === 204) 块中放置一个console.log 语句,它会登录开发工具吗?
  • 控件甚至没有进入该块内。
  • 使用 if (response.getStatusCode() == HttpStatus.NO_CONTENT) 可能会对您有所帮助。
  • 您在开发控制台中看到的请求状态如何?
  • @Timothy 200 OK

标签: angular rest api linkedin-api


【解决方案1】:

我认为您的请求返回了一些符合错误条件的代码(即小于 200 且大于 299),因此正在调用错误回调。可能是永久重定向等,不确定。

在您的订阅中放置一个错误回调并检查错误是什么

getProfileData() {

//IN.API.Raw("https://api.linkedin.com/v1/people/~?oauth2_access_token=" + this.code + "&format=json").result(this.displayProfiles).error(this.displayProfilesErrors);
this.apiclass.GetTokenAPILink(this.code).subscribe(
    x => {
        this.datalink = x;
        console.log("Linkedin",x)
    }, error => {
   console.log("Error is ",error);

  });

这将打印您遇到的错误,并且应该能够引导您朝着正确的方向前进。

【讨论】:

  • 这是错误的。错误状态:0 statusText:“”类型:3
  • 在地图的 else 块中,只需返回没有任何管道的 Json...返回 response.json()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
  • 2020-12-10
相关资源
最近更新 更多