2013-11-25 1 views
8

enter image description hereFacebookは、プログラムで、私は偽のFacebookページ(エンターテイメントページ)を作成した大きな写真

でFacebookページに投稿します。 添付画像の左側には、手作業で最初の投稿( の写真が大きいもの)とプログラムによるもの(小さな写真のもの)がありました。

FB.api(
     'https://graph.facebook.com/[myAppId]/feed', 
     'post', 
     { 
      message: 'this is a grumpy cat', 
      description: "This cat has been lost for decades now, please call at 654321486", 
      picture: "http://laughingsquid.com/wp-content/uploads/grumpy-cat.jpg" 

     }, 
     function (response) { 
      if (!response) { 
       alert('Error occurred.'); 
      } else if (response.error) { 
       document.getElementById('result').innerHTML = 
        'Error: ' + response.error.message; 
      } else { 
       document.getElementById('result').innerHTML = 
        '<a href=\"https://www.facebook.com/' + response.id + '\">' + 
         'Story created. ID is ' + 
         response.id + '</a>'; 
      } 
     } 
    ); 

しかし、私はそれで満足していない:

私は小さな写真に使用されるコードは次のようになります 私が働いているアプリケーションが ので、失われた動物のリストを作ること大きな写真ではるかに大きいでしょう。

Facebookのデベロッパーページでどのように行うかの例はありませんでした。 これは可能だと思いますが、私はそれをまだ見つけていません。 あなたは既にこの問題を既に経験しましたか?

答えて

7

最後に、私はそれをしました! 解決策を投稿しています。正しい方向に私を向けるためのcdbconceptsのおかげです。 再びドキュメントを読んだ後: は「あなたはまた、写真のURLとURLのPARAMを提供することで、写真を公開することができます。」:

https://developers.facebook.com/docs/reference/api/photo/

彼らはと言います URLは、下の例に示すように、同じサーバー上にある必要はありません。 であり、js-sdkで動作します。だからここ

が私のために働く最終的なコードは次のとおりです。

<html> 
<head> 
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> 
</head> 
<body> 

<div id="fb-root"></div> 
<script> 

var appId = 'Replace with your appId'; 

window.fbAsyncInit = function() { 
    FB.init({ 
     appId: appId, 
     status: true, // check login status 
     cookie: true, // enable cookies to allow the server to access the session 
     xfbml: true // parse XFBML 
    }); 


    var options = { 
     scope: 'manage_pages, publish_stream' 
    }; 

    FB.Event.subscribe('auth.authResponseChange', function (response) { 
     if (response.status === 'connected') { 
      testAPI(); 
     } else if (response.status === 'not_authorized') { 
      FB.login(function() { 
      }, options); 
     } else { 
      FB.login(function() { 
      }, options); 
     } 
    }); 
}; 

// 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)); 

// 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 + '.'); 
    }); 
} 

function error(msg) { 
    document.getElementById('result').innerHTML = 'Error: ' + msg; 
} 

function postApi() { 

    var myPageID = '484364788345193'; 
    var targetPageName = 'Entertainment page of ling'; 
    var pathToImg = 'http://laughingsquid.com/wp-content/uploads/grumpy-cat.jpg'; 
    var accessToken = null; 

    FB.api(
     'https://graph.facebook.com/me/accounts', 
     function (response) { 
      if (!response || response.error) { 
       console.log(response); 
       error('Error occured'); 
      } else { 
       console.log(response); 
       for (var i in response.data) { 
        if (targetPageName === response.data[i].name) { 
         accessToken = response.data[i].access_token; 
        } 
       } 
       if (accessToken) { 
        FB.api(
         'https://graph.facebook.com/' + myPageID + '/photos', 
         'post', 
         { 
          url: pathToImg, 
          access_token: accessToken, 
          message: "Tadaam" 
         }, 
         function (response) { 
          if (!response || response.error) { 
           console.log(response); 
           error('Error occured'); 
          } else { 
           console.log(response); 
           alert("PostId: " + response.id); 
          } 
         } 
        ); 
       } 
       else { 
        error("Page not found in the accounts: " + targetPageName); 
       } 
      } 
     } 
    ); 

} 


function logout() { 
    FB.logout(); 
} 


$(document).ready(function() { 
    $("#logout").click(function() { 
     logout(); 
    }); 
    $("#post1").click(function() { 
     postApi(); 
    }); 
}); 


</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. --> 

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


<button id="logout">Logout</button> 
<button id="post1">post something</button> 
<div id="result"></div> 
</body> 
</html> 
+0

素晴らしい、あなたがそれを聞いてうれしい! – chrisboustead

9

これを達成するには、2つのことが必要です。 JS-SDKで2番目のステップを実行できるかどうかはわかりませんが、必要に応じてサーバーサイドSDKを使用できます。

アプリケーションでは、manage_pagespublish_streamのアクセス権を要求する必要があります。 /{user-id}/accountsに電話をかけ、許可されたユーザーが管理しているすべてのページとそれぞれのpage access tokensを返します。

投稿するページのページアクセストークンが変数に格納されます。アップロードする写真をsourceパラメータ(コードを実行しているサーバーに対してローカルである必要があります)に設定し、ページアクセストークン(アプリケーションアクセストークンではありません)を使用してPOSTリクエストを/{page_id}/photosにします。

だから、の線に沿って次のようになります。

FB.api('/{page_id}/photos', 'post', { source: 'path/to/image.jpg', access_token: {page_access_token}, message: 'hey heres a photo' }, function(response) { 
    if (!response || response.error) { 
    alert('Error occured'); 
    } else { 
    alert('Post ID: ' + response.id); 
    } 
}); 

私は、アプリケーションがまた、初期化するときfileUploadとしてtrueを指定する必要があります信じています。

もしあなたに役立つなら、これを行うために私のPHPコードを分かち合うことができます。

+0

OKおかげで、私はそれをしようとします。そして、はい、私もPHPを使用するので、PHPのスニペットが役立ちます。 – ling

関連する問題