Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

ios - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...