Objective c 使用简单NSMutableURLRequest的Twitter api请求

Objective c 使用简单NSMutableURLRequest的Twitter api请求,objective-c,twitter,Objective C,Twitter,我正试图获得一个用户最新的twits,一个简单的请求,但得到一个代码错误400 我的请求主体缺少什么 NSString *consumerKey = @"api key"; NSString *consumerSecret = @"api secret"; NSString *consumerKeyRFC1738 = [consumerKey stringByAddingPercentEscapesUsingEncoding:

我正试图获得一个用户最新的twits,一个简单的请求,但得到一个代码错误400

我的请求主体缺少什么

    NSString *consumerKey = @"api key";
    NSString *consumerSecret = @"api secret";

    NSString *consumerKeyRFC1738 = [consumerKey stringByAddingPercentEscapesUsingEncoding:
                                    NSASCIIStringEncoding];
    NSString *consumerSecretRFC1738 = [consumerSecret stringByAddingPercentEscapesUsingEncoding:
                                       NSASCIIStringEncoding];

    NSString *concatKeySecret = [[consumerKeyRFC1738 stringByAppendingString:@":"]    stringByAppendingString:consumerSecretRFC1738];

    NSLog(@"-%@",concatKeySecret);
    NSString *concatKeySecretBase64 = [[concatKeySecret dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];



    NSMutableURLRequest *request = [NSMutableURLRequest
                                    requestWithURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2"]];




    [request setHTTPMethod:@"POST"];
    [request setValue:[@"Basic" stringByAppendingString:concatKeySecretBase64] forHTTPHeaderField:@"Authorization"];
    [request setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];


    NSString *str = @"grant_type=client_credentials";
    NSData *httpBody = [str dataUsingEncoding:NSUTF8StringEncoding];

    [request setHTTPBody:httpBody];


    //NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    NSHTTPURLResponse *response;
    [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: nil];

    if ([response respondsToSelector:@selector(allHeaderFields)])
    {
        NSDictionary *dictionary = [response allHeaderFields];
        NSLog(@"%@",dictionary);
        NSLog(@"%@",response);

     }
我的回答是:

<NSHTTPURLResponse: 0x10cc21ba0> { URL: https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2 } { status code: 400, headers {
    "Content-Encoding" = gzip;
    "Content-Length" = 86;
    "Content-Type" = "application/json; charset=utf-8";
.....
....
{URL:https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2 }{状态代码:400,标题{
“内容编码”=gzip;
“内容长度”=86;
“内容类型”=“应用程序/json;字符集=utf-8”;
.....
....

如果它适合您的需要,我建议您使用SLRequest。请注意,此框架将请求使用用户帐户的权限

NSURL *showUserURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];
NSDictionary *parameters = @{ @"screen_name": @"thestrayhotdog" };

SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                        requestMethod:SLRequestMethodGET
                                                  URL:showUserURL
                                           parameters:parameters];
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
    if (granted) {
        NSArray * accounts = [accountStore accountsWithAccountType:accountType];
        if ([accounts count]) {
            ACAccount *twitterAccount = [accounts firstObject];
            request.account = twitterAccount;
            [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *connectionError) {
                NSLog(@"%@",[[NSString alloc] initWithData:responseData
                                                  encoding:NSUTF8StringEncoding]);
                NSLog(@"Status code: %ld",(long)[urlResponse statusCode]);
                NSError *error = nil;
                id result = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];

                if (error) {
                    // Handle error
                }
                // success;
            }];
        }
    }
}];

你为什么不使用SLRequest?它更简单吗?你有一个完整的例子吗?我的目标只是获取某个页面的最后一个twitter。就是这样。我不需要写信给twitter,只需要阅读最新的。我想说它更简单,但让我展示给你看,你决定它是否适合你的需要。一个警告是,该应用程序应该拥有使用twitter的权限r帐户,如果它没有,它会要求它。