在Google +集成中无法获得用户名?

时间:2015-10-30 05:08:34

标签: ios objective-c google-plus

这是我的代码

   - (void)viewDidLoad {
        [super viewDidLoad];

        signIn = [GPPSignIn sharedInstance];
        signIn.shouldFetchGooglePlusUser = YES;
        signIn.shouldFetchGoogleUserEmail = YES;
        //signIn.shouldFetchGoogleUserEmail = YES;  // Uncomment to get the user's email

        // You previously set kClientId in the "Initialize the Google+ client" step
        signIn.clientID = kClientId;
        signIn.homeServerClientID = kClientId;
        // Uncomment one of these two statements for the scope you chose in the previous step
    //    signIn.scopes = @[ kGTLAuthScopePlusLogin ];  // "https://www.googleapis.com/auth/plus.login" scope
    //   signIn.scopes = @[ @"profile" ];            // "profile" scope
        signIn.scopes=[NSArray arrayWithObjects:kGTLAuthScopePlusLogin,kGTLAuthScopePlusMe, nil];
        signIn.shouldFetchGoogleUserEmail=YES;
        // Optional: declare signIn.actions, see "app activities"
        signIn.delegate = self;

        [signIn trySilentAuthentication];
    }
    - (void)signOut {
        [[GPPSignIn sharedInstance] signOut];
    }
    - (void)finishedWithAuth: (GTMOAuth2Authentication *)auth
                       error: (NSError *) error {
        NSLog(@"Received error %@ and auth object %@",error, auth);
        if(error)
        {

        }
        else
        {
            NSString *serverCode = [GPPSignIn sharedInstance].homeServerAuthorizationCode;
            NSLog(@"this is server code%@",serverCode);
            NSLog(@"%@", signIn.authentication.userEmail);
            GTLServicePlus* plusService = [[GTLServicePlus alloc] init];
            plusService.retryEnabled = YES;
            [plusService setAuthorizer:[GPPSignIn sharedInstance].authentication];
    //        [plusService setAuthorizer:signIn.authentication];
            GTLQueryPlus *query =
            [GTLQueryPlus queryForPeopleListWithUserId:@"me"
                                            collection:kGTLPlusCollectionVisible];
            [plusService executeQuery:query
                    completionHandler:^(GTLServiceTicket *ticket,
                                        GTLPlusPerson *peopleFeed,
                                        NSError *error) {
                        if (error) {
                            GTMLoggerError(@"Error: %@", error);
                        } else {
                            // Get an array of people from GTLPlusPeopleFeed
    //                        NSArray* peopleList = peopleFeed.image;
    //                        NSLog(@"peoplelist%@",peopleFeed.image);
                            NSLog(@"peoplelist%@",peopleFeed);
                            NSLog(@"name=%@",peopleFeed.name);//crashed here



                        }
                    }];
        }
    }

我的代码输出:

2015-10-30 10:27:52.335 Gintegration[652:16537] this is server code4/0m16vNMNOQV3PbjetJiBeIubCwb3safO0V5F14Q5t3o
2015-10-30 10:27:52.335 Gintegration[652:16537] kishorekumar@gmail.com
2015-10-30 10:27:52.928 Gintegration[652:16537] peoplelistGTLPlusPeopleFeed 0x78e783c0: {title:"Google+ List of Visible People" etag:""MrhFVuKLF7zHXL6gE2l7cEdzuiA/EyyvLoAPddEjQ_gTun2BNflcftA"" nextPageToken:"CGQQjN3vuIsq" totalItems:108 kind:"plus#peopleFeed" items:[100]}
2015-10-30 10:27:52.928 Gintegration[652:16537] -[GTLPlusPeopleFeed name]: unrecognized selector sent to instance 0x78e783c0
2015-10-30 10:27:52.989 Gintegration[652:16537] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GTLPlusPeopleFeed name]: unrecognized selector sent to instance 0x78e783c0'
*** First throw call stack:
(
    0   CoreFoundation                      0x02b8aa94 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0264be02 objc_exception_throw + 50
    2   CoreFoundation                      0x02b93de3 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x02ad1e3d ___forwarding___ + 1037
    4   CoreFoundation                      0x02ad1a0e _CF_forwarding_prep_0 + 14
    5   Gintegration                        0x0009322b __42-[ViewControllerS finishedWithAuth:error:]_block_invoke + 283
    6   Gintegration                        0x0009e802 -[GTLService handleParsedObjectForFetcher:] + 1646
    7   libobjc.A.dylib                     0x02660059 -[NSObject performSelector:withObject:] + 70
    8   Foundation                          0x009216e8 __NSThreadPerformPerform + 323
    9   CoreFoundation                      0x02aa4e7f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    10  CoreFoundation                      0x02a9ab0b __CFRunLoopDoSources0 + 523
    11  CoreFoundation                      0x02a99f28 __CFRunLoopRun + 1032
    12  CoreFoundation                      0x02a99866 CFRunLoopRunSpecific + 470
    13  CoreFoundation                      0x02a9967b CFRunLoopRunInMode + 123
    14  GraphicsServices                    0x04380664 GSEventRunModal + 192
    15  GraphicsServices                    0x043804a1 GSEventRun + 104
    16  UIKit                               0x0122acc1 UIApplicationMain + 160
    17  Gintegration                        0x0009356a main + 138
    18  libdyld.dylib                       0x03c88a21 start + 1
    19  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

如果我的代码中有任何错误,请指出。

我正在收到电子邮件ID,但在尝试获取用户个人资料信息时,我收到了错误消息。 请有人帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

你有没有尝试过" item"属性?文档here提到每个项目包括该人的id,displayName,image和url。要检索其他配置文件数据,请参阅people.get方法。 @property(非原子,保留)NSArray *项目; // GTLPlusPerson

答案 1 :(得分:0)

  

{title:" Google+可见人列表" ETAG:"" MrhFVuKLF7zHXL6gE2l7cEdzuiA / EyyvLoAPddEjQ_gTun2BNflcftA"" nextPageToken这个:" CGQQjN3vuIsq" totalItems:108种:"加上#peopleFeed"项目:[100]}

上面的词典包含名称键不可用,这就是崩溃的原因。

试试这个

- (void)viewDidLoad {
    [super viewDidLoad];

      GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.clientID=@GOOGLE_PLUS_Login_ID;
signIn.shouldFetchGoogleUserEmail = YES;
signIn.scopes = @[ @"profile" ];

signIn.delegate = self;

if (![signIn trySilentAuthentication])
    [signIn authenticate];
}

-(void)finishedWithAuth: (GTMOAuth2Authentication *)auth
error: (NSError *) error {
 if (error) {
    // Do some error handling here.
} else {
//         NSLog(@"Received error %@", auth.userEmail);
//         NSLog(@"Received error %@", auth.accessToken);

    if ( auth.userEmail)
    {
        [[[GPPSignIn sharedInstance] plusService] executeQuery:[GTLQueryPlus queryForPeopleGetWithUserId:@"me"] completionHandler:^(GTLServiceTicket *ticket, GTLPlusPerson *person, NSError *error)
         {
 // NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",person.image.url]];
            // NSLog(@"%@",url);
            //  NSLog(@"Name:%@",person.displayName);
//                 NSLog(@"birthday:%@",person.birthday);
//
//                 NSLog(@"gender:%@",person.gender);
  }];


                }

 }
 }