getting error when retrieving connections using linkedin api in iphone - iphone

I have an application where i am integrating linkedin in my app.I want to fetch the connections of the logged in user.For this i have done the code but it is returning error:
{
"errorCode": 0,
"message": "Unknown authentication scheme",
"requestId": "98TKWP0T23",
"status": 401,
"timestamp": 1355726431107
}
This is the code that i have done:-
(NSMutableArray*) getFriendsList
{
NSString *url1 = [NSString stringWithFormat:#"http://api.linkedin.com/v1/people/id=%#:(connections)?format=application/json",linkedinid];
NSLog(#"%#",url1);
NSURL *url = [NSURL URLWithString:url1];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc]
initWithURL:url
consumer:consumer
token:token
callback:nil
signatureProvider:nil];
[request setValue:#"json" forHTTPHeaderField:#"x-li-format"];
[request setHTTPMethod:#"POST"];
NSError *err;
NSURLResponse *resp;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&resp error:&err];
NSMutableArray *arr = [[NSMutableArray alloc] init];
if (response != nil)
{
NSString *stringResponse = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *linkedin_response = [parser objectWithString:stringResponse error:nil];
}
}
but in the stringresponse it is returning the above mentioned error.

Try this :
- (void)getFriendsList
{
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/connections"];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
[request setValue:#"json" forHTTPHeaderField:#"x-li-format"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(networkApiCallResult:didFinish:)
didFailSelector:#selector(networkApiCallResult:didFail:)];
}
- (void)networkApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSDictionary *demo = [responseBody objectFromJSONString];
NSMutableArray *connections = [[NSMutableArray alloc] init];
for(int i=0;i<[[demo objectForKey:#"_count"] intValue];i++)
{
NSDictionary *person = [NSString stringWithFormat:#"%# %#",[[[[responseBody objectFromJSONString]
objectForKey:#"values"]
objectAtIndex:i]
objectForKey:#"firstName"],[[[[responseBody objectFromJSONString]
objectForKey:#"values"]
objectAtIndex:i]
objectForKey:#"lastName"]];
NSLog(#"Connection %d : %#",i,[NSString stringWithFormat:#"%# %#",[[[[responseBody objectFromJSONString]
objectForKey:#"values"]
objectAtIndex:i]
objectForKey:#"firstName"],[[[[responseBody objectFromJSONString]
objectForKey:#"values"]
objectAtIndex:i]
objectForKey:#"lastName"]]);
[connections addObject:person];
}
}
- (void)networkApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error
{
NSLog(#"%#",[error description]);
}

From what it looks like, you are not passing up the "Username / Password" in the post, and as such you are getting the "Unknown authentication scheme". I am unsure if you would be able to detect in your own app the connections of the logged in user, unless you are storing their credentials inside your app.

Related

I am unable to subscribe/unsubscribe to a subreddit using the reddit api, in iOS

Can somebody help me with this? I am using an NSURL connection for GET and POST methods. The reddit api is found here: http://www.reddit.com/dev/api#POST_api_subscribe
I am specifically confused with what the ('sr', 'sr_name') parameter is. Is it an array of some sort? Or do I have to pass either of 'sr' or 'sr_name'?
My current code:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.reddit.com/api/subscribe"]]];
[request setHTTPMethod:#"POST"];
request.HTTPBody = [[NSString stringWithFormat:#"api_type=json&action=%#&uh=%#&sr=%#", #"sub", modhashString, SubredditTitle] dataUsingEncoding:NSASCIIStringEncoding];
NSURLResponse *response;
NSError *error;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:POSTReply
options:NSJSONReadingMutableContainers
error:nil];
NSLog(#"%#", json);
NSMutableArray *temp = [[json objectForKey:#"json"] objectForKey:#"errors"];
if ([temp count] > 0){
for (int i = 0; i<[temp count]; i++) {
NSArray *temp2 = [[NSArray alloc] init];
temp2 = [temp objectAtIndex:i];
NSLog(#"Contains errors %#", temp2);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[temp2 objectAtIndex:1] message:nil delegate:self cancelButtonTitle:#":/" otherButtonTitles:nil, nil];
[alert show];
}
}
else{
}

How to fetch LinkedIn connections/Friends/Contacts in iOS SDK?

I want to fetch all my friends from my LinkedIn profile. Please suggest me any tutorial.
My code is :
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/connections:(id,first-name,last-name,email-address,picture-url,Positions)"];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:self.oAuthLoginView.consumer
token:self.oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
[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:)];
You can use this for connections:For request part see this.
- (void)connectionsApiCallResult:(LOAServiceTicket *)ticket didFinish:(NSData *)data
{
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(#"connectionsApiCallResult====%#",responseBody);
[responseBody release];
}
- (void)connectionsApiCallResult:(LOAServiceTicket *)ticket didFail:(NSError *)error
{
NSLog(#"%#",[error description]);
}
-(void)GetConnectionsCall
{
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/connections:(headline,first-name,last-name,picture-url,id)"];
LOAMutableURLRequest *request =
[[LOAMutableURLRequest alloc] initWithURL:url
consumer:self.consumer
token:TestAccessToken
callback:nil
signatureProvider:nil];
[request setValue:#"json" forHTTPHeaderField:#"x-li-format"];
LOADataFetcher *fetcher = [[[LOADataFetcher alloc] init] autorelease];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(requestTokenResult:didFinish:)
didFailSelector:#selector(requestTokenResult:didFail:)];
}
Please try the following steps
first go to https://www.linkedin.com/secure/developer edit your App scope , make sure you have checked the r_network scope .
Then go to OAuthLoginView of you project , then go to -(void) requestTokenFromProvider method .
edit you scope in below lines .
OARequestParameter *nameParam = [[OARequestParameter alloc] initWithName:#"scope"
value:#"r_fullprofile+rw_nus+r_network"];
OARequestParameter * scopeParameter=[OARequestParameter requestParameter:#"scope"
value:#"r_fullprofile rw_nus r_network"];

how to use twitter user search api in MGTwitterengine iphone

I am using MGtwitterengine in iPhone , I want to use USER search API http://api.twitter.com/1/users/search.json?q={username} but I don't find any method for this in MGTwitterengine. how can I use this API in iphone to get users.
Thanks
Use like This :-
- (void)searchforTwUser {
OAToken *access_token = [[OAToken alloc] initWithKey:[tEngine oauthKey] secret:[tEngine oauthSecret]];
OAConsumer *aconsumer = [[OAConsumer alloc] initWithKey:kOAuthConsumerKey
secret:kOAuthConsumerSecret];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
NSString *spaceString=#" ";
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:self.searchName] invertedSet];
if ([spaceString rangeOfCharacterFromSet:set].location == NSNotFound)
{
NSString *Name = [self.searchName stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://api.twitter.com/1/users/search.json?q=%#",Name]];
NSLog(#"search name 1 is ..................................... %#",url);
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:aconsumer token:access_token realm:nil
signatureProvider:nil];
[request setHTTPMethod:#"GET"];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(searchTicket:didFinishWithData:)
didFailSelector:#selector(searchTicket:didFailWithError:)];
[request release];
}
else
{
NSString *addStr = #"%20";
NSString *firstCapChar = [[searchName substringToIndex:1] capitalizedString];
NSString *cappedString = [searchName stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:firstCapChar];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://api.twitter.com/1/users/search.json?q=%#%#",cappedString,addStr]];
NSLog(#"search name 2 is ..................................... %#",url);
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:aconsumer token:access_token realm:nil
signatureProvider:nil];
[request setHTTPMethod:#"GET"];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(searchTicket:didFinishWithData:)
didFailSelector:#selector(searchTicket:didFailWithError:)];
[request release];
}
[access_token release];
[aconsumer release];
}
- (void) searchTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dict = [response objectFromJSONString];
NSLog(#"Dict %#",dict);
[twSearchArray removeAllObjects];
if (twSearchArray != nil) {
[twSearchArray release];
twSearchArray = nil;
}
twSearchArray = (NSMutableArray *)dict;
NSLog(#"Twitter %#",twSearchArray);
self.twLoaded = YES;
[twSearchArray retain];
[self prepareSearchResults];
[response release];
}
- (void) searchTicket:(OAServiceTicket *)ticket didFailWithError:(NSData *)error {
NSLog(#"Errors is %#",error.description);
}

Capturing PHP Response Through NSJSONSerialization

Hi I have this code here.
NSString *name = [[NSString alloc] initWithFormat:[nameField text]];
NSString *street = [[NSString alloc] initWithFormat:[streetField text]];
NSString *city = [[NSString alloc] initWithFormat:[cityField text]];
NSString *state = [[NSString alloc] initWithFormat:[stateField text]];
NSString *zip = [[NSString alloc] initWithFormat:[zipField text]];
NSString *urlToAuthPage = [[NSString alloc] initWithFormat:#"&name=%#&street=%#&city=%#&state=%#&zip=%#&lat=%#&lon=%#", name, street, city, state, zip, str1, str2];
NSData *postData = [urlToAuthPage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:#"%d",[urlToAuthPage length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://yourlink.com/poop.php"]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
NSError *error;
NSMutableArray *infoArray = (NSMutableArray*)[NSJSONSerialization JSONObjectWithData:postData options:kNilOptions error:&error];
NSLog(#"%#", [infoArray objectAtIndex:0]);
if (conn) {
UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"Your party has been successfully posted" delegate:nil cancelButtonTitle:#"Close" otherButtonTitles: nil];
[successAlert show];
//NSLog(conn);
nameField.text = #"";
streetField.text = #"";
cityField.text = #"";
stateField.text = #"";
zipField.text = #"";
[nameField resignFirstResponder];
[streetField resignFirstResponder];
[cityField resignFirstResponder];
[stateField resignFirstResponder];
[zipField resignFirstResponder];
}
What I'm trying to do is get the response the server gives out that is in NSJon. I'm trying capture what is below. I've tried using an NSMutableArray to get it but it didn't work. What am I doing wrong?
{"status":1}
The problem is that NSJSONSerialization answers witha NSDictionary, not an NSMutableArray. You can't force it to be an array, since it needs a value and a key (in this case the key is "status" and the value is "1")

'NSInvalidArgumentException', reason: '-[SBJsonParser objectWithString:error:]: unrecognized selector sent to instance 0x6695330'

While creating a login view on my iphone app, I ran into this error:
'NSInvalidArgumentException', reason: '-[SBJsonParser objectWithString:error:]:
unrecognized selector sent to instance 0x6695330'
It's coming from this line:
NSDictionary *results = [parser objectWithString:json_string error:nil];
From this method:
+ (BOOL)loginWithUsername:(NSString *)username password:(NSString *)password
{
NSString *urlString = [NSString stringWithFormat:#"%#login3", ROSE_ROOT_URL];
NSURL *url = [NSURL URLWithString:urlString];
NSString *requestString = [NSString stringWithFormat:#"&mobile=1&username=%#&password=%#", username, password];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[NSData dataWithBytes:[requestString UTF8String] length:[requestString length]]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
[request release];
// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON product objects
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *results = [parser objectWithString:json_string error:nil];
[json_string release];
[parser release];
// NSDictionary *results = [RoseFetcher fetch:request];
// [request release];
if ([[results objectForKey:#"password"] intValue] == 0)
return NO;
return YES;
}
Any help or explanation would be greatly appreciated.
According to the documentation for SBJsonParser, it doesn't have a method called -objectWithString:error: - an opinion that's corroborated by your findings at runtime. Try sending it a message it will respond to, like -objectWithString:.