如何在Angular2中集成Linkedin [英] How to Integrate Linkedin in Angular2
本文介绍了如何在Angular2中集成Linkedin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Angular2中遇到关于LinkedIn身份验证的问题.
I have problem regarding LinkedIn Authentication in Angular2 my code is.
import {Component , OnInit , NgZone} from 'angular2/core';
import {HTTP_PROVIDERS} from 'angular2/http';
import {ROUTER_DIRECTIVES , Router} from 'angular2/router';
declare var IN : any;
@Component({
directives : [ROUTER_DIRECTIVES],
selector : '.main',
providers : [HTTP_PROVIDERS],
templateUrl : './app/registration/employee.reg.html'
})
export class EmployeeRegistrationComponent implements OnInit{
constructor(private zone : NgZone){
this.zone.run(() => {
$.proxy(this.OnLinkedInFrameworkLoad, this);
});
}
ngOnInit(){
var linkedIn = document.createElement("script");
linkedIn.type = "text/javascript";
linkedIn.src = "http://platform.linkedin.com/in.js";
linkedIn.innerHTML = "\n"+
"api_key: **********\n" +
"authorize: true\n" +
"onLoad:" + this.OnLinkedInFrameworkLoad ;
document.head.appendChild(linkedIn);
var script = document.createElement("script");
script.type = "in/Login";
document.body.appendChild(script);
}
OnLinkedInFrameworkLoad = () => {
IN.Event.on(IN, "auth", this.OnLinkedInAuth);
}
OnLinkedInAuth = () => {
IN.API.Profile("me").result(result => this.ShowProfileData);
}
ShowProfileData(profiles) {
console.log(profiles);
var member = profiles.values[0];
var id=member.id;
var firstName=member.firstName;
var lastName=member.lastName;
var photo=member.pictureUrl;
var headline=member.headline;
//use information captured above
}
}
控制台抛出错误:angular2-polyfills.js:1250 Uncaught Error: Could not execute 'function () {'. Please provide a valid function for callback.
请帮助我,我在做什么错了...
Please Help me out what is wrong I'm doing...
推荐答案
这对我有用:
import { Component, OnInit, NgZone } from '@angular/core';
export class RegistrationComponent implements OnInit{
constructor(private ngZone: NgZone) {
window['onLinkedInLoad'] = () => ngZone.run(() => this.onLinkedInLoad());
window['displayProfiles'] = (profiles) => ngZone.run(() => this.displayProfiles(profiles));
window['displayProfilesErrors'] = (error) => ngZone.run(() => this.displayProfilesErrors(error));
}
ngOnInit() {
var linkedIn = document.createElement("script");
linkedIn.type = "text/javascript";
linkedIn.src = "http://platform.linkedin.com/in.js";
linkedIn.innerHTML = "\n" +
"api_key: ****\n" +
"authorize: true\n" +
"onLoad: onLinkedInLoad\n"+
"scope: r_basicprofile r_emailaddress";
document.head.appendChild(linkedIn);
var script = document.createElement("script");
script.type = "in/Login";
document.body.appendChild(script);
}
public onLinkedInLoad() {
IN.Event.on(IN, "auth", this.onLinkedInProfile);
}
public onLinkedInProfile() {
IN.API.Profile("me")
.fields("id", "firstName", "lastName", "email-address")
.result(this.displayProfiles)
.error(this.displayProfilesErrors);
}
public displayProfiles(profiles) {
console.log(profiles);
}
public displayProfilesErrors(error) {
console.debug(error);
}
//Invoke login window with button
public liAuth() {
IN.User.authorize(function () {
console.log('authorize callback');
});
}
}
这篇关于如何在Angular2中集成Linkedin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文