1

在 Twitter 切换到 API 1.1 之前,我们正在(并且一直)使用 MGTwitterEngine + SAOauth 没有任何问题。我们已经进行了必要的更改以使用 1.1,并且几乎一切正常。
我们可以进行身份​​验证和获取状态更新,但我们不能发布。我们不能发布状态更新或友谊/创建或破坏。我们得到一个 401 错误返回:

错误域 = HTTP 代码 = 401 “操作无法完成。(HTTP 错误 401。)

我们有一个有效的访问令牌,因为我们可以登录并获取状态。只是似乎无法发布。

4

1 回答 1

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];
}
于 2013-06-16T11:13:33.220 回答