Search code examples
iphoneipadtwittersharekittwitter-follow

iPhone follow a user on twitter programmatically


I want to follow a twitter user on a button click. I used the share kit follow me method using oauth. But always I am getting this error.

{"error":"Could not authenticate with OAuth.","request":"/1/friendships/create/priyankav89.json"}

Then I tried to implement it myself

NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1/friendships/create.json"];
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:@"871252998" forKey:@"user_id"];
[dict setObject:@"true" forKey:@"follow"];
[dict setObject:@"KXL0jFLJY6RI4lszOz8r5w" forKey:@"consumer"];
NSString *jsonRepresentation=[dict JSONString];
NSLog(@"jsonRepresentation = %@",jsonRepresentation);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3];
NSData *postData=[NSData dataWithBytes:jsonRepresentation.UTF8String length:[jsonRepresentation length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *error=nil;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"respon = %s",responseData.bytes);

still I am getting the same error.

I searched a lot. Still no use. I don't want to load the url in a UIWebView or in safari. I want to support the app from ios4. So ios5 built-in twitter app cannot be used.


Solution

  • I found the problem. In share kit follow me method they are not calling the authorization method before following. So we want to call authorize before login and save the access token.