简体   繁体   中英

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

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?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM