ios – Twitter OAuth 1 POST请求无效

前端之家收集整理的这篇文章主要介绍了ios – Twitter OAuth 1 POST请求无效前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为iOS创建一个应用程序,我正在使用这个 OAuth library. GET请求似乎工作正常,但是一旦我尝试发出POST请求,我就会收到以下错误

[code 32] Could not authenticate you.

现在我不太确定发生了什么,因为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编码图像,它显然失败了……我有什么办法可以解决这个问题?

解决方法

好…

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

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

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

猜你在找的iOS相关文章