我在通过解析框架发布到 Twitter 时遇到问题。我已经使用这个登录了我的用户,
[PFTwitterUtils linkUser:[PFUser currentUser]];
然后我尝试使用它发布到 Twitter,
NSString *bodyString = @"this is a test";
// Explicitly percent-escape the '!' character.
bodyString = [bodyString stringByReplacingOccurrencesOfString:@"!" withString:@"%21"];
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"];
NSMutableURLRequest *tweetRequest = [NSMutableURLRequest requestWithURL:url];
tweetRequest.HTTPMethod = @"POST";
tweetRequest.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[[PFTwitterUtils twitter] signRequest:tweetRequest];
NSURLResponse *response = nil;
NSError *error = nil;
// Post status synchronously.
NSData *data = [NSURLConnection sendSynchronousRequest:tweetRequest
returningResponse:&response
error:&error];
// Handle response.
if (!error) {
NSLog(@"Response: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
} else {
NSLog(@"Error: %@", error);
}
然后抛出这个错误,
Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed.
(NSURLErrorDomain error -1012.)" UserInfo=0xac5eac0
{NSErrorFailingURLKey=https://api.twitter.com/1.1/statuses/update.json,
NSErrorFailingURLStringKey=https://api.twitter.com/1.1/statuses/update.json,
NSUnderlyingError=0xc0945b0 "The operation couldn’t be completed.
(kCFErrorDomainCFNetwork error -1012.)"}
如有任何帮助,我们将不胜感激!
最佳答案
希望这对您有所帮助。
首先,仔细检查您的 Twitter API 是否启用了读/写身份验证。
其次,您需要在原始文本中包含“status=”前体。
最后,您需要完整的“%”编码,而不仅仅是“!”。
这是重构后的代码:
NSString *bodyString = @"status=this is a test with spaces";
bodyString = [bodyString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"];
NSMutableURLRequest *tweetRequest = [NSMutableURLRequest requestWithURL:url];
tweetRequest.HTTPMethod = @"POST";
tweetRequest.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[[PFTwitterUtils twitter] signRequest:tweetRequest];
NSURLResponse *response = nil;
NSError *error = nil;
// Post status synchronously.
NSData *data = [NSURLConnection sendSynchronousRequest:tweetRequest
returningResponse:&response
error:&error];
// Handle response.
if (!error) {
NSLog(@"Response: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
} else {
NSLog(@"Error: %@", error);
}
关于ios - 使用 Parse 框架发布推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21990879/