Luke Irvin Luke Irvin - 5 months ago 75
iOS Question

Getting Email Address from LinkedIn API

I want to include a "Sign Up using LinkedIn" feature in my app.

I'd like to be able to get some information, such as name and email.

By default I am able to get a name, but I'm stuck on getting the email.

My results are in JSON.

Here's my code:

- (IBAction)logInWithLinkedIn:(id)sender
{
if ([_client validToken])
{
[self requestMeWithToken:[_client accessToken]];
}
else
{
[_client getAuthorizationCode:^(NSString *code)
{
[self.client getAccessToken:code success:^(NSDictionary *accessTokenData) {

NSString *accessToken = [accessTokenData objectForKey:@"access_token"];
[self requestMeWithToken:accessToken];

} failure:^(NSError *error) {

NSLog(@"Quering accessToken failed %@", error);
}];
} cancel:^{

NSLog(@"Authorization was cancelled by user");

} failure:^(NSError *error) {

NSLog(@"Authorization failed %@", error);
}];
}
}

- (void)requestMeWithToken:(NSString *)accessToken
{
[self.client GET:[NSString stringWithFormat:@"https://api.linkedin.com/v1/people/~?oauth2_access_token=%@&format=json", accessToken] parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary *result) {

NSLog(@"current user %@", result);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"failed to fetch current user %@", error);

}];
}

- (LIALinkedInHttpClient *)client
{
LIALinkedInApplication *application = [LIALinkedInApplication applicationWithRedirectURL:@"redirectURL"
clientId:@"key"
clientSecret:@"secret"
state:@"state"
grantedAccess:@[@"r_emailaddress"]];
return [LIALinkedInHttpClient clientForApplication:application presentingViewController:nil];
}


My result is:

firstName

headline

lastName

siteStandardProfileRequest

Anyone see how I can get the email?

Answer

You should use:

[self.client GET:[NSString stringWithFormat:@"https://api.linkedin.com/v1/people/~:(id,first-name,last-name,maiden-name,email-address)?oauth2_access_token=%@&format=json", accessToken] parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary *result)

This may helps :)