【问题标题】:Twitter+oAuth engine returning 401 when trying to POST to twitter API 1.1Twitter+oAuth 引擎在尝试 POST 到 twitter API 1.1 时返回 401
【发布时间】:2013-06-20 14:40:47
【问题描述】:

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

Error Domain=HTTP Code=401 "无法完成操作。(HTTP 错误 401。)

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

【问题讨论】:

    标签: ios twitter


    【解决方案1】:

    我假设您使用 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];
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      • 2012-08-19
      • 2014-06-08
      • 2014-02-26
      • 2013-12-19
      • 2023-03-09
      相关资源
      最近更新 更多