Получение разрешения от пользователя Facebook и размещение на стене - PullRequest
0 голосов
/ 03 ноября 2011

В дополнение к Опубликовать на стене пользователя после отправки приложения в 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
?>

Ответы [ 2 ]

1 голос
/ 03 ноября 2011

Я просто выложу код, который я использую, который работает. Надеюсь, это поможет

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/')
                ));
        }
0 голосов
/ 03 ноября 2011

Вам нужен токен доступа 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",
);

Вы можете получить токены доступа здесь .

...