【问题标题】:401 Unauthorized error using twitter API使用 Twitter API 的 401 未经授权的错误
【发布时间】:2014-04-03 11:16:37
【问题描述】:

对于这个 twitter API 请求,我总是收到 401 Unauthorized 响应。这很令人困惑,因为我有一个 Twitter 会话并且正在签署请求。提前致谢。

    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);
}

【问题讨论】:

    标签: ios objective-c twitter parse-platform


    【解决方案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
    

    【讨论】:

    • 接下来我会尝试这个解决方案但问题是我使用 Parse.com 作为后端并在应用程序中登录 Twitter(而不是登录设置)。那么,您能告诉我如何检查请求是否已签名吗?
    猜你喜欢
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 2019-02-01
    • 2013-06-15
    • 2012-06-24
    • 2012-04-08
    相关资源
    最近更新 更多