Login with google plus in ios on parse.com

后端 未结 3 2171
遇见更好的自我
遇见更好的自我 2021-02-04 20:21

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

相关标签:
3条回答
  • 2021-02-04 21:11

    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'];
    
    0 讨论(0)
  • 2021-02-04 21:15

    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 ..

    0 讨论(0)
  • 2021-02-04 21:26

    First parameter (session token) that you pass to +becomeInBackground:block: is Parse session token, not Google+ / Facebook / Twitter.

    This method is intended to be used with your own custom flow, where you get the session token differently than by providing username/password, e.g. via CloudCode + SMS auth.

    You can implement Google+ sign in using something like this:

    [PFUser logInWithUsernameInBackground:email
                                 password:@"yourSecretCommonPassword"
                                    block:
     ^(PFUser *user, NSError *error)
     {
         if ([error code] == kPFErrorObjectNotFound) {
             PFUser *currentUser = [PFUser currentUser];
             currentUser.username = email;
             currentUser.password = @"yourSecretCommonPassword";
             currentUser.email = email;
             currentUser[@"googleAuthToken"] = accessToken;
             currentUser[@"googleRefreshToken"] = refreshToken;
             [currentUser signUpInBackground];
         }
     }];
    
    0 讨论(0)
提交回复
热议问题