![](/img/trans.png)
[英]Invitation API in Linkedin is not working properly? Can somebody share me the code?
[英]LinkedIn Integration in iOS: Invitation API not working
我正在嘗試將LinkedIn集成到需要進行身份驗證的iOS應用程序中,然后獲取個人資料詳細信息並從我的應用程序發布到LinkedIn。 我使用此示例項目LinkedIn OAuth示例客戶端作為參考,非常成功地完成了所有這些操作,我進行了一些修改,一切工作都很好。 我也使用LinnkedIn API的細節來實現這一點。 接下來, 我需要發送邀請以從同一應用程序連接到LinkedIn 。 我嘗試使用Invitataion API 。
這是我的代碼段:
-(void)sendInvitationToconnectTo:(NSString *)emailAddress withMessage:(NSString *)text
{
NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/mailbox"];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:[[OAHMAC_SHA1SignatureProvider alloc] init]];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSDictionary *temp=[[NSDictionary alloc]initWithObjectsAndKeys:@"/people/email=userName@gmail.com",@"_path",@"userName1",@"first-name",@"userName2",@"last-name", nil];
NSDictionary *temp2=[[NSDictionary alloc] initWithObjectsAndKeys:temp,@"person",nil];
NSArray *arr2=[[NSArray alloc]initWithObjects:temp2, nil];
NSDictionary *value=[[NSDictionary alloc]initWithObjectsAndKeys:arr2,@"values", nil];
NSDictionary *dict3=[[NSDictionary alloc]initWithObjectsAndKeys:@"friend",@"connect-type",nil];
NSDictionary *dict4=[[NSDictionary alloc] initWithObjectsAndKeys:dict3,@"invitation-request", nil];
NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:dict4,@"item-content",@"Say yes!",@"body",@"Invitation to connect.",@"subject",value,@"recipients", nil];
NSString *updateString = [dict JSONString];
NSLog(@"updateString : %@",updateString);
[request setHTTPBodyWithString:updateString];
[request setHTTPMethod:@"POST"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:@selector(sendRequestApiCallResult:didFinish:)
didFailSelector:@selector(sendRequestApiCallResult:didFail:)];
[request release];
}
- (void)sendRequestApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
NSLog(@"invitation sent");
[self networkApiCall];
}
- (void)sendRequestApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error
{
NSLog(@"%@",[error description]);
}
我的json字符串看起來像這樣:
{“ body”:“ Say yes!”,“ subject”:“邀請連接。”,“收件人”:{“ values”:[{“ person”:{“名字”:“ userName1”,“最后-name “:” USERNAME2" , “_路徑”: “/ people/email=userName@gmail.com”}}]}, “項內容”:{ “邀請請求”:{ “連接型”:”朋友“}}}
發送邀請時, 我沒有任何錯誤,並且在調試時, sendRequestApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
方法被調用,顯示邀請發送的文本。 但是,問題是目標LinkedIn用戶沒有收到任何請求!
有人可以幫我嗎? 我的代碼中有什么地方做錯了嗎?
注意:我通過下面提到的兩個鏈接來獲取解決方案,嘗試了這些解決方案,但徒勞無功。
1. 邀請API-被401(iOS和OAuthConsumer)困擾-已解決
2.linkedin Connect API給出錯誤401-iPhone
我得到的NSURLResponse
是401!
編輯::我自己解決了。 請參閱下面的答案。
很高興通知大家,經過長達4天的持續研究,我解決了這個問題! 我從Linkedin線程中獲得了解決方案
您只需要在代碼中進行一些更改:
NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/mailbox"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
[request setHTTPMethod:@"POST"];
NSString *messageToPerson = @"/people/email=target@gmail.com";
NSDictionary *person = [[NSDictionary alloc] initWithObjectsAndKeys:[[[NSDictionary alloc] initWithObjectsAndKeys:messageToPerson,@"_path",@"TargetFirstName",@"first-name",@"TargetSecondName",@"last-name",nil] autorelease], @"person",nil];
NSArray *valueArray = [[NSArray alloc] initWithObjects:person,nil];
NSDictionary *values = [[NSDictionary alloc] initWithObjectsAndKeys:valueArray,@"values", nil];
NSDictionary *ir = [[NSDictionary alloc] initWithObjectsAndKeys:[[[NSDictionary alloc] initWithObjectsAndKeys:@"friend",@"connect-type",nil] autorelease], @"invitation-request",nil];
NSDictionary *ic = [[NSDictionary alloc] initWithObjectsAndKeys:ir,@"item-content", nil];
NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:values,@"recipients",@"Invitation",@"subject",@"ConnectWithMe",@"body", ir, @"item-content", nil];
[request setValue:@"json" forHTTPHeaderField:@"x-li-format"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSLog(@"%@",[update description]);
NSString *updateString = [update JSONString];
[request prepare];
[request setHTTPBodyWithString:updateString];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request delegate:self
didFinishSelector:@selector(postUpdateApiCallResult:didFinish:)
didFailSelector:@selector(postUpdateApiCallResult:didFail:) withId:1];
[request release];
並在oAdata OADataFetcher.m
- (void)fetchDataWithRequest:(OAMutableURLRequest *)aRequest delegate:(id)aDelegate didFinishSelector:(SEL)finishSelector didFailSelector:(SEL)failSelector withId:(int)idtm
{
[request release];
request = [aRequest retain];
delegate = aDelegate;
didFinishSelector = finishSelector;
didFailSelector = failSelector;
//note this change
if (idtm!=1) {
[request prepare];
}
connection = [[NSURLConnection alloc] initWithRequest:aRequest delegate:self];
}
嘗試這個。 肯定會工作。 希望這對將來的訪客有幫助。
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.