fb:当用户在网络应用上注册时如何在用户个人资料上发布?

时间:2013-03-16 09:13:48

标签: facebook facebook-graph-api

我正在将facebook登录集成到我的应用程序中,evrything工作正常。我现在想要两件事。

  1. 我希望每当有新用户点击并点击“使用facebook登录”时,在所有流程之后,应该在该用户墙上发布帖子,该用户已开始使用我的应用程序并带有指向app的链接.. 就像我们看到那些游戏一样,“< user>开始使用Hold'em扑克”

  2. 获取用户个人资料图片

  3. 此处是指向http://oauth.netne.net/index.php

    的链接

    我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

在用户墙上张贴:

使用Javascript:

    function doPost() {
    var params = {};
    params['message'] = "<USER> started using <YOUR APP NAME>";
    params['name'] = '<YOUR APP NAME>';
    params['description'] = '<DESCRIPTION ABOUT YOUR APP>';
    params['link'] = "<LINK TO YOUR WEBSITE>";
    //ON CLICKING THE POST WHERE YOU WANT USER  TO BE REDIRECTED e.g. www.yoursite.com
    params['picture'] = '<PATH OF PICTURE THAT YOU WANT TO BE DISPLAYED ON THE POST>';
    FB.api('/me/feed', 'post', params, function(response) {
        if(!response || response.error) {
            console.log('ERROR OCCURED '+response.error);
        }
        else {
            console.log('POSTED SUCCESSFULLY');
        }
    }}

使用PHP:

$ret_obj = $facebook->api('/me/feed', 'POST',
       array( 'link' => '<LINK TO YOUR WEBSITE>',
              'message' => '<USER> started using <YOUR APP NAME>',
              'description' => "<DESCRIPTION ABOUT YOUR APP>")); //Returns the POST ID.

获取用户的图像:

$fbImageUrl = https://graph.facebook.com/**<FBUSER_ID>**/picture?type=<VALUE>(或)

$fbImageUrl = https://graph.facebook.com/**<FBUSER_NAME>**/picture?type=<VALUE>

哪里可以是“小”或“正常”或“大”或“正方形”。

//用于保存图片:

$localFilePath = 'pathToImagesFolder/<SOME_NAME>.jpg'; //Use relative path.
$fbImage = file_get_contents($fbImageUrl); //Fetches the image and stores it in variable.
file_put_contents($file1, $img1); //Will save the image in the path specified.

答案 1 :(得分:0)

是的,你必须处理这种情况。如果用户未授予访问权限请尝试:

  try {
        $ret_obj = $facebook->api('/me/feed', 'POST',
        array( 'link' => '<LINK TO YOUR WEBSITE>',
               'message' => '<USER> started using <YOUR APP NAME>',
               'description' => "<DESCRIPTION ABOUT YOUR APP>"));

      } catch(FacebookApiException $e) {
        $login_url = $facebook->getLoginUrl(array('scope' => 'publish_stream'));
        header("Location: $login_url"); 
      }

但如果用户连续重定向,这会使用户烦恼。因此,最好跳过这一步而不是强迫用户接受它。请记住,您应该提供注册时所需的所有范围。