gpt4 book ai didi

javascript - Cordova:使用 javascript api 登录 facebook 不工作

转载 作者:行者123 更新时间:2023-11-29 21:57:13 25 4
gpt4 key购买 nike

我在我的应用程序中使用带有 facebook api 的 JavaScript 登录。它在网络应用程序中运行良好。但是当我为 android 构建应用程序然后单击按钮时,它会显示用于添加用于登录的 facebook 凭据的窗口,但在提交凭据时会显示白屏和重定向功能不起作用。我是 cordova 应用程序的新手。所以有人可以告诉我在 cordova 应用程序中使用 JavaScript 登录 facebook 是否正确,或者还有其他方法。下面是我的代码

HTML部分

 <div id="fb-root"></div>
<div id="user-info"></div>
<a href="#" class="f_icon" id='fb-auth'>Sign in with Facebook</a>

Facebook 的js

  window.fbAsyncInit = function() {
FB.init({
appId: '33XXXXXXXXXX',
status: true,
cookie: true,
xfbml: true,
oauth: true
});

function updateButton(response) {
var button = document.getElementById('fb-auth');

if (response.authResponse) {

FB.api('/me', function(response) {

});
button.onclick = function() {
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {

var user_info = {
name: response.name,
email: response.email,
fb_id: response.id,
gender: response.gender
};

$.ajax({
url: 'ws/webservice.php?mode=facebook_login',
data: user_info,
type: "POST",
dataType: 'json',
success: function(json) {
if (json.status == "success") {
window.localStorage.setItem("session_id",json.session_id);

var session_id = window.localStorage.getItem("session_id");

window.location.href = "dashboard.html";

} else {

}
},
});
});
} else {
//user cancelled login or did not grant authorization
}
}, {
scope: 'email'
});
}
} else {
//user is not connected to your app or logged out
button.innerHTML = 'Sign in with Facebook';
button.onclick = function() {
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {

var user_info = {
name: response.name,
email: response.email,
link: response.link,
fb_id: response.id,
gender: response.gender
};
$.ajax({
url: 'webservice.php?mode=facebook_login',
data: user_info,
type: "POST",
dataType: 'json',
success: function(json) {
if (json.status == "success") {

window.localStorage.setItem("session_id", json.session_id);

var session_id = window.localStorage.getItem("session_id");
window.location.href = "dashboard.html";

} else {

}
},
});


});
} else {
//user cancelled login or did not grant authorization
}
}, {
scope: 'email'
});
}
}
}

// run once with current status and whenever the status changes
FB.getLoginStatus(updateButton);
// FB.Event.subscribe('auth.statusChange', updateButton);
};
(function() {
var e = document.createElement('script');
e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());

最佳答案

由于 cordova 不是网络应用程序,它没有域,因此它没有任何页面可以返回到“站点”中。要让 Facebook connect 在 cordova 上运行,您应该仔细阅读如何实现插件。

这是适用于 Android 和 iOS 的 Facebook Connect 插件: https://github.com/Wizcorp/phonegap-facebook-plugin

假设您使用 cordova 3.3.0 及更高版本。

关于javascript - Cordova:使用 javascript api 登录 facebook 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25842618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com