Twitter OAuth 1 POST请求无效

时间:2013-05-18 21:19:28

标签: ios objective-c twitter oauth twitter-oauth

我正在为iOS创建一个应用程序,我正在使用此OAuth library。 GET请求似乎工作正常,但一旦我尝试发出POST请求,我就会收到以下错误:

  

[code 32]无法验证您的身份。

现在我不太清楚发生了什么,因为POST请求的生成与GET请求非常相似。是什么原因造成了这个错误?

以下是生成请求的代码:

+ (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;
}

修改

我设置HTTP正文的代码的最后几行似乎导致了问题。当我删除行

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

一切正常,除非参数特别大,如发送base64编码图像,它显然会失败...我有什么办法可以解决这个问题?

2 个答案:

答案 0 :(得分:1)

嗯......

我不知道究竟是什么原因造成的。 Can't send private message to non-follower.

API Console怎么样?它可以测试Twitter API。也许对你好。

首先,您正确测试Direct Message API。然后比较API控制台和您的代码之间的结果。

答案 1 :(得分:0)

我通过添加以下HTTP标头字段解决了这个问题:

[request setValue:@"api.twitter.com" forHTTPHeaderField:@"X-HostCommonName"];
[request setValue:@"api.twitter.com" forHTTPHeaderField:@"Host"];
[request setValue:@"https://api.twitter.com" forHTTPHeaderField:@"X-Target-URI"];
[request setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];