OGeek|极客世界-中国程序员成长平台

标题: ios - 使用 twitter API 出现 401 未经授权的错误 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 19:45
标题: ios - 使用 twitter API 出现 401 未经授权的错误

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

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

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] addOperationperation];
} else {
    block([self createErrorWithMessage:NSLocalizedString(NSLocalizedString(@"TWITTER LOGIN ERROR", nil), nil)], nil);
}



Best Answer-推荐答案


您确定正在签署请求吗?为什么不直接使用 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

关于ios - 使用 twitter API 出现 401 未经授权的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22129292/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4