[英]Display LinkedIn profile photo and user profile using LinkedIn API
我想顯示用戶個人資料照片及其個人資料字段,例如公司名稱,職務,行業和位置。 我調用ProfilePicCall來檢索個人資料圖片。
- (void)ProfilePicCall
{
NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/picture-url"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
NSLog(@"the request is %@",request);
[request setValue:@"json" forHTTPHeaderField:@"x-li-format"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:@selector(profileApiCallResult:didFinish:)
didFailSelector:@selector(profileApiCallResult:didFail:)];
}
然后在圖像視圖中顯示照片,我使用以下代碼
- (void)profileApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSDictionary *profile = [responseBody objectFromJSONString];
// [responseBody release];
if ( profile )
{
NSLog(@"Profile is %@",profile);
NSString *picture_url = [[NSUserDefaults standardUserDefaults]valueForKey:@"linkedid_Profile_url"];
NSURL *imageurl = [NSURL URLWithString:picture_url];
NSData *imagedata = [[NSData alloc]initWithContentsOfURL:imageurl];
UIImage *image = [UIImage imageWithData: imagedata];
[LinkedInPicture setImage:image];
}
else
{
NSDictionary *profile = [responseBody objectFromJSONString];
NSLog(@"last path componemt is %@",profile);
}
// The next thing we want to do is call the network updates
[self networkApiCall];
[[NSUserDefaults standardUserDefaults] setValue:@"Used" forKey:@"linkedin"];
}
但是圖像未顯示在圖像視圖中。 請幫助我顯示圖像,以及如何使用LinkedIn API
謝謝。
我找到了我的問題的解決方案,應該將LinkedIn API調用更改為:
- (void)profileApiCall
{
// NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~"];
NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~:(id,first-name,last-name,industry,picture-url,location:(name),positions:(company:(name),title),specialties,date-of-birth,interests,languages)"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
NSLog(@"the request is %@",request);
[request setValue:@"json" forHTTPHeaderField:@"x-li-format"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:@selector(profileApiCallResult:didFinish:)
didFailSelector:@selector(profileApiCallResult:didFail:)];
}
謝謝。
我不喜歡上面的方法
首先,我使用該庫來幫助使用Auth2.0進行LinkedIn的登錄過程https://github.com/jeyben/IOSLinkedInAPI
如下使用它或閱讀文檔並自行更改代碼。
解決方案是此請求https://api.linkedin.com/v1/people/~:(picture-url)?oauth2_access_token=&format = json
LIALinkedInApplication *application = [LIALinkedInApplication applicationWithRedirectURL:@"http://www.ancientprogramming.com/liaexample"
clientId:@"clientId"
clientSecret:@"clientSecret"
state:@"DCEEFWF45453sdffef424"
grantedAccess:@[@"r_fullprofile", @"r_network"]];
LIALinkedInHttpClient *client = [LIALinkedInHttpClient clientForApplication:application presentingViewController:nil];
[client getAuthorizationCode:^(NSString *code) {
[client getAccessToken:code success:^(NSDictionary *accessTokenData) {
NSString *accessToken = [accessTokenData objectForKey:@"access_token"];
//here you already have you access token
//SOLUTION == https://api.linkedin.com/v1/people/~:(picture-url)?oauth2_access_token=<ACCESS_TOKEN>&format=json
//make a http request and get the result with an image url
} cancel:^{
NSLog(@"Authorization was cancelled by user");
} failure:^(NSError *error) {
NSLog(@"Authorization failed %@", error);
}];
}
對我來說很好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.