Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login with google plus in ios on parse.com

I've integrated login via Google+ on my iphone app, all works perfect, but I want to login in ios app with Parse.com ,

already used this method but didn't work

[PFUser becomeInBackground:@"session-token-here" block:^(PFUser *user, NSError *error) {
  if (error) {
    // The token could not be validated.
  } else {
    // The current user is now set to user.
  }
}]; 

when I send access token in this then got error.

 invalid session error. error code =101. 2.refreshToken

Please any one can help me for login with google plus on parse.com in ios

like image 477
HungryWE Avatar asked Jun 28 '14 08:06

HungryWE


2 Answers

In my last project, I did this and it's working fine .. If the user is already registered, then it will login; otherwise it initiates the signup process.

- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth  error: (NSError *) error {
    NSLog(@"Received error %@ and auth object %@",error, auth);
    NSLog(@"user email %@  user id %@  user data %@, ",auth.userEmail,auth.userID, auth.userData);

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

    NSString *userName = [[auth.userEmail componentsSeparatedByString:@"@"] objectAtIndex:0];
    PFUser *user = [PFUser user];
    user.username = userName;
    user.password = @"12345"; // It will use a common password for all google+ users.
    user.email = auth.userEmail;
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
             NSLog(@"Successful Registration");

            // Get the user's profile information 
             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"];

            [plusService executeQuery:query
                    completionHandler:^(GTLServiceTicket *ticket,
                                        GTLPlusPerson *person,
                                        NSError *error) {
                        if (error) {
                            GTMLoggerError(@"Error: %@", error);
                        } else {
                            // Retrieve the display name and "about me" text
                             NSLog(@"Name %@  Display name %@  Person about %@ person birthday %@"  ,person.name,person.displayName,person.aboutMe ,person.image);

                            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:person.image.url]];
                            PFFile *imageFile = [PFFile fileWithName:@"Profileimage.png" data:imageData];

                            PFUser *user = [PFUser currentUser];
                            // other fields can be set just like with PFObject
                            user[@"User_name"] = person.displayName;
                            user[@"user_image"] = imageFile;
                            [user saveInBackground];

                         }
                    }];

        } else {
             // If the user is already registered, then it'll login 
             NSLog(@"error %@",error);
            [PFUser logInWithUsernameInBackground:userName password:@"12345"
                                            block:^(PFUser *user, NSError *error) {
                                                NSLog(@"object id for me = %@", user.objectId);
                                                if (user) {
                                                     NSLog(@"user login success %@",user);
                                                }else{
                                                    NSLog(@"Error on Login %@",error);
                                                }
                                            }];
             // Show the errorString somewhere and let the user try again.
        }
    }];

 }

It's working fine. Thanks ..

like image 73
Jogendra.Com Avatar answered Nov 18 '22 22:11

Jogendra.Com


The becomeInBackground method requires a sessionToken you can get from a ParseCloud Application. After a while looking for an example of Parse+Google I created a Github project you can find here: https://github.com/danielCarlosCE/parse-googlelogin

I get an accessToken from google

let gToken = user.authentication.accessToken

Send it to my ParseCloud function

PFCloud.callFunctionInBackground("accessGoogleUser", withParameters: ["accessToken":gToken])

Get a sessionToken as response and use it in the become method

PFUser.becomeInBackground(sessionToken as! String)

The ParseCloud project you can find here https://github.com/danielCarlosCE/parsecloud-googlelogin

In the main.js, you need to change this var with your clientID information from Google

var clientsIds = ['iOSClientId','androidClientId'];
like image 34
Daniel Carlos Avatar answered Nov 18 '22 22:11

Daniel Carlos