Reputation: 7224
I am attempting to perform reverse oauth to get twitter access tokens for a server.
I have figured out how to submit the request and receive a response, but when I do, it gives me this error:
Error: The operation couldn’t be completed. (NSURLErrorDomain error -1012.)
I looked this up, and it says that it means the user has canceled the request. I am not sure how this is possible, and I cannot figure out how to fix it.
Here is my code:
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);
}
});
}];
There must be something simple I am missing, and this is keeping me from finishing a project. Any help would be great, thanks!
Upvotes: 3
Views: 1164
Reputation: 911
Error code -1012 can be due to an authentication challenge. In my case, a Twitter account existed in Settings, but was not logged in for some reason. Once I entered the password for the account, everything worked perfectly.
Upvotes: 1
Reputation: 63546
I got this problem when I sent a request to https://api.twitter.com/oauth/request_token with an extra nonce and signature in the header. Specifically, the following code gave me a 1012, but the next chunk of code succeeded. This code is adapted from 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);
});
}
The following works. Note the change in the 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);
});
}
Upvotes: 0