iOS Twitter SLRequest返回url域错误-1012

时间:2013-07-14 20:00:49

标签: ios twitter oauth nsurlconnection twitter-oauth

我正在尝试执行反向oauth以获取服务器的twitter访问令牌。

我已经知道如何提交请求并收到回复,但是当我这样做时,它会给我这个错误:

Error: The operation couldn’t be completed. (NSURLErrorDomain error -1012.)

我查了一下,它说这意味着用户取消了请求。我不确定这是怎么可能的,我无法弄清楚如何解决它。

这是我的代码:

NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
    NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];

    NSString *oauth_nonce = [self genRandStringLength:32];
    NSString *oauth_timestamp = [timeStampObj stringValue];

    NSURL *feedURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"];

    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys: @"my key here", @"oauth_consumer_key", oauth_nonce, @"oauth_nonce", @"HMAC-SHA1", @"oauth_signature_method", oauth_timestamp, @"oauth_timestamp", @"1.0", @"oauth_version", @"reverse_auth", @"x_auth_mode", nil];

    SLRequest *twitterFeed = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:feedURL parameters:parameters];

    twitterFeed.account = self.userAccount;


    // Making the request

    [twitterFeed performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{

            // Check if we reached the reate limit

            if ([urlResponse statusCode] == 429) {
                NSLog(@"Rate limit reached");
                return;
            }

            // Check if there was an error

            if (error) {
                NSLog(@"The Error is: %@", error.localizedDescription);
                return;
            }

            // Check if there is some response data
            if (responseData) {

                NSLog(@"%@", responseData);

            }
        });
    }];

我必须有一些简单的东西,这让我无法完成一个项目。任何帮助都会很棒,谢谢!

2 个答案:

答案 0 :(得分:1)

错误代码-1012可能是由于身份验证质询。就我而言,设置中存在Twitter帐户,但由于某种原因未登录。输入帐户密码后,一切都运行良好。

答案 1 :(得分:0)

当我向https://api.twitter.com/oauth/request_token发送请求并在标头中添加额外的随机数和签名时,我遇到了此问题。具体来说,下面的代码给了我一个1012,但下一个代码块成功了。此代码改编自Sean Cook's Reverse Twitter Auth example

/**
 *  The first stage of Reverse Auth.
 *
 *  In this step, we sign and send a request to Twitter to obtain an
 *  Authorization: header which we will use in Step 2.
 *
 *  @param completion   The block to call when finished. Can be called on any thread.
 */
- (void)_step1WithCompletion:(TWAPIHandler)completion
{
    NSURL *url = [NSURL URLWithString:TW_OAUTH_URL_REQUEST_TOKEN];
    NSDictionary *dict = @{TW_X_AUTH_MODE_KEY: TW_X_AUTH_MODE_REVERSE_AUTH,
                           TW_OAUTH_NONCE:[self nonce],
                           TW_SIGNATURE_METHOD: TW_SIGNATURE_METHOD_VALUE,
                           };

    TWSignedRequest *step1Request = [[TWSignedRequest alloc] initWithURL:url parameters:dict requestMethod:TWSignedRequestMethodPOST];

    TWDLog(@"Step 1: Sending a request to %@\nparameters %@\n", url, dict);

    [step1Request performRequestWithHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            completion(data, error);
        });
    }

以下作品。请注意dict

中的更改
/**
 *  The first stage of Reverse Auth.
 *
 *  In this step, we sign and send a request to Twitter to obtain an
 *  Authorization: header which we will use in Step 2.
 *
 *  @param completion   The block to call when finished. Can be called on any thread.
 */
- (void)_step1WithCompletion:(TWAPIHandler)completion
{
    NSURL *url = [NSURL URLWithString:TW_OAUTH_URL_REQUEST_TOKEN];
    NSDictionary *dict = @{TW_X_AUTH_MODE_KEY: TW_X_AUTH_MODE_REVERSE_AUTH};

    TWSignedRequest *step1Request = [[TWSignedRequest alloc] initWithURL:url parameters:dict requestMethod:TWSignedRequestMethodPOST];

    TWDLog(@"Step 1: Sending a request to %@\nparameters %@\n", url, dict);

    [step1Request performRequestWithHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            completion(data, error);
        });
    }