如何使用JavaScript API设置我的Facebook个人资料图片?

时间:2016-09-16 06:54:08

标签: javascript facebook facebook-graph-api

如何使用Facebook JavaScript SDK设置我的Facebook个人资料图片?或者我如何使用重定向到个人资料图片设置页面?

 <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId      : 'app-id',
          xfbml      : true,
          version    : 'v2.7'
        });

        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {
              document.getElementById('status').innerHTML = 'We are connected.';
              document.getElementById('login').style.visibility = 'hidden';
            } else if (response.status === 'not_authorized') {
              document.getElementById('status').innerHTML = 'We are not logged in.'
            } else {
              document.getElementById('status').innerHTML = 'You are not logged into Facebook.';
            }
          });
      };

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

      // login with facebook with extra permissions
      function login() {
        FB.login(function(response) {
          if (response.status === 'connected') {
              document.getElementById('status').innerHTML = 'We are connected.';
              document.getElementById('login').style.visibility = 'hidden';
            } else if (response.status === 'not_authorized') {
              document.getElementById('status').innerHTML = 'We are not logged in.'
            } else {
              document.getElementById('status').innerHTML = 'You are not logged into Facebook.';
            }
        }, {scope: 'email,publish_actions'});
      }

      // getting basic user info
      function getInfo() {
        FB.api('/me', 'GET', {fields: 'first_name,last_name,name,id,picture.width(300).height(300)'}, function(response) {
          document.getElementById('status').innerHTML = "<img src='" + response.picture.data.url + "'>";
          console.log(response)
        });
      }


      // uploading photo on user timeline
function post() {
  FB.api('/me/feed', 'post', {message: 'qqqqqqqqqqqqqq'}, function(response) {
    document.getElementById('status').innerHTML = response.id;
  });
}

function uploadPhoto() {
  // FB.api('/me/photos', 'post', {source: 'https://i.ytimg.com/vi/MJYTEOkmKiI/maxresdefault.jpg'}, function(response) {
  //   if (!response || response.error) {
  //     document.getElementById('status').innerHTML = "Error!";
  //   } else {
  //     document.getElementById('status').innerHTML = response.id;
  //   }
  // });
    var imgURL="https://i.ytimg.com/vi/MJYTEOkmKiI/maxresdefault.jpg";

  FB.api('/me/photos', 'post', {message:"hello world",url : imgURL }, function(response) {
    if (!response || response.error) {
      console.log(response.error);
      document.getElementById('status').innerHTML = "Error!";
    } else {
      document.getElementById('status').innerHTML = response.id;
    }
  });

} 

我使用了publish_action权限和post方法在Facebook上发布。

或者,我如何执行以下操作:通过Graph API获取上传的图像并重定向到图像的链接,将&amp; makeprofile = 1添加到查询字符串。现在,用户将被重定向到个人资料图像裁剪页面。

0 个答案:

没有答案