Twitter API获取用户信息

时间:2014-12-29 08:03:17

标签: ios objective-c twitter sttwitter

有很多相同的问题,但我也找不到适合我的解决方案。我决定发布这个问题。用户通过Twitter登录的应用程序然后尝试获取用户信息,但总是得到错误。我使用 STTwitter 。这是我的代码:

  - (void)setOAuthToken:(NSString *)token oauthVerifier:(NSString *)verifier
{
    __weak typeof (self)weakself = self;
    self.twitterAPI = [STTwitterAPI twitterAPIWithOAuthConsumerKey:Twitter_API_KEY consumerSecret:Twitter_API_SECRET];
    [self.twitterAPI postAccessTokenRequestWithPIN:verifier
                                      successBlock:^(NSString *oauthToken, NSString *oauthTokenSecret, NSString *userID, NSString *screenName) {
                                          [weakself.twitterAPI getUserInformationFor:screenName
                                                                        successBlock:^(NSDictionary *user) {
                                                                            ///we have an account
                                                                            NSLog(@"something");
                                                                        } errorBlock:^(NSError *error) {
                                                                            /// error
                                                                            NSLog(@"error:%@", error);
                                                                        }];

                                      } errorBlock:^(NSError *error) {
                                          ///error
                                          NSLog(@"Error:%@", error);
                                      }];
}

用户授权后,我可以获得tokenoauth_verifier,然后可以传入上面的函数。但是我得到了错误:

Error:Error Domain=STHTTPRequest Code=401 "HTTP Status 401: Unauthorized" UserInfo=0x7f84c1531420 {NSLocalizedDescription=HTTP Status 401: Unauthorized}

我也尝试过以下代码:

   self.twitterAPI = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:Twitter_API_KEY consumerSecret:Twitter_API_SECRET];
[self.twitterAPI verifyCredentialsWithSuccessBlock:^(NSString *username) {
    NSLog(@"");
    [self.twitterAPI getUserInformationFor:@"jac_ln" successBlock:^(NSDictionary *user) {
        NSLog(@"");
    } errorBlock:^(NSError *error) {
        NSLog(@"EROR:%@", error);
    }];
} errorBlock:^(NSError *error) {
    NSLog(@"ERROR:%@", error);
}];

但也无法从twitter获取用户信息。请帮助我。

2 个答案:

答案 0 :(得分:0)

[App_Delegate.twitter getUserInformationFor:[NS_Defaults objectForKey:kUserScreenName] successBlock:^(NSDictionary * user)      {

     [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

     NSLog(@"Uesr Info-- %@", user);
     NSDictionary  *dictUser = [[NSDictionary alloc]initWithObjects:@[[user objectForKey:@"name"],[user objectForKey:@"profile_image_url"]] forKeys:@[kUserName,kUserProfileImage]];


     [NS_Defaults setObject:dictUser forKey:kUserInfo];

     [NS_Defaults synchronize];

     UIStoryboard *objStory = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
     App_Delegate.mainVC =[objStory instantiateViewControllerWithIdentifier:@"MainVC"];
     [self.navigationController pushViewController:App_Delegate.mainVC animated:YES];
     [NS_Defaults setBool:YES forKey:@"isLogin"];
     [NS_Defaults synchronize];


 } errorBlock:^(NSError *error)
 {
     NSLog(@"Error-- %@", error);
 }];

答案 1 :(得分:0)

您需要设置一个回调网址,甚至是一个简单的字符串。