Twitter屏幕名称在Parse中返回null

时间:2015-04-11 17:47:21

标签: ios objective-c twitter parse-platform

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

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];

        }

3 个答案:

答案 0 :(得分: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
    }
}];

答案 1 :(得分:0)

尝试在Twitter门户中进行配置。它将为您提供应用程序具有哪些权限的选项(这意味着您可能必须在Twitter的仪表板中手动返回用户名)。

答案 2 :(得分: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];
         }
     }];
}