如何在iOS中从Google +获取个人资料图片?

时间:2015-07-10 11:15:59

标签: ios objective-c google-api google-plus

目前在我的应用中,我正在从Google +中获取电子邮件,GoogleID,用户名,性别。

这是我在viewDidLoad中的代码:

GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.shouldFetchGooglePlusUser = YES;
signIn.clientID = kClientId;
signIn.scopes = [NSArray arrayWithObjects:
                 kGTLAuthScopePlusMe,
                 kGTLAuthScopePlusUserinfoEmail,
                 kGTLAuthScopePlusLogin ,
                 nil];

signIn.shouldFetchGoogleUserID = YES;
signIn.shouldFetchGoogleUserEmail = YES;
signIn.delegate = self;
[signIn trySilentAuthentication];

这是我的GPPSignInDelegate方法:

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


    if (error) 
    {

    } 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);


        GTLServicePlus* plusService = [[GTLServicePlus alloc] init] ;
        plusService.retryEnabled = YES;


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


        [plusService executeQuery:query
                completionHandler:^(GTLServiceTicket *ticket,
                                    GTLPlusPerson *person,
                                    NSError *error) {
                    if (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);
                    }
                }];
    }


  }

 -(void)refreshInterfaceBasedOnSignIn
{

 if ([[GPPSignIn sharedInstance] authentication])
  {

    self.signInButton.hidden = YES;

  } else 
  {
    self.signInButton.hidden = NO;

  }


 }

我的问题是如何从Google+获取个人资料图片网址? 我提到了Getting Google+ profile picture url with user_id,但我没有收到准确答案。

3 个答案:

答案 0 :(得分:3)

试试这个

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",person.image.url]];

这里我添加完整代码

GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.clientID=@GOOGLE_PLUS_Login_ID;  // here add your ID
signIn.shouldFetchGoogleUserEmail = YES;
signIn.scopes = @[ @"profile" ];

signIn.delegate = self;

if (![signIn trySilentAuthentication])    // if it is not authenticate it re authenticate once
    [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)
         {
 // this is for fetch profile image
 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",person.image.url]];
             NSLog(@"%@",url);
NSLog(@"Name:%@",person.displayName);
  }];

      }

 }
}

答案 1 :(得分:2)

这个怎么样?

    let user: GIDGoogleUser = ...
    if user.profile.hasImage{
        let profilePicURL = user.profile.imageURLWithDimension(200).absoluteString
        print(profilePicURL)

    }

请参阅GIDProfileData课程参考。

答案 2 :(得分:1)

Swift 3

let dimension = round(thumbSize.width * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)

thumbSize.width是图像的必需宽度。

let dimension = round(100 * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)
相关问题