【发布时间】: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