我正在为iOS创建一个应用程序,我正在使用这个OAuth库。GET请求似乎工作正常,但是一旦我尝试进行POST请求,我就会收到以下错误消息:
[代码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编码的图像等参数异常大时会失败之外,一切都正常...我应该怎么做才能解决这个问题?