0

有很多相同的问题,但我也找不到适合我的解决方案。我决定发布这个问题。我的用户通过 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);
                                      }];
}

用户授权后,我可以得到token然后oauth_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 获取用户信息。请帮助我。

4

2 回答 2

0

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

于 2016-04-22T14:46:50.840 回答
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);
 }];
于 2016-02-11T10:53:18.643 回答