2012-08-22 68 views
3

目前玩弄GooglePlusSample與範圍:谷歌加iOS SDK:如何獲取登錄用戶的電子郵件?

@"https://www.googleapis.com/auth/plus.me", 
@"https://www.googleapis.com/auth/userinfo.email" and 
@"https://www.googleapis.com/auth/userinfo.profile". 

打過電話auth.userEmail,auth.userData在回調方法finishedWithAuth:error:,但都是空的......

+0

你解決了嗎?我遇到過同樣的情況... – medvedNick

+0

還沒有。該項目仍在開發中,此功能暫停。目前我們只顯示用戶名。 – Centurion

回答

0

這爲我工作:

首先使用userinfo.email範圍如下:

signInButton.scope = [NSArray arrayWithObjects: 
         kGTLAuthScopePlusMe, 
         kGTLAuthScopePlusUserinfoEmail, 
         nil]; 

然後d efine這些方法:

- (GTLServicePlus *)plusService { 
    static GTLServicePlus* service = nil; 
    if (!service) { 
    service = [[GTLServicePlus alloc] init]; 
    // Have the service object set tickets to retry temporary error conditions 
    // automatically 
    service.retryEnabled = YES; 
    // Have the service object set tickets to automatically fetch additional 
    // pages of feeds when the feed's maxResult value is less than the number 
    // of items in the feed 
    service.shouldFetchNextPages = YES; 
    } 
    return service; 
} 

- (void)fetchUserProfile { 
    // Make a batch for fetching both the user's profile and the activity feed 
    GTLQueryPlus *profileQuery = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"]; 
    profileQuery.fields = @"id,emails,image,name,displayName"; 
    profileQuery.completionBlock = ^(GTLServiceTicket *ticket, id object, NSError *error) { 
    if (error == nil) { 
     // Get the user profile 
     GTLPlusPerson *userProfile = object; 
     // Get what we want 
     NSArray * userEmails = userProfile.emails; 
     NSString * email = ((GTLPlusPersonEmailsItem *)[userEmails objectAtIndex:0]).value; 
     NSString * name = userProfile.displayName; 
     NSString * profileId = userProfile.identifier; 
    } else { 
     // Log the error 
     NSLog(@"Error : %@", [error localizedDescription]); 
    } 
    }; 

    GTLBatchQuery *batchQuery = [GTLBatchQuery batchQuery]; 
    [batchQuery addQuery:profileQuery]; 
    GTLServicePlus *service = self.plusService; 
    self.profileTicket = [service executeQuery:batchQuery 
          completionHandler:^(GTLServiceTicket *ticket, 
               id result, NSError *error) { 
          self.profileTicket = nil; 
          // Update profile 
          }]; 
} 

最後調用「fetchUserProfile」的方法,在「finishedWithAuth」按:

- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth 
        error: (NSError *) error 
{ 
    // An error? 
    if (error != nil) { 
    // Log 
    } else { 
    // Set auth into the app delegate 
    myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    appDelegate.auth = auth; 
    // Get user profile 
    self.plusService.authorizer = auth; 
    [self fetchUserProfile]; 
    } 
} 

注意,這可能不是完美的,因爲它是一個「正在進行的工作」,在特別是:獲取正確的電子郵件地址,當用戶有多個,但它是一個開始!

祝你好運。 Steve

+0

謝謝,但我已經嘗試過,但我在profileQuery完成塊中收到錯誤(請參閱下文)。我有三重檢查 - 在控制檯中啓用了Google+ API,客戶端代碼正確(儘管我可以登錄並使用現有值進行發佈)。這是一個錯誤:「錯誤域= com.google.GTLJSONRPCErrorDomain代碼= 403」操作無法完成。 (Access Not Configured)「UserInfo = 0x6af0150 {NSLocalizedFailureReason =(Access Not Configured),GTLStructuredError = GTLErrorObject 0x893d470:{message:」Access Not Configured「code:403 data:[1]},error = Access Not Configured}」 – Centurion

0

如果您在Google api控制檯中有Access未配置的錯誤檢查服務。確保你啓用谷歌加API服務。

4

一旦用戶通過身份驗證,您可以撥打[[GPPSignIn sharedInstance] userEmail]獲取經過身份驗證的用戶電子郵件。

+3

You需要設置'[GPPSignIn sharedInstance] .shouldFetchGoogleUserEmail = YES'來訪問該屬性,否則它會給你'nil' –

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

    NSLog(@"Received Error %@ and auth object==%@",error,auth); 

    if (error) { 
     // Do some error handling here. 
    } else { 
    [self refreshInterfaceBasedOnSignIn]; 

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

    // 1. Create a |GTLServicePlus| instance to send a request to Google+. 
    GTLServicePlus* plusService = [[GTLServicePlus alloc] init] ; 
    plusService.retryEnabled = YES; 

    // 2. Set a valid |GTMOAuth2Authentication| object as the authorizer. 
    [plusService setAuthorizer:[GPPSignIn sharedInstance].authentication]; 


    GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"]; 

    // *4. Use the "v1" version of the Google+ API.* 
    plusService.apiVersion = @"v1"; 

    [plusService executeQuery:query 
      completionHandler:^(GTLServiceTicket *ticket, 
           GTLPlusPerson *person, 
           NSError *error) { 
       if (error) { 



        //Handle Error 

       } else 
       { 


        NSLog(@"Email= %@",[GPPSignIn sharedInstance].authentication.userEmail); 
        NSLog(@"GoogleID=%@",person.identifier); 
        NSLog(@"User Name=%@",[person.name.givenName stringByAppendingFormat:@" %@",person.name.familyName]); 
        NSLog(@"Gender=%@",person.gender); 

       } 
      }]; 
    } 

} 
+0

基督謝謝你這樣做......不能爲我的生活弄清楚如何得到這個人的名稱*沒有*使用谷歌加。 – Mike

+0

謝謝,但我仍然有電子郵件null。還有其他的選擇嗎? –

相關問題