2013-03-07 2 views
3

저는 페이스 북 API로 처음으로 작업하고 있습니다. 거기에서 우리는 아래에 이름을 불러올 수 있음을 발견했다. console.log (response.name); 하지만 전자 메일 및 fbid도 가져올 수 있습니까?그래프 API를 사용하여 페이스 북 로그인을 사용하여 사용자 이메일 및 ID를 얻는 방법

감사합니다, Enamul

<?php ?> 
<div id="fb-root"></div> 
<script> 
    // Additional JS functions here 
    window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : 'f', // App ID 
     channelUrl : '//localhost/practices/phpTest/fblogin/login.php/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 
    }); 
    FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 
     // connected 
    } else if (response.status === 'not_authorized') { 
     // not_authorized 
     login(); 
    } else { 
     // not_logged_in 
     login(); 
    } 
    }); 

    // Additional init code here 

    }; 

    function login() { 
    FB.login(function(response) { 
     if (response.authResponse) { 
      // connected 
      testAPI(); 
     } else { 
      // cancelled 
     } 
    }); 
    } 

    function testAPI() { 

    console.log('Welcome! Fetching your information.... '); 
    FB.api('/me', function(response) { 

     console.log('Good to see you, ' + response.name + '.'); 
    }); 
    } 

    // 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"; 
    ref.parentNode.insertBefore(js, ref); 
    }(document)); 
</script> 
<?php ?> 
+0

은 끝났습니까? 의견이 없으면 대답하고, 그렇다면 답을 받아들입니다. – ThePCWizard

+0

@ThePCWizard 늦게 받아 들여서 죄송합니다. 나는 그것을 시험 할 수 없기 때문에 얼마간 떨어져있었습니다. – user1559230

답변

4

당신이 이름을 액세스 같은 방법으로.

response.emailresponse.id을 즉하지만 이메일 주소를 가져 오기 위해 당신은 그것을 액세스 할 수있는 권한이 있어야합니다.

scope="email"을 FB 로그인 버튼에 추가하십시오.

[편집]

function login() { 
    FB.login(function(response) { 
     if (response.authResponse) { 
      // connected 
     testAPI(); 
     } else { 
      // cancelled 
     } 
    }, { scope: 'email' }); 
    } 

function testAPI() { 

    console.log('Welcome! Fetching your information.... '); 
    FB.api('/me', function(response) { 

     console.log('Good to see you, ' + response.name + '.' + ' Email: ' + response.email + ' Facebook ID: ' + response.id); 
    }); 
} 

여기에 초보자를위한 좋은 튜토리얼입니다 : 버튼에 범위 = "이메일"을 추가하는 것 외에도 http://www.loginworks.com/technical-blogs/404-working-with-facebook-javascript-sdk

+0

코드를 게시했습니다. 내 코드에서 어떤 변경을해야하는지 알려주세요. – user1559230

+1

업데이트 된 답변을 확인하십시오. – ThePCWizard

+1

로그인 예 : , 응답에는 이름과 아이디 만 포함되며 이메일은 포함되지 않습니다. . 뭐가 잘못 되었 니? – Dave

4

, 당신은 반환하는 필드를 지정할 필요가. 전체 코드 구조에 대한

function testAPI() { 
    FB.api('/me', {fields: 'name, email'}, function(response) { 
     console.log(response); 
     console.log(response.email); 
    }); 
} 

<fb:login-button scope="public_profile, email" onlogin="checkLoginState();" data-auto-logout-link="true" data-size="large"> 

facebook documentation를보십시오.

관련 문제