【问题标题】:How to use STTwitter for posting image on twitter after authorization?授权后如何使用 STTwitter 在 Twitter 上发布图片?
【发布时间】:2017-10-23 12:10:39
【问题描述】:

我正在使用 STTwitter 演示应用程序在授权后从演示应用程序向我的 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]);
}];

}

即使是带有 postStatusesUpdate 方法的简单推文也不会发布到我的推特帐户。它总是进入失败块(推特发布失败)。上述方法是从 appdelegate 调用的。

编辑: 我使用 SLRequest 在 Twitter 中成功发布了内容,但如果未登录 Twitter 本机应用程序,则无法使用我的应用程序再次登录 Twitter。上面的代码用于授权推特但不发布内容,SLRequest 用于发布内容但不授权。知道吗?

【问题讨论】:

  • 看起来有一些故障块。看起来他们两个记录了一些东西。
  • @danh:sorry..我刚刚编辑过..有 2 个错误块..给我错误的错误块是“twitter post failed”。
  • 好像也记录了error.localizedDescription

标签: ios objective-c sttwitter


【解决方案1】:

我得到了解决方案,所以想在这里发布作为答案:

我使用了 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);
}

【讨论】:

    猜你喜欢
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2015-09-30
    • 2013-03-14
    • 2013-12-26
    • 1970-01-01
    相关资源
    最近更新 更多