Why I get this error when saving Twitter ACAccount

2019-05-18 21:30发布

问题:

I'm trying to save a Twitter account into the ACAccountStore.

I'm authenticating the user with MPOauth (this works perfectly, I can authenticate and post a message) and when I receive the access token and the access token secret I proceed to save the account.

First of all, I split the token access for taking only the token itself, not the user ID. After that this is my code

if ([username length] > 0 && [token length] > 0 && [secret length] > 0) {

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    ACAccount *account = [[ACAccount alloc] initWithAccountType:accountType];
    ACAccountCredential *credentials = [[ACAccountCredential alloc] initWithOAuthToken:token tokenSecret:secret];

    [account setCredential:credentials];
    [account setUsername:username];

    [accountStore saveAccount:account withCompletionHandler:^(BOOL success, NSError *error) {

        if (success) {

            NSLog(@"the account was saved!");

        } else {

            NSLog(@"the account was NOT saved");
        }

        [accountStore release];
        [account release];
        [credentials release];
    }];
}

But never works, I get this

Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)"

I read that this responds to an error authenticating the data against Twitter, thing that is done by iOS5 framework before saving the account.

I've already read this reponse and the post in Twitter.

What is wrong?

回答1:

if token is OAToken you should replace this

ACAccountCredential *credentials = [[ACAccountCredential alloc] initWithOAuthToken:token tokenSecret:token];

with this

ACAccountCredential *outhCredential = [[ACAccountCredential alloc] initWithOAuthToken:token.key tokenSecret:token.secret];

You should pass key & secret as 1st and 2nd arguments respectively .