实际上我正在使用 OAuth 2.0。并使用 afnetworking 类,通过使用 webview 给出 url 作为客户端 key 及其授权端点、响应类型和状态来显示 Linkedin 登录页面。一切正常
-(void)requestforAccessToken:(NSString *)authorisationCode
{
NSString *grantType = @"authorization_code";
NSString *postParameter = [NSString stringWithFormat:@"grant_type=%@&code=%@&redirect_uri=%@&client_id=%@&client_secret=%@",grantType,authorisationCode,encodedRdirectURL,linkedInKey,linkedInSecret];
NSData *postdata = [postParameter dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:accessTokenEndPoint]];
request.HTTPMethod = @"POST";
request.HTTPBody = postdata;
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFJSONResponseSerializer
serializerWithReadingOptions:NSJSONReadingAllowFragments];
[[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * response, id responseObject, NSError * error )
{
if (!error)
{
NSLog(@"Reply JSON: %@", responseObject);
NSString *accessToken = [responseObject objectForKey:@"access_token"];
NSLog(@"Access Token %@",accessToken);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:accessToken forKey:@"linkedInAccessToken"];
[defaults synchronize];
}
}] resume];
}
通过使用这种方法,我获得了访问 token ,现在我的问题是如何获取用户数据,如个人资料图片和所有基本数据。是否有像 facebook 这样的解决方案,只需附加 accesstoken,您将获得 JSON 数据。即使我不想使用 linkedin 的 SDK,也想直接交流所以请帮助。谢谢您
最佳答案
以下是我用来从 LinkedIn 用户获取所有可能信息的 URL(未与 LinkedIn 合作):
[NSString stringWithFormat:@"https://api.linkedin.com/v1/people/~:(first-name,last-name,id,industry,location,headline,specialties,current-share,summary,picture-url,email-address,positions)?oauth2_access_token=%@&format=json", accessToken]
accessToken
是您在身份验证后立即获得的。
只需在此 URL 上使用 AFNetworking
发出 GET
请求,您就会收到如您所说的 JSON 回复,如下所示:
{
emailAddress = "****@gmail.com";
firstName = Pi***e;
headline = "Developpeur iOS chez Pi****e";
id = 6Z***bFH3;
industry = "Computer Software";
lastName = "Test Account";
location = {
country = {
code = fr;
};
name = "Paris Area, France";
};
pictureUrl = "https://media.licdn.com/mpr/mprx/0_Gs*****U-9SswsbZKo3J*********eyCoHgYQadNFGRWQDyCwHjBVSdN4dVyw6gBFGIuQ3ZGnWmtsSdZUTjhIXErcmklEA7X";
positions = {
"_total" = 1;
values = (
{
company = {
name = Pi****e;
};
id = 823***52;
isCurrent = 1;
location = {
};
title = "Developpeur iOS";
}
);
};
}
关于ios - 关于在 linkedin 中获取用户数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38071554/