2014-02-24 54 views
0

我有问题通过解析框架发布到twitter。我已经登录使用此我的用户,使用解析框架发布推文

[PFTwitterUtils linkUser:[PFUser currentUser]]; 

然后我尝试发布使用这个叽叽喳喳,

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.)"} 

任何帮助将是大量赞赏!

+0

https://developer.apple.com/library/mac/documentation/Networking/Reference/CFNetworkErrors/Reference/reference.html了解错误代码的解释。这意味着“取消认证”,所以签名请求时可能出现一些问题? – jrturton

回答

6

希望这是及时的帮助。

首先,仔细检查您的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); 
} 
+0

谢谢soo。读/写身份验证启用是我的问题。我解决了它。 –

+0

每个人 - 不要忘记“状态=”! –

0

我用布尝试后鸣叫,并得到它的ID作为响应,但是,这是不允许的,由于Twitter的开发团队的答案时,我问,如果可能,这就是为什么使用解析此代码,可以发布推特,并得到其ID

if (![PFTwitterUtils isLinkedWithUser:user]) 
{ 
    [PFTwitterUtils linkUser:user block:^(BOOL succeeded, NSError *error) { 
     if ([PFTwitterUtils isLinkedWithUser:user]) { 
      NSLog(@"Woohoo, user logged in with Twitter!"); 
      //or put code here to login USER 
     } 
     else 
     { 
      [PFTwitterUtils unlinkUserInBackground:user block:^(BOOL succeeded, NSError *error) { 
       if (!error && succeeded) { 
        NSLog(@"The user is no longer associated with their Twitter account."); 
       } 
      }]; 
     } 
    }]; 
} 
else 
{ 
    // here when user logged in with twitter as a parse user 

    NSString *[email protected]"Success"; 
    NSString *bodyString = [NSString stringWithFormat:@"status=%@", [status stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    // 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]; 
    //[user a] 
    // [tweetRequest setValue:@"872557363-aeHtbWHK9DjaHsmo6ogI2PcriigIk0IPa0Hk9gAV" forHTTPHeaderField:@"access_token"]; 
    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]); 
     NSDictionary * parsedData = [[NSDictionary alloc]init]; 
     parsedData = [NSJSONSerialization 
         JSONObjectWithData:data //1 
         options:kNilOptions 
         error:&error]; 
     NSString *tweetId=[parsedData valueForKey:@"id_str"]; 
     NSLog(@"the hell tweet id = %@",tweetId); 

    } else { 
     NSLog(@"Error: %@", error); 
    } 

} 
0

这是斯威夫特版本:

if !PFTwitterUtils.isLinkedWithUser(user) { 
     PFTwitterUtils.linkUser(user, { 
      (succeeded: Bool?, error: NSError?) -> Void in 
      if PFTwitterUtils.isLinkedWithUser(user) { 
       println("Woohoo, user logged in with Twitter!") 
       let verify = NSURL(string: "https://api.twitter.com/1.1/account/verify_credentials.json") 
       var request = NSMutableURLRequest(URL: verify!) 
       PFTwitterUtils.twitter()!.signRequest(request) 
       var response: NSURLResponse? 
       var error:NSError? = nil 
       var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error) 

       if let jsonObject: AnyObject = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error:&error) { 
        if let dict = jsonObject as? NSDictionary { 
         NSLog("the hell dist =%@", dict) 
        } else { 
         println("not a dictionary") 
        } 
       } else { 
        println("Could not parse JSON: \(error!)") 
       } 
       // dict = NSJSONSerialization.JSONObjectWithData(data, options:, error: NSErrorPointer()) 

           } 
     }) 
    } 
    else 
    { 
     var tweetSTr=NSString() 
     tweetSTr="tweeted" 
     var bodyStr=NSString(format: "status=%@", tweetSTr.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!) 
      bodyStr=bodyStr.stringByReplacingOccurrencesOfString("!", withString: "%21") 


     var url=NSURL(string: "https://api.twitter.com/1.1/statuses/update.json") 
     var tweetRequest=NSMutableURLRequest(URL: url!) 
     tweetRequest.HTTPMethod="POST" 
     tweetRequest.HTTPBody=bodyStr.dataUsingEncoding(NSUTF8StringEncoding) 

     PFTwitterUtils.twitter().signRequest(tweetRequest) 
     var response: NSURLResponse? 
     var error:NSError? = nil 
     var data = NSURLConnection.sendSynchronousRequest(tweetRequest, returningResponse: &response, error: &error) 

     if let jsonObject: AnyObject = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error:&error) { 
      if let dict = jsonObject as? NSDictionary { 
       NSLog("myTweetINfo =%@", dict) 
       var tweetID=NSString() 
       tweetID=dict.valueForKey("id_str") as String 
       NSLog("myJustPostedTweetID = %@", tweetID) 

      } else { 
       println("not a dictionary") 
      } 
     } else { 
      println("Could not parse JSON: \(error!)") 
     } 

    }