ios - 使用 Parse 框架发布推文

标签 ios twitter parse-platform

我在通过解析框架发布到 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/

相关文章:

ios - iOS UIView Transition subview 放错了片刻

ios - 在 iOS 应用程序中从 Firebase 获取发送推送通知的历史记录

iphone - 如何通过单击 UIPageControl 的点来更改页面

Android Facebook异常 : Login attempt failed

ios - 为什么我的 Core Graphics 绘图代码运行缓慢?

mysql - 如何成功地将 UTF-8 文本从 MySQL 发布到 Twitter

android - Twitter 是否支持 OAuth 2.0?

r - 我怎样才能在给定时间段内获得所有(即 1500 条以上)带有给定主题标签的推文?

ios - PFGeoPoint 永久保存在 map 上的东西?

android - 没有收到通知