I have received my API key for the twitter API.
Currently my request looks like this
NSString *twitterURL = [NSString stringWithFormat:#"https://api.twitter.com/1.1/search/tweets.json?q=xyz&count=20"];
Now how do I build my request url with my API key?
You can use the STTwitter library.
STTwitterAPI *twitter = [STTwitterAPI twitterAPIWithOAuthConsumerKey:#""
consumerSecret:#""
oauthToken:#""
oauthTokenSecret:#""];
[twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {
[_twitter getSearchTweetsWithQuery:#"xyz"
successBlock:^(NSDictionary *searchMetadata, NSArray *statuses) {
NSLog(#"-- statuses: %#", statuses);
} errorBlock:^(NSError *error) {
NSLog(#"-- error: %#", error);
}];
} errorBlock:^(NSError *error) {
NSLog(#"-- error: %#", error);
}];
Related
Hii i'm trying to get linkedin connection. i have see some same questions but did not find any relevant solution. please help me how can i find connection using latest SDK and which permission i need for connections.
i have used argument as
#define LinkedInApiUrl #"http://api.linkedin.com/v1/people/~/connections:(id,headline,first-name,last-name)"
- (void)requestMeWithToken:(NSString *)accessToken
{
[self.client GET:[NSString stringWithFormat:#"%#?oauth2_access_token=%#&format=json",LinkedInApiUrl,accessToken] parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary *result) {
NSLog(#"current user %#", result);
[self linkedinAuthenticationResponse:result error:nil];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"failed to fetch current user %#", error);
[self linkedinAuthenticationResponse:nil error:error];
}];
}
- (LIALinkedInHttpClient *)client
{
LIALinkedInApplication *application = [LIALinkedInApplication
applicationWithRedirectURL:[NSString stringWithFormat:#"%#",#"https://www.something.com"]
clientId:LINKEDIN_CLIENT_ID
clientSecret:LINKEDIN_CLIENT_SECRET
state:#"someState"
grantedAccess:#[ LISDK_EMAILADDRESS_PERMISSION, LISDK_BASIC_PROFILE_PERMISSION,LISDK_RW_COMPANY_ADMIN_PERMISSION,LISDK_W_SHARE_PERMISSION ]];
return [LIALinkedInHttpClient clientForApplication:application presentingViewController:self];
}
i am used STTweeter for posting Tweets but it showing EXC_BAD_ACESS Error
and showing error STTwitterTwitter ErrorDomain code 220
My code
- (void)setOAuthToken:(NSString *)token oauthVerifier:(NSString *)verifier {
// in case the user has just authenticated through WebViewVC
[self dismissViewControllerAnimated:YES
completion:^{//
}];
[_twitter postAccessTokenRequestWithPIN:verifier
successBlock:^(NSString *oauthToken, NSString *oauthTokenSecret,
NSString *userID, NSString *screenName) {
NSLog(#"-- screenName: %#", screenName);
self.twitter = [STTwitterAPI
twitterAPIAppOnlyWithConsumerKey:_consumerKeyTextField.text
consumerSecret:_consumerSecretTextField.text];
[_twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {
[_twitter postStatusUpdate:#"tweet text"
inReplyToStatusID:nil
latitude:nil
longitude:nil
placeID:nil
displayCoordinates:nil
trimUser:nil
successBlock:nil
errorBlock:nil];
NSLog(#"Sucess");
} errorBlock:^(NSError *error) {
NSLog(#"_____ERROR____%#", error);
}];
_loginStatusLabel.text = screenName;
}
errorBlock:^(NSError *error) {
_loginStatusLabel.text = [error localizedDescription];
NSLog(#"-- %#", [error localizedDescription]);
}];
}
I am having a very hard time here. There is one part in my application that STTwitter is successful and there is another part (using the same code) that does not return anything.
The part that does NOT work: `
-(IBAction)followTwitter:(id)sender {
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"twitter_on_file" ] == nil) {
UIAlertView *allert = [[UIAlertView alloc] initWithTitle:#"Uh oh!" message:#"You have not linked your twitter account quite yet! Head to My Account settins to do so." delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[allert show];
} else {
ACAccountStore *store1 = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType = [store1 accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
if ([twitterAccountType accessGranted]) {
[store1 requestAccessToAccountsWithType:twitterAccountType options:nil completion:^(BOOL granted, NSError *error) {
arrayOfUsernames = [[NSMutableArray alloc] init];
iosAccounts = [store1 accountsWithAccountType:twitterAccountType];
for (ACAccount *accou in iosAccounts) {
[arrayOfUsernames addObject:accou.username];
}
NSString *usernameOnFile = [[NSUserDefaults standardUserDefaults] objectForKey:#"twitter_on_file" ];
int tracker = 0;
for (NSString *username in arrayOfUsernames) {
if ([username isEqualToString:usernameOnFile]) {
NSLog(#"Using twitter account: %#", username);
STTwitterAPI *twitterAPI = [STTwitterAPI twitterAPIOSWithAccount:iosAccounts[tracker]];
[twitterAPI verifyCredentialsWithSuccessBlock:^(NSString *username) {
NSLog(#"Successfully authenticated the user");
} errorBlock:^(NSError *error) {
NSLog(#"Erorr: %#", error);
}];
NSLog(#"Twitter API: %#", twitterAPI);
[twitterAPI postFriendshipsCreateForScreenName:#"kickscaterer" orUserID:nil successBlock:^(NSDictionary *befriendedUser) {
NSLog(#"Befriend %#", befriendedUser);
} errorBlock:^(NSError *error) {
NSLog(#"Error: %#", error);
}];
} else {
tracker++;
}
}
}];
}
}
}
`
The part that DOES work:
STTwitterAPI *twitter= [STTwitterAPI twitterAPIOSWithAccount:iosAccounts[indexForAlert.row]];
[twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {
// ...
NSLog(#"Username: %#", username);
// [self.tableView reloadData];
[[NSUserDefaults standardUserDefaults] setObject:twitter.userName forKey:#"twitter_on_file"];
} errorBlock:^(NSError *error) {
NSLog(#"Error: %#", error);
// ...
}];
[twitter postFriendshipsCreateForScreenName:#"didi4" orUserID:nil successBlock:^(NSDictionary *befriendedUser) {
NSLog(#"Befriend %#", befriendedUser);
} errorBlock:^(NSError *error) {
NSLog(#"Error: %#", error);
}];
Thanks!
The postFriendshipsCreateForScreenName: method should be called inside the success block, at the same level than the log "Successfully authenticated the user".
i am using sttwiter sdk for twitter login in phonegap.
This method note return -
- (void)setOAuthToken:(NSString *)token oauthVerifier:(NSString *)verifier
{
[_twitter postAccessTokenRequestWithPIN:verifier successBlock:^(NSString *oauthToken, NSString *oauthTokenSecret, NSString *userID, NSString *screenName)
{
NSLog(#"-- screenName: %#", screenName);
}
errorBlock:^(NSError *error)
{
// _loginStatusLabel.text = [error localizedDescription];
NSLog(#"-- %#", [error localizedDescription]);
}];
}
I'm having trouble in posting a reply to a specific status ID
Here's my authentication code
twitterWithReply = [STTwitterAPI twitterAPIOSWithFirstAccount];
[twitterWithReply verifyCredentialsWithSuccessBlock:^(NSString *username) {
NSLog(#"Login Successful");
self.replyTextField.enabled = YES;
}
errorBlock:^(NSError *error){
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Sorry"
message:#"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}];
And here's my code for posting the reply
NSLog(#"the text content%#",self.replyTextField.text);
[self.replyTextField resignFirstResponder];
if (self.replyTextField.text.length>0 && self.replyTextField.text.length <=140) {
// Post reply
[twitterWithReply postStatusUpdate:self.replyTextField.text inReplyToStatusID:self.tweeetID latitude:nil longitude:nil placeID:nil displayCoordinates:nil trimUser:nil successBlock:^(NSDictionary *status) {
[self showSuccessDialog];
} errorBlock:^(NSError *error) {
NSLog(#"%#",[error localizedDescription]);
}];
}
Currently what it does is to post to my own timeline not in reply to a specific tweet. I'm under the impression that my authentication code is wrong. So how do I authenticate then?
I just checked this scenario with the development version of STTwitter and it works as expected.
If this code doesn't work for you, then please fill an issue with as much details as possible.
self.twitter = [STTwitterAPI twitterAPIOSWithFirstAccount];
[_twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {
[_twitter postStatusUpdate:#"test1" inReplyToStatusID:nil latitude:nil longitude:nil placeID:nil displayCoordinates:nil trimUser:nil successBlock:^(NSDictionary *status) {
NSLog(#"-- status1: %#", status);
[_twitter postStatusUpdate:#"test2" inReplyToStatusID:[status valueForKey:#"id_str"] latitude:nil longitude:nil placeID:nil displayCoordinates:nil trimUser:nil successBlock:^(NSDictionary *status) {
NSLog(#"-- status2: %#", status);
} errorBlock:^(NSError *error) {
NSLog(#"-- error2: %#", error);
}];
} errorBlock:^(NSError *error) {
NSLog(#"-- error1: %#", error);
}];
} errorBlock:^(NSError *error) {
NSLog(#"-- error0: %#", error);
}];