參考資源
測試中出現通訊失敗的情況時,請把擋廣告外掛停止,不過擋的其實就是廣告,不會影響登入功能
--
取不到 Email
1 2 3 |
應用程式不需要提交 Facebook 審查,即可向用戶要求以下兩項權限: public profile email |
所以 Email 並不需要另外申請權限,將 FB.api('/me') 修改就好了
1 2 3 |
FB.api('/me?fields=email,name', function(response) { console.log(response); }); |
--
完整程式範例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<!DOCTYPE html> <html lang="zh-tw"> <head> <title>臉書 JavaScript SDK 登入 by Hoyo</title> <meta charset="UTF-8"> </head> <body> <script> function statusChangeCallback(response) { // Called with the results from FB.getLoginStatus(). console.log(response); // The current login status of the person. if (response.status === 'connected') { // Logged into your webpage and Facebook. testAPI(); } } function checkLoginState() { // Called when a person is finished with the Login Button. FB.getLoginStatus(function(response) { // See the onlogin handler statusChangeCallback(response); }); } window.fbAsyncInit = function() { FB.init({ appId : '330758555207817', cookie : true, // Enable cookies to allow the server to access the session. xfbml : true, // Parse social plugins on this webpage. version : 'v11.0' // Use this Graph API version for this call. }); FB.getLoginStatus(function(response) { // Called after the JS SDK has been initialized. statusChangeCallback(response); // Returns the login status. }); }; function testAPI() { FB.api('/me?fields=email,name', function(response) { console.log(response); // document.getElementById('status').innerHTML = // 'Thanks for logging in, ' + response.name + '!'; }); } function fb_login(){ FB.getLoginStatus(function(response) { if (response.status === 'connected') { console.log(response); } else { FB.login(function(response) { }, {scope: 'public_profile,email'}); } }); } function fb_logout(){ FB.getLoginStatus(function(response) { if (response.status === 'connected') { FB.logout(); } }); } </script> <button type="button" onclick="fb_login()">登入</button> <button type="button" onclick="fb_logout()">登出</button> <!-- Load the JS SDK asynchronously --> <script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js"></script> </body> </html> |
--
499 total views, 1 views today