简体   繁体   English

授权后如何使用STTwitter在Twitter上发布图像?

[英]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 : 授权后,我正在使用STTwitter演示应用程序将tweet和图像从演示应用程序发布到我的Twitter帐户。我正在尝试以下操作:

- (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. 即使使用postStatusesUpdate方法的简单推文也不会发布到我的Twitter帐户中,它始终会输入失败阻止(推文发布失败)。上述方法是从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. 编辑:我成功使用SLRequest在Twitter中发布了内容,但如果未在Twitter本机应用程序中登录,则无法使用我的应用程序再次登录到Twitter。 Above code works for authorization to twitter but not posting content and SLRequest works for posting content but not authorization.Any idea? 上面的代码可用于授权Twitter,但不能发布内容,而SLRequest可用于发布内容,但不能授权。

I got the solutions so thought of posting here as an answer: 我得到了解决方案,因此想到在这里发布答案:

I used TWTRAPIClient and initialize it using userid.And called TWTRAPIClient sendTwitterRequest 我使用了TWTRAPIClient并使用userid对其进行了初始化,并称为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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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