我想在我的應用中加入「使用LinkedIn註冊」功能。從LinkedIn獲取電子郵件地址API
我希望能夠獲得一些信息,例如姓名和電子郵件。
默認情況下,我能夠獲得一個名稱,但我一直在獲取電子郵件。
我的結果是JSON。
這裏是我的代碼:
- (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];
}
我的結果是:
的firstName
標題
lastName的
siteStandardProfileRequest
任何人都可以看到我如何獲得電子郵件?
而且你還應該添加在你的應用程序 'EMAILADDRESS' 權限:http://developer.linkedin.com/documents/authentication#granting – androniennn
我所用這個庫https://github.com/jeyben/IOSLinkedInAPI/tree/master/IOSLinkedInAPI然後它可以很好地與上面的解決方案。非常感謝:-) – byJeevan