Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter OAuth 1 POST requests not working

I am creating an application for iOS and I am using this OAuth library. GET requests seem to work fine but as soon as I try to make POST requests I get the following error :

[code 32] Could not authenticate you.

Now I am not quite sure what is happening, as POST requests are generated pretty similarly to the GET requests. Any ideas what is causing this error ?

Here is the code generating the request:

+ (NSURLRequest *)preparedRequestForPath:(NSString *)path
                              parameters:(NSDictionary *)queryParameters
                              HTTPmethod:(NSString *)HTTPmethod
                              oauthToken:(NSString *)oauth_token
                             oauthSecret:(NSString *)oauth_token_secret
{
    if (!HTTPmethod
        || !oauth_token) return nil;



    NSMutableDictionary *allParameters = [self standardOauthParameters];
    allParameters[@"oauth_token"] = oauth_token;
    if (queryParameters) [allParameters addEntriesFromDictionary:queryParameters];

    NSString *parametersString = CHQueryStringFromParametersWithEncoding(allParameters, NSUTF8StringEncoding);

    NSString *request_url = API_URL;
    if (path) request_url = [request_url stringByAppendingString:path];
    NSString *oauth_consumer_secret = CONSUMER_SECRET;
    NSString *baseString = [HTTPmethod stringByAppendingFormat:@"&%@&%@", request_url.utf8AndURLEncode, parametersString.utf8AndURLEncode];
    NSString *secretString = [oauth_consumer_secret.utf8AndURLEncode stringByAppendingFormat:@"&%@", oauth_token_secret.utf8AndURLEncode];
    NSString *oauth_signature = [self.class signClearText:baseString withSecret:secretString];
    allParameters[@"oauth_signature"] = oauth_signature;

    NSString *queryString;
    if (queryParameters) queryString = CHQueryStringFromParametersWithEncoding(queryParameters, NSUTF8StringEncoding);
    if (queryString) request_url = [request_url stringByAppendingFormat:@"?%@", queryString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:request_url]];
    request.HTTPMethod = HTTPmethod;

    NSMutableArray *parameterPairs = [NSMutableArray array];
    [allParameters removeObjectsForKeys:queryParameters.allKeys];
    for (NSString *name in allParameters) {
        NSString *aPair = [name stringByAppendingFormat:@"=\"%@\"", [allParameters[name] utf8AndURLEncode]];
        [parameterPairs addObject:aPair];
    }
    NSString *oAuthHeader = [@"OAuth " stringByAppendingFormat:@"%@", [parameterPairs componentsJoinedByString:@", "]];
    [request setValue:oAuthHeader forHTTPHeaderField:@"Authorization"];

    if ([HTTPmethod isEqualToString:@"POST"]
        && queryParameters != nil) {
        NSData *body = [queryString dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:body];
    }

    return request;
}

EDIT

The very last lines of the code where I set the HTTP body seem to cause the problem. When I remove the lines

 NSData *body = [queryString dataUsingEncoding:NSUTF8StringEncoding];
 [request setHTTPBody:body];

everything works fine, except when the parameters are exceptionally bigger like sending a base64 encoded image, where it obviously fails... What could I possibly do to fix this ?

like image 852
the_critic Avatar asked May 18 '13 21:05

the_critic


1 Answers

Well...

I don't know what is cause it exactly. Can't send private message to non-follower.

How about API Console? It can be test Twitter APIs. maybe good to you.

First you test Direct Message API right way. and then compare results between API Console and your codes.

like image 103
Sung-Pil Lim Avatar answered Oct 06 '22 23:10

Sung-Pil Lim