到处寻找这个但没有运气。在设备上运行 PhoneGap 应用程序时出现此错误(在浏览器中工作正常):
Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains.
这是因为我没有将主机添加到 facebook 应用程序配置中。但是,PhoneGap/Cordova 通过 file:///协议(protocol)访问该应用程序,因此我没有可以添加到 Facebook 的域。
潜在的选择:1)弄清楚如何使用cordova native 插件(这很难,因为我们使用的是Parse),2)切换Cordova使用localhost而不是file://(不知道如何做到这一点) 。
最佳答案
我一直沿着这条路走下去,最终选择了选项#1。由于我们并不是真正处理网站,因此无需添加域名。 Cordova 需要使用 file://我认为没有任何办法可以解决它。使用该插件的技巧是保持登录状态与 Parse 同步(使用 Parse.FacebookUtils.logIn)。他的一些代码应该可以帮助你。这是我检查登录状态的方法:
try {
console.log("Trying to get FB login status");
return FB.getLoginStatus(function(response) {
var accessToken, currentView, expDate, facebookAuthData, uid, user;
console.log(response);
if (response.status === "connected") {
uid = response.authResponse.userID;
accessToken = response.authResponse.accessToken;
console.log("Logged in!");
user = Parse.User.current();
if (user != null) {
console.log("we have a user");
} else {
console.log("we don't have a user... need to login with parse");
expDate = new Date(response.authResponse.expirationTime);
facebookAuthData = {
id: response.authResponse.userID + "",
access_token: response.authResponse.accessToken,
expiration_date: expDate.toISOString()
};
Parse.FacebookUtils.logIn(facebookAuthData, {
success: function(_user) {
return console.log("Logged in with Parse!");
},
error: function(error1, error2) {
return console.log("Unable to create/login as Facebook user");
}
});
}
} else {
// not logged in to fb...
}
});
} catch (e) {
return console.log(e);
}
这是我处理登录的方式:
return FB.Event.subscribe("auth.authResponseChange", function(response) {
var expDate, facebookAuthData;
if (response.status === "connected") {
console.log(response.status);
try {
expDate = new Date(response.authResponse.expirationTime);
facebookAuthData = {
id: response.authResponse.userID + "",
access_token: response.authResponse.accessToken,
expiration_date: expDate.toISOString()
};
return Parse.FacebookUtils.logIn(facebookAuthData, {
success: function(_user) {
//return window.location.hash = "loginsuccess";
},
error: function(error1, error2) {
console.log("Unable to create/login to as Facebook user");
}
});
} catch (ex) {
return console.log("parse login error " + ex);
}
} else if (response.status === "not_authorized") {
// handle not auth event
} else {
// take them home?
}
});
}
关于javascript - PhoneGap/Cordova 上的 Facebook——文件协议(protocol)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23743480/