2013-11-25 2 views
8

enter image description here페이스 북 프로그래밍 큰 사진 나는 가짜 페이스 북 페이지 (엔터테인먼트 페이지)를 만든

와 페이스 북 페이지에 게시합니다. 첨부 된 이미지의 왼쪽에 수동으로 첫 번째 게시물을 작성했습니다 (위 그림은 큰 사진 ). 그리고 프로그래밍 방식으로 하나의 게시물을 작성했습니다 (위의 사진은 작은 사진).

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

그러나 나는 그것으로 행복하지 않다 :

나는 작은 사진에 사용 된 코드는 다음과 같습니다 내가 일하고 있어요 응용 프로그램이 때문에, 분실 동물의 목록을 만들어 그것을 큰 사진의 경우 훨씬 더 큽니다.

페이스 북 개발자 페이지에서이를 수행하는 방법에 대한 예제가 없습니다. 이것이 가능하다고 생각하지만 아직 발견하지 못했습니다. 이전에 이미이 문제를 겪었습니까?

답변

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

이 작업을 수행하는 데는 두 가지 사항이 필요합니다. JS-SDK에서 두 번째 단계를 수행 할 수 있는지 100 % 확신 할 수는 없지만 필요한 경우 서버 측 SDK를 사용할 수 있습니다.

신청서에 manage_pagespublish_stream 권한이 필요합니다. 그런 다음 승인 된 사용자가 관리하는 모든 페이지와 해당 page access tokens을 반환하는 /{user-id}/accounts으로 전화하십시오.

게시하려는 페이지에 대해 반환 된 페이지 액세스 토큰을 변수에 저장하십시오. 업로드 할 사진을 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

확인 감사합니다, 나는 그것을 시도 할 것이다 :

그래서 여기 나를 위해 작동 최종 코드입니다. 그리고 예, 저는 또한 PHP를 사용하여 PHP 코드가 도움이 될 것입니다. – ling