在Twitters切换到API 1.1之前,我们(并且一直)使用MGTwitterEngine + SAOauth没有问题。我们已经对1.1进行了必要的修改,几乎所有工作都有效 我们可以验证和获取状态更新,但我们无法发布。我们无法发布状态更新或友情/创建或销毁。我们收到了401错误:
错误域= HTTP代码= 401“无法完成操作。(HTTP错误401。)
我们有一个有效的访问令牌,因为我们可以登录并获取状态。似乎无法发布POST。
答案 0 :(得分:0)
我假设您使用SA_OAuthTwitterEngine。尝试使用以下实现替换方法_sendRequestWithMethod ...:
- (NSString *)_sendRequestWithMethod:(NSString *)method
path:(NSString *)path
queryParameters:(NSDictionary *)params
body:(NSString *)body
requestType:(MGTwitterRequestType)requestType
responseType:(MGTwitterResponseType)responseType
{
BOOL isPOST = (method && [method isEqualToString:@"POST"]);
NSString *fullPath = path;
if (params) {
fullPath = [self _queryStringWithBase:fullPath parameters:(isPOST ? nil : params) prefixed:YES];
}
// --------------------------------------------------------------------------------
// modificaiton from the base clase
// the base class appends parameters here
// --------------------------------------------------------------------------------
// if (params) {
// fullPath = [self _queryStringWithBase:fullPath parameters:params prefixed:YES];
// }
// --------------------------------------------------------------------------------
NSString *urlString = [NSString stringWithFormat:@"%@://%@/%@",
(_secureConnection) ? @"https" : @"http",
_APIDomain, fullPath];
NSURL *finalURL = [NSURL URLWithString:urlString];
if (!finalURL) {
return nil;
}
// --------------------------------------------------------------------------------
// modificaiton from the base clase
// the base class creates a regular url request
// we're going to create an oauth url request
// --------------------------------------------------------------------------------
// NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:finalURL
// cachePolicy:NSURLRequestReloadIgnoringCacheData
// timeoutInterval:URL_REQUEST_TIMEOUT];
// --------------------------------------------------------------------------------
OAMutableURLRequest *theRequest = [[[OAMutableURLRequest alloc] initWithURL:finalURL
consumer:self.consumer
token:_accessToken
realm: nil
signatureProvider:nil] autorelease];
if (method) {
[theRequest setHTTPMethod:method];
}
[theRequest setHTTPShouldHandleCookies:NO];
// Set headers for client information, for tracking purposes at Twitter.
[theRequest setValue:_clientName forHTTPHeaderField:@"X-Twitter-Client"];
[theRequest setValue:_clientVersion forHTTPHeaderField:@"X-Twitter-Client-Version"];
[theRequest setValue:_clientURL forHTTPHeaderField:@"X-Twitter-Client-URL"];
NSMutableArray *oauthParams = [NSMutableArray array];
for (id key in params) {
id value = [params objectForKey:key];
[oauthParams addObject:[OARequestParameter requestParameterWithName:key value:value]];
}
[theRequest setParameters:oauthParams];
// --------------------------------------------------------------------------------
// modificaiton from the base clase
// our version "prepares" the oauth url request
// --------------------------------------------------------------------------------
[theRequest prepare];
// Create a connection using this request, with the default timeout and caching policy,
// and appropriate Twitter request and response types for parsing and error reporting.
MGTwitterHTTPURLConnection *connection;
connection = [[MGTwitterHTTPURLConnection alloc] initWithRequest:theRequest
delegate:self
requestType:requestType
responseType:responseType];
if (!connection) {
return nil;
} else {
[_connections setObject:connection forKey:[connection identifier]];
[connection release];
}
return [connection identifier];
}