在parse.com的ios中使用google plus登录
我已经在我的iphone应用程序上集成了通过Google+登录的功能,一切都很完美,但我想用Parse.com登录ios应用程序 已使用此方法,但不起作用在parse.com的ios中使用google plus登录,ios,iphone,login,parse-platform,google-plus,Ios,Iphone,Login,Parse Platform,Google Plus,我已经在我的iphone应用程序上集成了通过Google+登录的功能,一切都很完美,但我想用Parse.com登录ios应用程序 已使用此方法,但不起作用 [PFUser becomeInBackground:@"session-token-here" block:^(PFUser *user, NSError *error) { if (error) { // The token could not be validated. } else { // The curre
[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.
}
}];
当我在这里发送访问令牌时,出现了一个错误
invalid session error. error code =101. 2.refreshToken
请任何人都可以帮助我在ios中的parse.com上使用google plus登录您传递给+becomeInBackground:block:
的第一个参数(会话令牌)是解析会话令牌,而不是google+/Facebook/Twitter
此方法旨在与您自己的自定义流一起使用,在自定义流中,您获取会话令牌的方式与提供用户名/密码的方式不同,例如通过CloudCode+SMS auth
您可以使用以下内容实现Google+登录:
[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];
}
}];
在我的上一个项目中,我做到了这一点,而且效果很好。。如果用户已经注册,则将登录;否则它将启动注册过程
- (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.
}
}];
}
很好用。谢谢 becomeInBackground方法需要可以从ParseCloud应用程序获得的sessionToken。在寻找Parse+Google的示例后,我创建了一个Github项目,您可以在这里找到: 我从谷歌得到一个accessToken
let gToken = user.authentication.accessToken
将其发送到我的ParseCloud函数
PFCloud.callFunctionInBackground("accessGoogleUser", withParameters: ["accessToken":gToken])
获取sessionToken作为响应,并在Been方法中使用它
PFUser.becomeInBackground(sessionToken as! String)
你可以在这里找到ParseCloud项目
在main.js中,您需要使用来自Google的clientID信息更改此变量
var clientsIds = ['iOSClientId','androidClientId'];
感谢您提供的解决方案,但从安全角度来看,使用静态密码让我很恼火。每个用户生成的不同哈希(基于用户名和其他一些特定于应用程序的常量)将提高安全性。由于parse.com SDK缺乏可扩展性,因此没有很好的解决方案。您甚至在登录时也在重置密码。如果我们希望用户也使用其密码登录,该怎么办?然后,他仍然可以使用他的用户名,密码登录以及谷歌登录。