blinkmacalahan
blinkmacalahan

Reputation: 520

LinkedIn v2 API Profile Picture

I'm very confused with the new API and how to best get the profile picture associated with a profile. I'm successfully receiving data from

https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))

however, I'm curious the best way to parse the response. Inside the response, I see displayImage~ -> elements -> identifiers which has the field identifier with the url to the image; however, identifiers is an array. In what conditions would this return multiple values and how do I determine which one to use? Right now, it just seems like I should blindly take the first element.

Thanks!

Upvotes: 2

Views: 2421

Answers (1)

Michael Montalbano
Michael Montalbano

Reputation: 480

I ended up just parsing the response, which actually gives four values in the elements, each are different profile picture sizes. The four values are [100x100, 200x200, 400x400, 800x800]. I wanted the highest quality so i used index 3 below:

let dataDictionary = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:Any]

let help1 = ((dataDictionary["profilePicture"] as! [String:Any])["displayImage~"]!) as! [String:Any]
let help2 = (help1["elements"]! as! [Any])[3] as! [String:Any]
let help3 = (help2["identifiers"] as! [Any])[0] as! [String:Any]

let imageurl = URL(string: help3["identifier"] as! String)
let data = try? Data(contentsOf: imageurl!)
self.imageView.image = UIImage(data: data!)

Upvotes: 1

Related Questions