PFUser * aUser = [PFUser currentUser]; //可能不是当前用户NSLog(“username%@”,[aUser username]); //如果它不是FB用户,这没关系。另外,它给了我一个代币。我知道有一个[PFUser isLinkedWithUser:aUser]返回的是CURRENT LOGGED USER被链接,但是用户可能不是该设备中的当前用户。
这适用于currentUser:
+ (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链接。用户必须登录此设备。 有什么想法吗?
答案 0 :(得分:4)
如果您的用户不是当前用户,则不会发送authData。您有两种选择:
对于选项二,您的云代码应执行与以下类似的操作:
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);
});
});
免责声明:上述代码大致来自个人项目,但已经进行了足够的调整,我不保证语法错误。