2013-11-25 11 views
8

enter image description herefacebook programlı büyük bir fotoğrafın benim sahte facebook sayfası (eğlence sayfası) oluşturduk

ile facebook sayfasında yayınlanması. ekli görüntünün Sol tarafta, ben (aşağıda büyük fotoğraf ile bir) elle ilk yayını yapılan ve programlı bir (küçük fotoğraf ile yukarıda bir tane).

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

Ama onunla mutlu değilim:

Ben küçük fotoğraf için kullanılan kod şöyle Üzerinde çalıştığım uygulama kadar kayıp hayvanların bir listesini yapmak o büyük fotoğraflarla çok daha büyük olurdu.

Bunu facebook geliştirici sayfalarında nasıl yapacağınıza dair bir örnek görmedim. Bunun mümkün olduğuna inanıyorum ama henüz bulamadım. Daha önce bu problemi geçmiş miydiniz?

cevap

7

Sonunda yaptım! Çözümü gönderiyorum, bana doğru yönde işaret ettiğim için cdbconcepts teşekkürler. tekrar doktor okuduktan sonra: "Ayrıca fotoğrafın URL ile bir url param sağlayarak bir fotoğraf yayınlayabilir.":

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

Öyle derler aşağıdaki örnekte gösterildiği gibi url, aynı sunucuda olmak zorunda değildir, ve js-sdk ile çalışır.

<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

Harika, bunu duyduğunuzu duyduğuma sevindim! – chrisboustead

9

bunu başarmak için yapmanız gereken iki şey vardır. JS-SDK ikinci adımı yapmanıza izin verirse% 100 emin değilim, ancak gerekirse sunucu tarafında SDK kullanabilirsiniz.

Uygulama, manage_pages ve publish_stream iznini istemelidir. Daha sonra, yetkili kullanıcının yönettiği tüm sayfaları ve ilgili page access tokens'u döndürecek olan /{user-id}/accounts numaralı telefonu arayın.

Gönderi yapmak istediğiniz sayfa için döndürülen sayfa erişim belirtecini bir değişkende saklayın. Yüklemek istediğiniz fotoğrafı source parametresi olarak ayarlayın (kodu çalıştıran sunucuya yerel olmalıdır) ve sayfa erişim belirtecini kullanarak POST isteğini /{page_id}/photos yapın (Uygulama erişim belirteci NOT!).

yüzden çizgisinde olacaktır:

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

ben Uygulama ayrıca başlatırken fileUpload olarak gerçek belirtmek gerekiyor inanıyoruz.

Size yardımcı olması için PHP kodumu paylaşmaktan mutluluk duyarız.

+0

Tamam teşekkürler, ben o çalışacağım:

Yani burada benim için çalışan nihai kodudur. Ve evet, ayrıca php'yi de kullanıyorum, böylece bir php snippet'i faydalı olacaktır. – ling

İlgili konular