0

我正在使用 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。上面的代码适用于对 twitter 的授权但不发布内容,SLRequest 适用于发布内容但不授权。知道吗?

4

1 回答 1

0

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

我使用了 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);
}
于 2017-05-24T18:52:32.860 回答