简体   繁体   English

为什么facebook登录不能在android上使用phonegap?

[英]Why facebook login not working with phonegap on android?

UPDATE: 更新:

I almost got it working. 我差点搞定了。 I needed to change js.src assignment from js.src = "//connect.facebook.net/en_US/sdk.js"; 我需要从js.src = "//connect.facebook.net/en_US/sdk.js";更改js.src赋值js.src = "//connect.facebook.net/en_US/sdk.js"; to: js.src="https://connect.facebook.net/en_US/all.js"; to: js.src="https://connect.facebook.net/en_US/all.js";

But now when I enter the login screen I have this error: 但现在当我进入登录界面时出现此错误:

Can't Load URL: The domain of this URL isn't included in the app's domains. 无法加载网址:此网址的域名未包含在应用的域中。 To be able to load this url, add all domains and subdomains of your app to the App Domains field in your app settings. 要加载此网址,请将应用的所有域和子域添加到应用设置中的应用域名字段中。

I did just that but the same error is given. 我做到了这一点,但给出了同样的错误。 Any ideas? 有任何想法吗?

I have an app where I made a facebook login using JavaScript SDK. 我有一个应用程序,我使用JavaScript SDK进行Facebook登录。 The code works great on mobile browser and on desktop or laptop browsers, but when I install the app using phonegap I have the next error: 该代码在移动浏览器和台式机或笔记本电脑浏览器上运行良好,但当我使用phonegap安装应用程序时,我有下一个错误:

Uncaught ReferenceError: FB is not defined

Can anyone please help me with this? 有人可以帮我这个吗?

I've read multiple threads on stack about this, but none of them seemed to make some light. 我已经在堆栈上阅读了多个关于此的线程,但是它们似乎都没有发现。

My code is this: 我的代码是这样的:

$(document).ready(function() {
  (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {
      return;
    }
    js = d.createElement(s);
    js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));

  window.fbAsyncInit = function() {
    FB.init({
      appId: 'app_id_secret',
      xfbml: true,
      version: 'v2.7'
    });
  };
  // This is called with the results from from FB.getLoginStatus().
  function statusChangeCallback(response) {
    if (response.status === 'connected') {
      // Logged into your app and Facebook.
      getUserData();
    } else if (response.status === 'not_authorized') {
      // The person is logged into Facebook, but not your app.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into this app.';
    } else {
      // The person is not logged into Facebook, so we're not sure if
      // they are logged into this app or not.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into Facebook.';
    }
  }

  function checkLoginState() {
    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });
  }

  $('.fb_login').on('click', function(e) {
    e.preventDefault();
    FB.login(function(response) {
      if (response.authResponse) {
        FB.api('/me', {
          fields: 'name, email, first_name, last_name'
        }, function(response) {});
      }
    }, {
      scope: 'email,public_profile',
      return_scopes: true
    });
  });
});

I've also found this link Can Facebook JS SDK work with Phonegap / Cordova? 我也发现这个链接Facebook JS SDK可以与Phonegap / Cordova一起使用吗? where someone asks almost the same question. 有人问几乎同样的问题。 There is suggested to use an cordova plugin. 建议使用cordova插件。 Is this a solution? 这是一个解决方案吗? If yes, which one of them is best? 如果是的话,哪一个最好?

There is a plugin called phonegap.facebook.inappbrowser , that I think it might help you. 有一个名为phonegap.facebook.inappbrowser的插件,我认为它可能对你有帮助。

You can find the plugin here . 你可以在这里找到插件。

First add the plugin script in your file head like this: 首先在文件头中添加插件脚本,如下所示:

<script type="text/javascript" src="js/phonegap.facebook.inappbrowser.js"></script>

Then call it using your own custom parameters for app id, redirect url and permissions: 然后使用您自己的app id,重定向网址和权限的自定义参数调用它:

// Settings
FacebookInAppBrowser.settings.appId = '123456789';
FacebookInAppBrowser.settings.redirectUrl = 'http://example.com';
FacebookInAppBrowser.settings.permissions = 'email';

// Login(accessToken will be stored trough localStorage in 'accessToken');
FacebookInAppBrowser.login({
    send: function() {
        console.log('login opened');
    },
    success: function(access_token) {
        console.log('done, access token: ' + access_token);
    },
    denied: function() {
        console.log('user denied');
    },
    timeout: function(){
        console.log('a timeout has occurred, probably a bad internet connection');
    },
    complete: function(access_token) {
        console.log('window closed');
        if(access_token) {
            console.log(access_token);
        } else {
            console.log('no access token');
        }
    },
    userInfo: function(userInfo) {
        if(userInfo) {
            console.log(JSON.stringify(userInfo));
        } else {
            console.log('no user info');
        }
    }
});

Also, I saw that you want to get the first_name and last_name of the logged in user. 另外,我看到你想要获取登录用户的first_name和last_name。 The plugin doesn't have those set by default after login. 该插件在登录后没有默认设置。 But you can modify a variable in the plugin file called get_url . 但是您可以修改名为get_url的插件文件中的变量。 Change it from this: 改变它:

var get_url  = "https://graph.facebook.com/me?fields=email,name,gender&access_token=" + window.localStorage.getItem('facebookAccessToken');

To this: 对此:

var get_url  = "https://graph.facebook.com/me?fields=email,first_name,last_name,name,gender&access_token=" + window.localStorage.getItem('facebookAccessToken');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM