How to use STTwitter for posting image on twitter after authorization?

102 Views Asked by At

I am using STTwitter demo app to post tweet and images to my twitter account from demo app after authorization.I am trying below :

- (void)setOAuthToken:(NSString *)token oauthVerifier:(NSString *)verifier {

[_twitter postAccessTokenRequestWithPIN:verifier successBlock:^(NSString *oauthToken, NSString *oauthTokenSecret, NSString *userID, NSString *screenName) {        
    _loginStatusLabel.text = [NSString stringWithFormat:@"%@ (%@) %@," , screenName];

     STTwitterAPI *twitter = [STTwitterAPI twitterAPIWithOAuthConsumerKey:_consumerKeyTextField.text consumerSecret:_consumerSecretTextField.text oauthToken:_twitter.oauthAccessToken oauthTokenSecret:_twitter.oauthAccessTokenSecret];

    [twitter verifyCredentialsWithUserSuccessBlock:^(NSString *username, NSString *userID){
       [self.twitter postStatusesUpdate:@"test" inReplyToStatusID:nil latitude:nil longitude:nil placeID:nil displayCoordinates:nil trimUser:nil autoPopulateReplyMetadata:nil excludeReplyUserIDsStrings:nil attachmentURLString:nil useExtendedTweetMode:nil successBlock:  ^(NSDictionary *status) {
            NSLog(@"twitter post success");

        }
            errorBlock:^(NSError *error) {
                NSLog(@"twitter post failed %@",error.localizedDescription);

        }];
    }

} errorBlock:^(NSError *error) {

    _loginStatusLabel.text = [error localizedDescription];
    NSLog(@"-- %@", [error localizedDescription]);
}];

}

Even simple tweet with postStatusesUpdate method is not posting to my twitter account.It always enter failure block(twitter post failed).The above method is called from appdelegate.

EDIT: I successfully post content in twitter using SLRequest but unable to login again to twitter using my app if not loggedin in twitter native app. Above code works for authorization to twitter but not posting content and SLRequest works for posting content but not authorization.Any idea?

1

There are 1 best solutions below

0
On

I got the solutions so thought of posting here as an answer:

I used TWTRAPIClient and initialize it using userid.And called TWTRAPIClient sendTwitterRequest

NSString *statusesShowEndpoint = @"https://upload.twitter.com/1.1/media/upload.json";
NSDictionary *params = @{@"media" : imageString};
NSError *clientError;
NSURLRequest *request = [client URLRequestWithMethod:@"POST" URL:statusesShowEndpoint parameters:params error:&clientError];
if (request) {
    [client sendTwitterRequest:request completion:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data) {
            NSLog(@"response %@",response);
            NSError *jsonError;
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
            NSLog(@"Media ID :  %@",[json objectForKey:@"media_id_string"]);
            NSString *mediaID =[json objectForKey:@"media_id_string"];

            if (mediaID!=nil) {
                NSString *statusesShowEndpoint = @"https://api.twitter.com/1.1/statuses/update.json";
                NSDictionary *message = @{@"status": websiteURL, @"wrap_links": @"true", @"media_ids": mediaID};
                NSError *clientError;
                NSURLRequest *request = [client URLRequestWithMethod:@"POST" URL:statusesShowEndpoint parameters:message error:&clientError];
                [client sendTwitterRequest:request completion:^(NSURLResponse *response, NSData *data, NSError *connectionError){
                    if (data) {
                        NSLog(@"response %@",response);
                        NSError *jsonError;
                        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
                    }
                    else {
                        NSLog(@"Error: %@", connectionError);
                    }
                }];

            }
        }
        else {
            NSLog(@"Error: %@", connectionError);
        }
    }];
}
else {
    NSLog(@"Error: %@", clientError);
}