javascript - Facebook 登录 - 混合应用

标签 javascript css facebook html facebook-login

更新:

最后,我最终通过 Eclipse for android 和 xcode for iOS 使用 apache cordova/phonegap 来实现。这是唯一适用于我的首选设置的解决方案。

下载插件和文档的链接:https://github.com/phonegap/phonegap-facebook-plugin

上一篇:

我想在我的混合应用程序中实现 facebook 登录。我已经对 facebook 文档做了一些研究,但我还没有找到任何有用的东西。如果你能给我一些提示,那将非常有帮助。如果有人能告诉我该怎么做,我将悬赏。

我不想通过 Phonegap/cordova 和其他框架,因为我需要很多时间来研究这些框架。

混合应用程序 - 与 native 应用程序一样,在设备上运行,并使用网络技术(HTML5、CSS 和 JavaScript)编写。混合应用程序在 native 容器内运行,并利用设备的浏览器引擎(而非浏览器)在本地呈现 HTML 和处理 JavaScript。 Web-to-native 抽象层支持访问在移动 Web 应用程序中无法访问的设备功能,例如加速度计、相机和本地存储。

如果有人有任何解决方案并愿意提供帮助,请告诉我。

我尝试过 Javascript SDK,但没有成功。

代码:

<div id="fb-root"></div>
<script>
 window.fbAsyncInit = function() {
  FB.init({
    appId      : 'xxxx', // App ID
    channelUrl : '//xxxx/channel.html', // Channel File
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true  // parse XFBML
  });

  // Here we subscribe to the auth.authResponseChange JavaScript event. This event is fired
  // for any authentication related change, such as login, logout or session refresh. This means that
  // whenever someone who was previously logged out tries to log in again, the correct case below 
  // will be handled. 
  FB.Event.subscribe('auth.authResponseChange', function(response) {
    // Here we specify what we do with the response anytime this event occurs. 
    if (response.status === 'connected') {
      // The response object is returned with a status field that lets the app know the     current
      // login status of the person. In this case, we're handling the situation where they 
      // have logged in to the app.     
      testAPI();
    } else if (response.status === 'not_authorized') {
      // In this case, the person is logged into Facebook, but not into the app, so we call
      // FB.login() to prompt them to do so. 
      // In real-life usage, you wouldn't want to immediately prompt someone to login 
      // like this, for two reasons:
      // (1) JavaScript created popup windows are blocked by most browsers unless they 
      // result from direct interaction from people using the app (such as a mouse click)
      // (2) it is a bad experience to be continually prompted to login upon page load.
      FB.login();
    } else {
      // In this case, the person is not logged into Facebook, so we call the login() 
      // function to prompt them to do so. Note that at this stage there is no indication
      // of whether they are logged into the app. If they aren't then they'll see the Login
      // dialog right after they log in to Facebook. 
      // The same caveats as above apply to the FB.login() call here.
      FB.login();
    }
  });
  };

  // Load the SDK asynchronously
  (function(d){
   var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   /* js.src = "//connect.facebook.net/en_US/all.js"; */
   js.src="https://connect.facebook.net/en_US/all.js";
   ref.parentNode.insertBefore(js, ref);
  }(document));

  // Here we run a very simple test of the Graph API after login is successful. 
  // This testAPI() function is only called in those cases. 
  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Good to see you, ' + response.name + '.');
    });
  }
</script>

<!--
  Below we include the Login Button social plugin. This button uses the JavaScript SDK to
  present a graphical Login button that triggers the FB.login() function when clicked.

  Learn more about options for the login button plugin:
  /docs/reference/plugins/login/ -->

<!-- <fb:login-button show-faces="true" width="200" max-rows="1"></fb:login-button> -->

<!-- End script of Facebook Login -->

最佳答案

Facebook app details 您目前是在本地计算机上测试它还是在服务器上托管 HTML? 如果在服务器上 - 您的域是什么?

  1. 在应用域上更新它(见图片)
  2. 在“使用 Facebook 登录的网站”部分更新它
  3. 为了测试问题,从 testAPI() 中删除 FB.api 调用。只是发出警报。
  4. 从标准浏览器对其进行测试。如果有效 - 您的 FB 定义没有任何问题。

关于javascript - Facebook 登录 - 混合应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19128502/

相关文章:

facebook - 从 Flickr、Facebook、Instagram 和 iPhoto 中查找丢失的照片

javascript - Promise 如何在 Node.js 和 AWS lambda 函数中工作

javascript - 在 PEG.js 上为 STRING.STRING.STRING 制作语法表达式

javascript - 如何在 Cypress 快照测试中处理时区?

javascript - After Effects/ExtendScript Function.caller 等效项

html - 在页面容器外放置一个 div

css - z-index - 固定的、绝对的、相对的

javascript - 咕噜声 : Adding an alias to a non-project folder

python - Facebook 营销 API 设置 action_attribution_windows 无 View

ios - 如何使用 IOS 的 Facebook Graph API