2014-02-24 86 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!)") 
     } 

    }