有没有简单的方法来获取用户LinkedIn个人资料照片?
理想情况下与Facebook的相似 - http://graph.facebook.com/userid/picture
小智 51
您可以使用此调用检索原始照片尺寸:
http://api.linkedin.com/v1/people/~/picture-urls::(original)
请注意,这可能是任何大小,因此您需要在您身边进行缩放,但图像是用户上传的原始图像.
Ada*_*erg 33
不那么容易......你需要通过OAuth,然后代表会员,你要求:
http://api.linkedin.com/v1/people/{user-id}/picture-url
Mad*_*ota 10
完成使用OAuth 2.x 的Linkedin 用户身份验证后,向人员URL发出请求.
其中~代表当前经过身份验证的用户.响应将是这样的......
{
"id": "KPxRFxLxuX",
"emailAddress": "johndoe@example.com",
"firstName": "John",
"lastName": "Doe",
"formattedName": "John Doe",
"pictureUrl": "https://media.licdn.com/mpr/mprx/0_0QblxThAqcTCt8rrncxxO5JAr...cjSsn6gRQ2b"
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
小智 10
如果您使用API的2.0版本(所有开发人员都需要在2019年3月1日之前进行迁移),则应使用投射来扩展profilePicture.displayImage。如果执行此操作,则将在其中包含完整的JSON元素displayImage~(“〜”不是错字),profilePicture其中包含您可能需要的所有信息。
https://api.linkedin.com/v2/me?projection=(id,profilePicture(displayImage~:playableStreams))
您可以在Profile Picture API文档中查看更多信息,以查看JSON响应或Profile API文档。
小智 6
我在我的解决方案中使用OWIN,因此在用户允许您的应用程序使用LinkedIn凭据后,可以使用简单而简单的GET请求来访问URL https://api.linkedin.com/v1/people/~:(picture-url)?format=json如前所述,请求标题中的Bearer授权解决了我的问题.
我的Startup.Auth.cs文件
var linkedInOptions = new LinkedInAuthenticationOptions()
{
ClientId = [ClientID],
ClientSecret = [ClientSecret],
Provider = new LinkedInAuthenticationProvider()
{
OnAuthenticated = (context) =>
{
// This is the access token received by your application after user allows use LinkedIn credentials
context.Identity.AddClaim(new Claim(
"urn:linkedin:accesstoken", context.AccessToken));
context.Identity.AddClaim(new Claim(
"urn:linkedin:name", context.Name));
context.Identity.AddClaim(new Claim(
"urn:linkedin:username", context.UserName));
context.Identity.AddClaim(new Claim(
"urn:linkedin:email", context.Email));
context.Identity.AddClaim(new Claim(
"urn:linkedin:id", context.Id));
return Task.FromResult(0);
}
}
};
app.UseLinkedInAuthentication(linkedInOptions);
Run Code Online (Sandbox Code Playgroud)
我在LinkedIn中获取用户个人资料图片的方法:
public string GetUserPhotoUrl(string accessToken)
{
string result = string.Empty;
var apiRequestUri = new Uri("https://api.linkedin.com/v1/people/~:(picture-url)?format=json");
using (var webClient = new WebClient())
{
webClient.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + accessToken);
var json = webClient.DownloadString(apiRequestUri);
dynamic x = JsonConvert.DeserializeObject(json);
string userPicture = x.pictureUrl;
result = userPicture;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
最后我的动作片段消耗了上面的方法:
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
...
var externalIdentity = HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
string accessToken =
externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == "urn:linkedin:accesstoken").Value;
model.PhotoUrl = GetUserPhotoUrl(accessToken);
...
}
Run Code Online (Sandbox Code Playgroud)
我希望它可以提供帮助.最好的祝福
| 归档时间: |
|
| 查看次数: |
62736 次 |
| 最近记录: |