Access user's iOS default Twitter account iOS

2019-09-02 01:27发布

问题:

I try to access my user's iOS default Twitter account.

I use STTwitter library. The below code used to work fine on my device, but now it's not. The returned "oAuthToken", "oAuthTokenSecret", and "userID" are nil. but it's still working on the simulator

STTwitterAPI *twitter = [STTwitterAPI twitterAPIWithOAuthConsumerName:nil
                                                          consumerKey:@"myConsumerKey"
                                                       consumerSecret:@"myConsumerSecret"];

[twitter postReverseOAuthTokenRequest:^(NSString *authenticationHeader) {

    STTwitterAPI *twitterAPIOS = [STTwitterAPI twitterAPIOSWithFirstAccount];

    [twitterAPIOS verifyCredentialsWithSuccessBlock:^(NSString *username) {

        [twitterAPIOS postReverseAuthAccessTokenWithAuthenticationHeader:authenticationHeader
                                                            successBlock:^(NSString *oAuthToken,
                                                                           NSString *oAuthTokenSecret,
                                                                           NSString *userID,
                                                                           NSString *screenName) {


                                                                // use the tokens...
                                                                self.oAuthToken = oAuthToken;
                                                                self.oAuthTokenSecret = oAuthTokenSecret;

                                                                if(self.oAuthToken && self.oAuthTokenSecret){
                                                                    completionHandler();

                                                                }
                                                                else{

                                                                    ACAccountStore *account = [[ACAccountStore alloc] init];
                                                                    ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

                                                                    NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];

                                                                    [account renewCredentialsForAccount:[arrayOfAccounts firstObject] completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) {

                                                                             [self getUserTwitterDataFromPhoneSettingsInView:view WithCompletionHandler:completionHandler];

                                                                    }];



                                                                }

                                                            } errorBlock:^(NSError *error) {
                                                                // Error

                                                            }];

    } errorBlock:^(NSError *error) {
        // no twitter account
        // user denied access to their account(s)

    }];

} errorBlock:^(NSError *error) {
    // ...

}];

回答1:

For some reason, it turns out that the Twitter account entered in iOS Settings lacks the 'oauth_token' property and may need to be validated again. A possible fix is to go to iOS Settings and setup the account again. Now, I'll try to find a way to detect this state in STTwitter.