【问题标题】:How to Integrate Linkedin in Angular2如何在 Angular2 中集成 Linkedin
【发布时间】:2016-11-23 05:34:59
【问题描述】:

我的代码是关于 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.

请帮我看看我做错了什么......

【问题讨论】:

  • 那么是 Angular 允许您将脚本标签附加到 dom 吗?
  • 是的.. 因为我正在使用 javascript dom 函数.. 来处理脚本标签
  • @DoubleH 你有没有让这个工作?我很难实现这一点,而且关于如何做这样的事情的文档很少。

标签: angular


【解决方案1】:

你使用的是 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>

【讨论】:

    【解决方案2】:

    这对我有用:

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

    【讨论】:

    • 我试过了,但我得到了 IN.Event.on(IN undefined ,你能告诉我如何解决吗?
    • 查看原帖:声明 var IN: any; @Component({
    • 我已经添加了但同样的错误以及如果我尝试你的代码函数没有调用?
    • LinkedIn 不再支持 JavaScript SDK stackoverflow.com/questions/55862429/…
    【解决方案3】:

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

    问题在于你放哪里:

    "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 登录问题,但我会再尝试其他解决方案

    【讨论】:

      猜你喜欢
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      • 2011-09-06
      • 1970-01-01
      • 2018-02-02
      相关资源
      最近更新 更多