Twitter的屏幕名称在解析中返回null

8

通过 Twitter 登录并尝试获取用户的屏幕名称。每次都会产生 null 值。有什么想法吗?

PFUser *currentUser = [PFUser currentUser];
    [PFTwitterUtils logInWithBlock:^(PFUser *user, NSError *error) {
        if (!user) {
            NSLog(@"Uh oh. The user cancelled the Twitter login.");
            return;
        } else if (user.isNew) {
            twitterScreenName = [PFTwitterUtils twitter].screenName;
            NSLog(@"%@",[PFTwitterUtils twitter].screenName);
            NSString * requestString = [NSString stringWithFormat:@"https://api.twitter.com/1.1/users/show.json?screen_name=%@", twitterScreenName ];

                                        NSURL *verify = [NSURL URLWithString:requestString];
                                        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:verify];
                                        [[PFTwitterUtils twitter] signRequest:request];

                                        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                NSError *error;
                NSDictionary* result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
                if (!error) {

                   user.username =twitterScreenName;
                   user[@"name"]= result[@"name"];
                    user[@"profileDescription"] = result[@"description"];
                  user[@"imageURL"] = [result[@"profile_image_url_https"] stringByReplacingOccurrencesOfString:@"_normal" withString:@"_bigger"];
                    [user saveEventually];
                }
            }];
          [self performSegueWithIdentifier: @"username" sender: self];

        }

我会在下面回答,但如果你正在使用logInWithBlock,你不应该使用第一行PFUser *currentUser = [PFUser currentUser],因为还没有任何用户登录,所以没有当前用户! - AlexKoren
@spogebob92 你解决了吗?我也遇到了同样的问题,但是想不出来怎么解决。 - Cyprian
3个回答

3
这是我如何做到的:
[PFTwitterUtils logInWithBlock:^(BOOL succeeded, NSError *error) {
    if ([PFTwitterUtils isLinkedWithUser:[PFUser currentUser]]) {
        NSURL *info = [NSURL URLWithString:@"https://api.twitter.com/1.1/account/settings.json"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:info];
        [[PFTwitterUtils twitter] signRequest:request];
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            if (!!data) {
                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
                NSString *userName = dict[@"screen_name"];
                userName = [userName stringByReplacingOccurrencesOfString:@"Twitter:" withString:@""];

                PFUser *user = [PFUser currentUser];
                user[@"Twitter"] = userName;
                [user saveEventually];
            } else {
              //uh oh, no twitter response
            }
        }];
    } else {
        //uh oh, failed login
    }
}];

使用该代码时,我得到了以下错误:不兼容的块指针类型,将“void (^)(BOOL, NSError *__strong)”发送到类型为“PFUserResultBlock”(又名“void (^)(PFUser *__strong, NSError *__strong)”)的参数。 - spogebob92
哦,糟糕,应该是[PFTwitterUtils logInWithBlock:^(PFUser *user, NSError *error) {}]; 这是我的错误。 - AlexKoren
有时候它能够获取数据,有时候却不能。这真的很奇怪。你有什么想法吗? - spogebob92
它什么时候会发生,什么时候不会发生?随机的吗? - AlexKoren
我需要登录两次才能获取屏幕名称。 - spogebob92
这个解决方案在我使用最新版本的Parse时仍然有效。你一定是设置有误了。 - AlexKoren

0
- (IBAction)TWloginPress:(id)sender {
    [activityview setHidden:NO];
    [activityview startAnimating];
    [[Twitter sharedInstance] logInWithCompletion:^
     (TWTRSession *session, NSError *error) {
         if (session) {
              [self signupUser:session.userName email:nil loggedinvia:@"TWITTER"];
         } else {
             NSLog(@"error: %@", [error localizedDescription]);
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"There was some problem with signing you with twitter. Please try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
         }
     }];
}

0

请尝试在 Twitter 门户网站上进行配置。它会为您提供应用程序具有哪些权限的选项(这意味着您可能需要手动启用从 Twitter 仪表板内返回用户名的功能)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接