2014-03-02 31 views
2

我總是得到這個twitter API請求401 Unauthorized響應。這是令人困惑的,因爲我有一個推特會議並正在簽署請求。提前致謝。401未經授權的錯誤使用Twitter API

NSURL *retweetAPIURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1.1/statuses/retweet/%@.json", tweetID]]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:retweetAPIURL]; 
request.HTTPMethod = @"POST"; 

if (self.twitterSession) { 

    [self.twitterSession signRequest:request]; 

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    operation.responseSerializer = [AFJSONResponseSerializer serializer]; 
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     block(nil, responseObject); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     block(error, nil); 
    }]; 

    [[NSOperationQueue mainQueue] addOperation:operation]; 
} else { 
    block([self createErrorWithMessage:NSLocalizedString(NSLocalizedString(@"TWITTER LOGIN ERROR", nil), nil)], nil); 
} 

回答

1

您確定請求正在簽名?你爲什麼不使用SLRequest

NSURL *retweetAPIURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1.1/statuses/retweet/%@.json", tweetID]]; 
ACAccount *account = // ...; 
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:retweetAPIURL parameters:nil]; 
request.account = account; 

NSURLRequest *preparedURLRequest = [request preparedURLRequest]; 

// create your AFHTTPRequestOperation using preparedURLRequest 
+0

我會嘗試下一個解決方案但問題是我使用Parse.com後端和登錄Twitter的應用程序(不登錄設置)。所以,你會告訴我如何檢查請求是否簽署? –

相關問題