我正在使用STTwitter演示应用程序在授权后从演示应用程序向我的推特帐户发布推文和图片。我正在尝试以下操作:
- (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方法的简单tweet也不能发布到我的twitter account.It always enter failure块(twitter post失败)上面的.The方法是从appdelegate调用的。
编辑:我成功地使用SLRequest在推特上发布内容,但如果没有在推特本地应用程序中登录,则无法使用我的应用程序再次登录到推特。上面的代码适用于twitter的授权,但不适用于发布内容;SLRequest适用于发布内容,但不适用于authorization.Any idea?
发布于 2017-05-25 02:52:33
我得到了解决方案,所以我想在这里张贴作为答案:
我使用了TWTRAPIClient,并使用名为TWTRAPIClient sendTwitterRequest的userid.And对其进行初始化
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);
}https://stackoverflow.com/questions/44136516
复制相似问题