如何通过FacebookUtil链接现有的Parse用户

时间:2016-12-12 03:58:11

标签: swift parse-platform parse-server pfuser pffacebookutils

我已经尝试了很多链接现有的解析用户到Facebook。

我正在使用swift 3。

但是我找不到解决方案

我的状态是

  1. 我的帐户没有关联Facebook。

  2. 我希望通过facebookUtils链接我的帐户

  3. 我的应用有“登录或注册Facebook”按钮

  4. 它的工作方式如下

  5. 我的问题是

    1. 当我点击Facebook登录按钮时,我没有登录。这意味着我没有PFUser.current()
    2. 这是我正在尝试做的事情

      1. 通过facebookGraph API获取电子邮件地址,并通过查询电子邮件检查是否使用

      2. 我曾尝试像这样登录

      3. 试试吧! PFUser.logIn(withUsername:userObj.username!,password:userObj.password!)

        但它无法正常工作,因为我无法获取密码。它总是返回零

        1. 我尝试过通过1个查询结果获得的链接PFUser对象

        2. 我也尝试过这样的云功能

        3. Parse.Cloud.define(“fblink”,function(request,response){

          Scanner in = null;
          int count = 0;
          try {
              in = new Scanner(file);
              while(in.hasNext())
              {
                  String line=in.nextLine();
                  if(line.contains(input)){
                      count++; 
                  }
              }
              if (count > 0){
                  // count contains the number of matches
                  popUp1 pu1 = new popUp1();
                  pu1.setVisible(true);            
              }
              else {
                  popUp2 pu2 = new popUp2();
                  pu2.setVisible(true);
              }
          } catch (FileNotFoundException e) {
              System.out.println("Error");
          }
          

          });

          但它不起作用......我认为云函数方法在这种情况下不是解决方案。

          我发现了很多与用户合并到Facebook authdata相关的问题。但我找不到任何解决方案。

          我希望能找到好的方法。

          var query = new Parse.Query(Parse.User);
          query.equalTo("username", request.params.username);
          
          query.first({
            success: function(object) {
          
                 if (!Parse.FacebookUtils.isLinked(object)) {
                        Parse.FacebookUtils.link(object, null, {
                          success: function(user) {
                            alert("Woohoo, user logged in with Facebook!");
                              response.success(true);   
                          },
                          error: function(user, error) {
                            alert("User cancelled the Facebook login or did not fully authorize.");
                              response.success(false);   
                          }
                        });
                      }
            },
            error: function(error) {
              console.log("Error: " + error.code + " " + error.message);
            }
          });
          

          以下是我在loadFacebookUserDetails中尝试过的内容。

          func fbBtnTapped() {
          
                  //Show Activity indicator
                  let spiningActivity = MBProgressHUD.showAdded(to: self.view, animated: true)
                  spiningActivity?.labelText = "Loading"
                  spiningActivity?.detailsLabelText = "Please Wait"
          
          
                  let permissions = ["public_profile", "email"]
          
          
                  PFFacebookUtils.logInInBackground(withReadPermissions: permissions, block: {(user:PFUser?, error:Error?) -> Void in
          
                      if let user = user {
                          if user.isNew {
                              print("User signed up and logged in through Facebook!")
          
                          } else {
                              print("User logged in through Facebook!")
                          }
                      } else {
                          print("Uh oh. The user cancelled the Facebook login.")
          
                      }
          
          
                      if(error != nil)
                      {
                          spiningActivity?.hide(true)
          
                          print("in FBBUTTONTapped Error")
                          // display an error message
                          let userMessage = error!.localizedDescription
                          let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.alert)
          
                          let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)
          
                          myAlert.addAction(okAction)
          
                          self.present(myAlert, animated: true, completion: nil)
                          return
                      }
          
                      // Load facebook details like userName, email address, profile picture.
                      self.loadFacebookUserDetails()
          
          
                  })
          
          
          
              }
          

1 个答案:

答案 0 :(得分:3)

如果他不是Facebook关联帐户,为了能够链接您需要PFUser.current()的帐户。您的选择是:

1 :)使用您的逻辑通过登录页面登录facebook。这意味着您在Users列中有了新用户。所以基本上可以用作parse登录,无论以后如何。您可以使用电子邮件或Facebook名称更新用户名(因为默认情况下,Facebook登录将使用ID填充用户名)。一旦他到达成功页面,如果他以后选择不使用他的Facebook帐户,您可以要求他填写密码。然后,欢迎他取消链接设置页面中的帐户与取消链接按钮。

@IBAction func unlinkFacebook(_ sender: AnyObject) {
    let user = PFUser.current()
    showHUD(message: "unlinking")
    PFFacebookUtils.unlinkUser(inBackground: user!, block:{
        (succeeded: Bool?, error) -> Void in
        if succeeded! {
            self.simpleAlert(mess: "Your account is no longer associated with your Facebook account.")
            self.hideHUD()

        }else{
            self.simpleAlert(mess: "\(error!.localizedDescription)")
            self.hideHUD()
        }
    })
}

所有取消链接都会清除authData列。

2 :)以解析用户身份登录,成功登录后请求将其帐户链接到Facebook。一个func可能像:

func linkFacebook() {
   let user = PFUser.current()
    showHUD(message: "linking")
    if !PFFacebookUtils.isLinked(with: user!) {
        PFFacebookUtils.linkUser(inBackground:  user!, withReadPermissions: nil, block:{
            (succeeded: Bool?, error) -> Void in
            if succeeded! {
             self.simpleAlert(mess: "Success. You can now log in using your facebook account in future.")
             self.hideHUD()
            }else{
             self.simpleAlert(mess: "\(error!.localizedDescription)")
             self.hideHUD()
            }
        })
    }
}

可以将其放在您的viewdidLoad或一个很好的按钮中,如果用户选择想要关联他的帐户,则可以选择该按钮。请记住,如果要存储Facebook详细信息,则需要调用Facebook sdk来更新用户详细信息。

只有Users table中填充的列为authData