如何在登录facebook后获取profilepic路径

时间:2018-04-19 02:49:14

标签: javascript facebook facebook-sdk-4.0 facebook-sdk-3.0

这里我正在用facebook登录,而且它工作正常,现在我的问题是登录后我需要个人资料图片路径,我不知道如何获取路径,如果有的话一个人知道请更新你的答案。现在我得到像 id,姓名,电子邮件这样的值,但我不是profilepic路径

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

      };
      (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);
      }());

    function fetchUserDetail()
    {
      FB.api('/me?fields=id,name,email', function(responseFromFB){ 
                //console.log("Name: "+ response.name + "\Email: "+ response.email + "ID: "+response.id);
				console.log(JSON.stringify(responseFromFB)); 
				 var userName = responseFromFB.name;
				var userEmail = responseFromFB.email;
				var profilePic = '1.png';
				var registerFrom = 'Web';
				var FCM_Token = '';
				  $.ajax({
					url:'admin/rest/registerFB',
					type:'POST',
					data: {userName: userName, userEmail: userEmail, profilePic: profilePic, registerFrom: registerFrom, FCM_Token: FCM_Token},
					success:function(loginResponse){
						  if(loginResponse['status']=='success'){
							 window.location.href = "index.php";
						  } 
							
					},
				}); 
            });
			
			
    }

    function checkFacebookLogin() 
    {
        FB.getLoginStatus(function(response) {
          if (response.status === 'connected') {
            fetchUserDetail();
          } 
          else 
          {
            initiateFBLogin();
          }
         });
    }

    function initiateFBLogin()
    {

    	
			   FB.login(function(response) {
				fetchUserDetail();
			}, {scope: 'email'});
    }
<html>
<head>
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">
</head>
<body>
  <input type="button" class="btn btn-lg btn-fb" value="Sign in using Facebook" scope="public_profile,email" onclick="checkFacebookLogin();"/>
<div id="fb-root"></div>

1 个答案:

答案 0 :(得分:0)

当您收到FB的回复时,您将获得已注册应用程序的id用户。您可以使用此ID并获取用户的图像。 下面的代码动态创建一个图像标记并添加到正文中。将此添加到您的fetchUserDetail()响应处理程序。

let image = document.createElement("img");
image.src = `https://graph.facebook.com/v2.12/${responseFromFB.id}/picture`;
document.body.appendChild(image);

API文档here

相关问题