1

PFUser *aUser = [PFUser 当前用户]; //可能不是当前用户 NSLog("username %@", [aUser username]); //如果不是FB用户也可以。否则它给了我一个令牌。我知道有一个 [PFUser isLinkedWithUser:aUser] 返回的是 CURRENT LOGGED USER 已链接,但 aUser 可能不是此设备中的当前用户。

这适用于当前用户:

+ (void)getUsernameWithCompletionBlock:(void(^)(NSString *username))handler {
if([PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]){ //<- 
    FBRequest *request = [FBRequest requestForMe];
    [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    if (!error) {
        NSDictionary *userData = (NSDictionary *)result;
        NSString *name = userData[@"name"];
        handler(name);
    }}];
} else {
    handler([[PFUser currentUser] username]);
}

}

但我想要这样的东西

+(void)getUsernameWithUser:(PFUser *)user andCompletionBlock:(void(^)(NSString     *username))handler {
if([PFFacebookUtils isLinkedWithUser:user]){ //<- THIS ALWAYS RETURN FALSE
   //return the real facebook username
   } else {
         handler([user username]);
     }
 }

在文档中说:isLinkedWithUser:

用户是否将其帐户链接到 Facebook。

(BOOL)isLinkedWithUser:(PFUser *)user 参数 user 用于检查 facebook 链接的用户。用户必须在此设备上登录。有任何想法吗?

4

1 回答 1

4

When you have users who aren't the current user, the authData isn't sent. You have two options:

  1. Each user can store their Facebook username (and any other data you want publicly accessible) in the User table.
  2. Create a cloud code function which uses the master key to retrieve the authData and queries for the Facebook username.

For option two, your cloud code should do something similar to the following:

Parse.Cloud.define("facebookAlias", function(request, response) {
  Parse.Cloud.useMasterKey();
  new Parse.Query(Parse.User).get(request.params.user_id).then(function(user) {
    var authData = user.get("authData");

    // Quit early for users who aren't linked with Facebook
    if (authData === undefined || authData.facebook === undefined) {
      response.success(null);
       return;
    }

    return Parse.Cloud.httpRequest({
      method: "GET",
      url: "https://graph.facebook.com/me",
      params: {
        access_token: authData.facebook.access_token,
        fields: "username",
      }
    });

  }).then(function(json) {
     response.success(JSON.parse(json)["username"]);

  // Promises will let you bubble up any error, similar to a catch statement
  }, function(error) {
    response.error(error);
  });
});

Disclaimer: the above code is roughly from a personal project but enough has been tweaked that I don't warrant against syntax errors.

于 2013-05-08T15:58:19.573 回答