簡體   English   中英

取得Facebook用戶的許可並張貼到他們的牆上

[英]Grabbing permission from facebook user and posting to their wall

除了在Facebook應用提交后向用戶發布牆 (我的老問題)之外,我想出了以下代碼,但似乎不起作用? 我認為最好打開一個新問題,因為這是一個新問題。

我究竟做錯了什么? 另外,此代碼應放在哪里?

<?php
$session = $facebook->getSession();

//Is user logged in and has allowed this app to access its data
if (!$session) {
    $loginUrl = $facebook->getLoginUrl(array(
    'canvas' => 1,
    'fbconnect' => 0,
    'next' => 'enter.php',
    'cancel_url' => 'index.php',
    ));    

//    use the $loginUrl created on the enter button to request permission;
}
$user_id = $facebook->getUser();


//post to wall
    $attachment = array('message' => '<message>',
                    'name' => '<name here>',
                    'caption' => '<caption here>',
                    'link' => '<link to app>',
                    'description' => '<enter description >',
                    'picture' => '<enter image url>',
                    'actions' => array(array('name' => '<enter action label>', 
                                      'link' => '<enter action url>')
                    );

    $permissions = $facebook->api("/me/permissions");
    if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
// Permission is granted!
// Do the related task
try {
$post_id = $facebook->api('/me/feed', 'post', $attachment);
    } catch (CurlException $e) {
//timeout so try to resend
$post_id = $facebook->api('/me/feed', 'post', $attachment);
    } 
    catch (FacebookApiException $e) {
error_log($e);
    }   
    } else {
// We don't have the permission
// Alert the user or ask for the permission!
    }

// store the post id in-case you need to delete later
?>

我只發布我正在使用的代碼即可。 希望能幫助到你

fbClass.php

    public function __construct() {
    // Naredimo instanco
    $facebook = new Facebook(array(
                'appId' => $this->fbid,
                'secret' => $this->fbsecret,
                'cookie' => true,
            ));

    $this->facebook = $facebook;
}

function authUser($facebook) {

    $user = $facebook->getUser();

    if ($user) {
        try {
            // Proceed knowing you have a logged in user who's authenticated.
            $user_profile = $facebook->api('/me');
        } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
        }
    }

    // Login or logout url will be needed depending on current user state.
    if (!($user)) {
        $loginUrl = $facebook->getLoginUrl(array(
                    'scope' => 'user_about_me, user_birthday, email, publish_stream',
                    'redirect_uri' => 'http://apps.facebook.com/myappname/',
                ));
        echo("<script> top.location.href='" . $loginUrl . "'</script>");
    } else {
        return true;
    }
}

process.php

$facebook = $fbClass->facebook;
$fbAuth = $fbClass->authUser($facebook);
if ($fbAuth) {

        $res = $facebook->api('/me/feed/', 'post', array(
                    'message' => MESSAGE,
                    'name' => NAME,
                    'caption' => '',
                    'description' => DESC,
                    'picture' => PIC,
                    'link' => 'http://www.facebook.com/myapp/',
                    'actions' => array('name' => 'Test', 'link' => 'http://apps.facebook.com/myapp/')
                ));
        }

您需要一個Facebook訪問令牌才能使此代碼正常工作。 將您的令牌添加到以下代碼中的“ My Access token here中:

$attachment = array(
'access_token' => 'My Access token here',
'message'      => '',
'name'         => 'My Wall Post Header/Title Here',
'caption'      => 'Small caption here',
'link'         => 'http://www.mywebsite.org',
'description'  => 'Wall Post Details Here',
'picture'      => "http://www.mywebsite.org/images/logo.gif",
);

您可以在此處獲取訪問令牌。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM