10

iPhoneアプリからgoogle plusにログインできました。しかし、ログインしたユーザーの詳細を取得する方法は? プロフィールID、メールアドレスなど

同様の質問に対するStackoverflowの回答を試しましたが、 うまくいきませんでした。そのサンプルでは、​​ここで accessTocken に正確に何が渡されているか、

NSString *str =  [NSString stringWithFormat:@"https://www.googleapis.com/oauth2/v1/userinfo?access_token=%@",accessTocken];

そのコードを- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth error: (NSError *) error { }メソッドに実装しました。ただしauth.accessToken、null 値を返します。

そのため、auth.accessToken を使用してその URL を追加することはできません。これを行う他の方法はありますか?

4

3 に答える 3

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

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

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

        // 3. 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);
            }
        }];
    }
}

うまくいけば、これはあなたを助けるでしょう

于 2013-07-02T12:30:21.950 に答える
14

これは、現在ログインしているユーザーの電子メール ID を取得する最も簡単で簡単な方法です。最初に GPPSignIn クラスのインスタンス変数を作成します。

GPPSignIn *signIn;

次に、viewDidLoadで初期化します

- (void)viewDidLoad
 {
 [super viewDidLoad];

  static NSString * const kClientID = @"your client id";
  signIn = [GPPSignIn sharedInstance];
  signIn.clientID= kClientID;
  signIn.scopes= [NSArray arrayWithObjects:kGTLAuthScopePlusLogin, nil];
  signIn.shouldFetchGoogleUserID=YES;
  signIn.shouldFetchGoogleUserEmail=YES;
  signIn.delegate=self;

 }

次に、View Controller に を実装GPPSignInDelegateします。ここで、ログインしているユーザーの電子メール ID を取得できます。

 - (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
           error:(NSError *)error
 {  
  NSLog(@"Received Access Token:%@",auth);
  NSLog(@"user google user id  %@",signIn.userEmail); //logged in user's email id
 }
于 2013-04-12T05:23:48.023 に答える