2015-01-12 4 views
-1

안녕하세요, Google 플러스에 성공적으로 로그인했습니다. 이제 이메일, 이미지, 이름 같은 친구들의 정보를 가져 오려고합니다. 그러나 오류가 발생했습니다.Google 플러스 - 친구를 가져 오는 동안 오류가 발생했습니다.

중 하나가 내가 실수 만들고있어 경우, 좀 도와 주 시겠어요 -이 코드를 시도

-

- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth 
       error: (NSError *) error { 

    self.plusService.authorizer = auth; 

    NSLog(@"%@",[NSString stringWithFormat:@"Email---> %@\n\n",[GPPSignIn sharedInstance].authentication.userEmail]); 
    NSLog(@"Received error %@ and auth object  ---> %@\n\n",error, auth); 

// 1. 만들기 | GTLServicePlus을 | 인스턴스를 사용하여 Google+에 요청을 보냅니다.

GTLServicePlus* plusService = [[GTLServicePlus alloc] init] ; 

    plusService.retryEnabled = YES; 

// 2. 유효한 | GTMOAuth2Authentication을 | 객체를 위임자로 사용합니다. .

[plusService setAuthorizer:[GPPSignIn sharedInstance].authentication]; 

// 3.을의 Google+ API의 "V1"버전 *

plusService.apiVersion = @"v1"; 
    GTLQueryPlus *query = [GTLQueryPlus queryForPeopleListWithUserId:@"me" collection:kGTLPlusCollectionVisible]; 

    [plusService executeQuery:query 
      completionHandler:^(GTLServiceTicket *ticket, 
           GTLPlusPeopleFeed *person, 
           NSError *error) { 
     if (error) 
      { 
       GTMLoggerError(@"Error: %@", error); 
      } 
      else { 

       NSArray *peopleList = person.items; 
       NSLog(@"--People_List--->%@",peopleList); 

      }}]; 

    } 

하는 것은 오류 가져 오기 - 가져 오기 친구 자세한 내용은

[lvl=3] __41-[ViewController finishedWithAuth:error:]_block_invoke() Error: Error Domain=com.google.GTLJSONRPCErrorDomain Code=401 "The operation couldn’t be completed. (Invalid Credentials)" UserInfo=0x7b0a1d10 {error=Invalid Credentials, GTLStructuredError=GTLErrorObject 0x7b089360: {message:"Invalid Credentials" code:401 data:[1]}, NSLocalizedFailureReason=(Invalid Credentials)} 
+0

오류를 얻기 위해이 코드를 사용하여 그것을 자기 Google+의 일부 인증 오류가 있다는 말. – vivek

답변

0

을 당신은 사용할 수 있습니다 Google 주소록 api

그 사용

#import "GDataFeedContact.h" 
#import "GDataContacts.h" 

클래스 파일

는 데이터

-(void)getGoogleContacts 
    { 
     GDataServiceGoogleContact *service = [self contactService]; 
     GDataServiceTicket *ticket; 

     BOOL shouldShowDeleted = TRUE; 


     const int kBuncha = 2000; 

     NSURL *feedURL = [GDataServiceGoogleContact contactFeedURLForUserID:kGDataServiceDefaultUser]; 

     GDataQueryContact *query = [GDataQueryContact contactQueryWithFeedURL:feedURL]; 
     [query setShouldShowDeleted:shouldShowDeleted]; 
     [query setMaxResults:kBuncha]; 

     ticket = [service fetchFeedWithQuery:query 
            delegate:self 
          didFinishSelector:@selector(contactsFetchTicket:finishedWithFeed:error:)]; 

     [self setContactFetchTicket:ticket]; 
    } 

    - (void)setContactFetchTicket:(GDataServiceTicket *)ticket 
    { 
     mContactFetchTicket = ticket; 
    } 

    - (GDataServiceGoogleContact *)contactService 
    { 
     static GDataServiceGoogleContact* service = nil; 

     if (!service) { 
      service = [[GDataServiceGoogleContact alloc] init]; 

      [service setShouldCacheResponseData:YES]; 
      [service setServiceShouldFollowNextLinks:YES]; 
     } 


    //pass the useremail and password.here 

     NSString *username = @"[email protected]"; 
     NSString *password = @"yourpassword"; 


     [service setUserCredentialsWithUsername:username 
             password:password]; 

     return service; 
    } 


    - (void)contactsFetchTicket:(GDataServiceTicket *)ticket 
       finishedWithFeed:(GDataFeedContact *)feed 
          error:(NSError *)error { 

     if (error) { 
      NSDictionary *userInfo = [error userInfo]; 
      NSLog(@"Contacts Fetch error :%@", [userInfo objectForKey:@"Error"]); 
      if ([[userInfo objectForKey:@"Error"] isEqual:@"BadAuthentication"]) { 

       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!" 
                    message:@"Authentication Failed" 
                    delegate:self 
                  cancelButtonTitle:@"Ok" 
                  otherButtonTitles:nil, nil]; 
       [alertView show]; 
      } else { 
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!" 
                    message:@"Failed to get Contacts." 
                    delegate:self 
                  cancelButtonTitle:@"Ok" 
                  otherButtonTitles:nil, nil]; 
       [alertView show]; 
      } 
     } else { 

      NSArray *contacts = [feed entries]; 
      NSLog(@"Contacts Count: %d ", [contacts count]); 
      [googleContacts removeAllObjects]; 
      for (int i = 0; i < [contacts count]; i++) { 
       GDataEntryContact *contact = [contacts objectAtIndex:i]; 

       // Name 
       NSString *ContactName = [[[contact name] fullName] contentStringValue]; 
       NSLog(@"Name : %@", ContactName); 

       // Email 
       GDataEmail *email = [[contact emailAddresses] objectAtIndex:0]; 
       NSString *ContactEmail = @""; 
       if (email && [email address]) { 
        ContactEmail = [email address]; 
        NSLog(@"EmailID : %@", ContactEmail); 
       } 

       // Phone 
       GDataPhoneNumber *phone = [[contact phoneNumbers] objectAtIndex:0]; 
       NSString *ContactPhone = @""; 
       if (phone && [phone contentStringValue]) { 
        ContactPhone = [phone contentStringValue]; 
        NSLog(@"Phone : %@", ContactPhone); 
       } 

       // Address 
       GDataStructuredPostalAddress *postalAddress = [[contact structuredPostalAddresses] objectAtIndex:0]; 
       NSString *address = @""; 
       if (postalAddress) { 
        NSLog(@"formattedAddress : %@", [postalAddress formattedAddress]); 
        address = [postalAddress formattedAddress]; 
       } 

       // Birthday 
       NSString *dob = @""; 
       if ([contact birthday]) { 
        dob = [contact birthday]; 
        NSLog(@"dob : %@", dob); 
       } 

       if (!ContactName || !(ContactEmail || ContactPhone)) { 
        NSLog(@"Empty Contact Fields. Not Adding."); 
       } 
       else 
       { 
        if (!ContactEmail) { 
         ContactEmail = @""; 
        } 
        if (!ContactPhone) { 
         ContactPhone = @""; 
        } 
        NSArray *keys = [[NSArray alloc] initWithObjects:@"name", @"emailId", @"phoneNumber", @"address", @"dob", nil]; 
        NSArray *objs = [[NSArray alloc] initWithObjects:ContactName, ContactEmail, ContactPhone, address, dob, nil]; 
        NSDictionary *dict = [[NSDictionary alloc] initWithObjects:objs forKeys:keys]; 

        [googleContacts addObject:dict]; 
       } 
      } 
      NSSortDescriptor *descriptor = 
      [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]; 
      [googleContacts sortUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]]; 

     get the more info refer this http://dipinkrishna.com/blog/2013/07/ios-google-contacts/4/ 
관련 문제