如何使用 LinkedIn Javascript API 获取电子邮件地址字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19843698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get email address field using the LinkedIn Javascript API?
提问by Ulises Figueroa
I'm using the LinkedIn Javascript API to sign in users to my application, however the API is not returning the email address even though I'm requiring permission for that specific field. I'm including the API script as follows:
我正在使用 LinkedIn Javascript API 将用户登录到我的应用程序,但是即使我需要对该特定字段的许可,该 API 也不会返回电子邮件地址。我包括 API 脚本如下:
<script type="text/javascript" src="//platform.linkedin.com/in.js">
api_key: API_KEY
scope: r_fullprofile r_emailaddress
</script>
then I'm including the Log In button in the markup:
然后我在标记中包含登录按钮:
<script type="in/Login" data-onAuth="onLinkedInAuth">
and finally I have a function to add the callback for the API response:
最后我有一个函数来为 API 响应添加回调:
function onLinkedInAuth() {
var fields = ['first-name', 'last-name', 'email-address'];
IN.API.Profile("me").fields(fields).result(function(data) {
console.log(data);
}).error(function(data) {
console.log(data);
});
};
I'm only getting the First and Last Name but the API doesn't return the email field.
我只得到名字和姓氏,但 API 不返回电子邮件字段。
Reference: https://developer.linkedin.com/documents/profile-fields#email
参考:https: //developer.linkedin.com/documents/profile-fields#email
回答by Aya Magdy
1- be sure you made email permission (r_emailaddress) in your app http://developer.linkedin.com/documents/authentication#granting
1- 确保您在您的应用程序中获得了电子邮件权限 (r_emailaddress) http://developer.linkedin.com/documents/authentication#granting
2- then you may use this
2-那么你可以使用这个
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: key
**onLoad: onLinkedInLoad**
authorize: true
</script>
<script>
function onLinkedInLoad() {
IN.Event.on(IN, "auth", onLinkedInAuth);
}
// 2. Runs when the viewer has authenticated
function onLinkedInAuth() {
IN.API.Profile("me").fields("first-name", "last-name", "email-address").result(function (data) {
console.log(data);
}).error(function (data) {
console.log(data);
});
}
</script>
hope this will help you :) thanks
希望这会帮助你 :) 谢谢
回答by Sricharan Krishnan
Hello there @Ulises Figueroa, May be I am coming in a bit late but this is how I had got this done:
你好@Ulises Figueroa,可能是我来晚了,但这就是我完成这项工作的方式:
Start off with the initial script tag on the top of your page within the head section:
从 head 部分页面顶部的初始脚本标记开始:
<script>
Client Id Number here:
onLoad: onLinkedInLoad
authorize: true
</script>
Then, in your JS File,(I had placed an external JS File to process this API sign up/ Auth), have the following details placed:
然后,在您的 JS 文件中(我已经放置了一个外部 JS 文件来处理此 API 注册/身份验证),放置以下详细信息:
function onLinkedInLoad() {
IN.Event.on(IN, "auth", getProfileData);
}
function onSuccess(data) {
console.log(data);
}
function onError(error) {
console.log(error);
}
function getProfileData(){
IN.API.Profile("me").fields(["firstName","lastName", "email-address", "positions"]).result(function(data) {
var profileData = data.values[0];
var profileFName = profileData.firstName;
var profileLName = profileData.lastName;
if(data.values[0].positions._total == "0" || data.values[0].positions._total == 0 || data.values[0].positions._total == undefined) {
console.log("Error on position details");
var profileCName = "Details Are Undefined";
}
else {
var profileCName = profileData.positions.values["0"].company.name;
}
var profileEName = profileData.emailAddress;
//console.log all the variables which have the data that
//has been captured through the sign up auth process and
//you should get them...
});
}
Then last but not the least, add the following in your HTML DOCUMENT which can help you initiate the window popup for the linkedin auth sign up form:
最后但并非最不重要的一点是,在您的 HTML 文档中添加以下内容,这可以帮助您启动linkedin 身份验证注册表单的弹出窗口:
<script type="in/Login"></script>
The above setup had worked for me. Sure this will help you out.
上述设置对我有用。当然,这会帮助你。
Cheers and have a nice day.
干杯,祝你有美好的一天。
回答by enrique-ramirez
Implementation looks good. I'd believe this is a result from the profile's privacy settings. Per linked-in's docs:
实施看起来不错。我相信这是个人资料隐私设置的结果。每个链接的文档:
Not all fields are available for all profiles. The fields available depend on the relationship between the user you are making a request on behalf of and the member, the information that member has chosen to provide, and their privacy settings. You should not assume that anything other than id is returned for a given member.
并非所有字段都可用于所有配置文件。可用的字段取决于您代表提出请求的用户与成员之间的关系、成员选择提供的信息以及他们的隐私设置。您不应假设为给定成员返回了 id 以外的任何内容。
回答by Ulises Figueroa
I figured out that this only happens with certain LinkedIn accounts, so this might be caused because some privacy setting with the email. I couldn't find any reference to the documentation so I had to consider the case when email field is not available.
我发现这只会发生在某些 LinkedIn 帐户上,所以这可能是由于电子邮件的某些隐私设置造成的。我找不到对文档的任何参考,因此我不得不考虑电子邮件字段不可用的情况。
回答by Siddhartha Mukherjee
Use this api:
使用这个api:
https://api.linkedin.com/v1/people/~:(id,firstName,lastName,num-connections,picture-url,emailAddress)?format=json
login() {
let scopes:any = ['r_basicprofile', 'r_emailaddress', 'rw_company_admin', 'w_share'];
this.linkedin.login(scopes, true)
.then(() => {
this.linkedin.getRequest('people/~:(id,firstName,lastName,num-connections,picture-url,emailAddress)')
.then(res => {
this.selfData = res;
})
.catch(e => {
console.log(e);
});
})
.catch(e => {
console.log('Error logging in', e);
});
}