如何在Angular2中集成Linkedin

时间:2016-07-20 06:09:12

标签: angular

我对Angular2中的LinkedIn身份验证有疑问,我的代码是。

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.

请帮我解决我正在做的事情......

3 个答案:

答案 0 :(得分:1)

我通过检查第一个脚本标记中的源代码输出找到了问题。

问题出在你的位置:

"onLoad:" + this.OnLinkedInFrameworkLoad;

它将输出为:

onLoad: function () {↵            IN.Event.on(IN, "auth", _this.OnLinkedInAuth);↵        }

就像它在官方开发者文档中所说的那样

  

重要!

     

标签内的参数之间的换行很重要,因为没有其他形式的字段分隔符。如果您使用模板化语言或代码缩小器从渲染的HTML中删除空格,请注意您需要设置例外以保留此标记中的换行符,否则将无法正确解析它们。

来源:https://developer.linkedin.com/docs/getting-started-js-sdk

我还没有找到如何解决Angular 2的LinkedIn登录问题,但我会再试一次其他解决方案

答案 1 :(得分:1)

这对我有用:

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');
                   });
                }   
            }

答案 2 :(得分:0)

您使用的是javascript dom函数,因此您需要更改此内容:

"onLoad:" + this.OnLinkedInFrameworkLoad ;

对此:

"onLoad: onLinkedInLoad";

现在将其添加到index.html的<head>

<script type="text/javascript">

// Setup an event listener to make an API call once auth is complete
function onLinkedInLoad() {
    IN.Event.on(IN, "auth", getProfileData);
}

// Handle the successful return from the API call
function onSuccess(data) {
    console.log(data);
}

// Handle an error response from the API call
function onError(error) {
    console.log(error);
}

// Use the API call wrapper to request the member's basic profile data
function getProfileData() {
    IN.API.Raw("/people/~").result(onSuccess).error(onError);
}

</script>