使用ios中的Swift和OAuth登录Twitter

时间:2014-08-11 06:36:24

标签: ios swift twitter-oauth

我是Swift和IOS的新手。我想在我的swift iOS应用程序中使用OAuth实现使用Twitter帐户登录。

我实施了它,但我得到了

  

[{“message”:“无法验证您”,“代码”:32}]}

错误

2 个答案:

答案 0 :(得分:0)

是的,将iOS的OAuth实施到iOS是相当繁重的。在我的应用程序中,我使用ACAccountStore进行身份验证。对于你我可以建议使用this库。

答案 1 :(得分:-1)

太容易了

我使用的框架STTwitter非常好。另请观看此视频Twitter app only Authentication并查看STTwitterDemoiOS演示更清晰。

步骤1:创建Twitter应用并获取消费者密钥和消费者秘密。

步骤2:下载STTwitter Framework并将文件拖放到Xcode项目中。

第3步:UIWebView / Safari登录

- (IBAction)signInWithTwitterClicked:(id)sender {
    //login by website
    self.twitter = [STTwitterAPI twitterAPIWithOAuthConsumerKey:CONSUMER_KEY
                                                 consumerSecret:CONSUMER_SECRETE];
    [_twitter postTokenRequest:^(NSURL *url, NSString *oauthToken) {
        NSLog(@"URL: %@", url);
        NSLog(@"OauthToken: %@", oauthToken);
       // if(1) {
         //   [[UIApplication sharedApplication] openURL:url];
        //} else {
        //WebViewVc taken from STTwitterDemoiOS  demo.
            WebViewVC *webViewVC = [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewVC"];
            [self presentViewController:webViewVC animated:YES completion:^{
                NSURLRequest *request = [NSURLRequest requestWithURL:url];
                [webViewVC.webView loadRequest:request];
            }];
       // }
    } authenticateInsteadOfAuthorize:NO
                    forceLogin:@(YES)
                    screenName:nil
                 oauthCallback:@"myapp://twitter_access_tokens/"
                    errorBlock:^(NSError *error) {
                        NSLog(@"-- error: %@", error);
                      //  _loginStatusLabel.text = [error localizedDescription];
    }];

}

//步骤4和步骤5意味着回调我们的应用程序

步骤4:配置info.plist,如图所示。enter image description here

步骤5:处理应用程序委托方法

    - (NSDictionary *)parametersDictionaryFromQueryString:(NSString *)queryString {

        NSMutableDictionary *md = [NSMutableDictionary dictionary];

        NSArray *queryComponents = [queryString componentsSeparatedByString:@"&"];

        for(NSString *s in queryComponents) {
            NSArray *pair = [s componentsSeparatedByString:@"="];
            if([pair count] != 2) continue;

            NSString *key = pair[0];
            NSString *value = pair[1];

            md[key] = value;
        }

        return md;
    }


    - (BOOL)application:(UIApplication *)application
                openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication
             annotation:(id)annotation
    {
        //Twitter integration

       // if ([[url scheme] isEqualToString:@"myapp"] == NO) return NO;


        NSDictionary *d = [self parametersDictionaryFromQueryString:[url query]];
        NSString *token = d[@"oauth_token"];
        NSString *verifier = d[@"oauth_verifier"];
       // NSLog(@"Twitter Token=> %@\n Twitter Verifier=>%@",token,verifier);
        ViewController *vc = (ViewController *)[[self window] rootViewController];
        StartupViewController *startVc=(StartupViewController *)[[vc childViewControllers] objectAtIndex:0];
        [startVc setOAuthToken:token oauthVerifier:verifier];

//startVc is my controller where my "Login with twitter" button is there.

        //if no Facebook integration then return YES instead if return    //[FBAppCall handleOpenURL:url sourceApplication:sourceApplication];

 //Facebook Integration 
        return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];

    }

第6步:获取用户凭据。

-(void)setOAuthToken:(NSString *)token oauthVerifier:(NSString *)verifier {

    // in case the user has just authenticated through WebViewVC
    [self dismissViewControllerAnimated:YES completion:^{
       //Dismiss presented controller.
    }];

    [_twitter postAccessTokenRequestWithPIN:verifier successBlock:^(NSString *oauthToken, NSString *oauthTokenSecret, NSString *userID, NSString *screenName) {
        //Here is your Ans.
        NSLog(@"SUCCESS screenName: %@ ,userID=%@", screenName,userID);

    } errorBlock:^(NSError *error) {

        NSLog(@"-- %@", [error localizedDescription]);
    }];
}

希望这对某人有所帮助。