【问题标题】:Grabbing permission from facebook user and posting to their wall从 facebook 用户那里获取许可并发布到他们的墙上
【发布时间】:2011-12-21 01:55:43
【问题描述】:

除了Post to users wall upon facebook app submission(我的老问题),我想出了以下代码,但它似乎不起作用?我认为最好打开一个新问题,因为它是一个新问题。

我做错了什么?另外,这段代码应该放在哪里?

<?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
?>

【问题讨论】:

    标签: php facebook api permissions


    【解决方案1】:

    我将发布我正在使用的代码。希望对你有帮助

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

    进程.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/')
                    ));
            }
    

    【讨论】:

    • 嗨,彼得,我已经尝试过该代码,但没有任何乐趣。你是用什么方法设置的?你如何调用你的代码?另外,您不需要访问令牌吗?
    • 好吧,你必须使用类。 fbClass.phpclass fbClass { private $fbid = "my app id"; private $fbsecret = "secret"; 开头,然后我将它包含在 process.php 中:require 'classes/facebook-sdk/src/facebook.php'; require 'classes/fbClass.php'; 创建一个新实例:$fbClass = new fbClass();,然后调用函数:$facebook = $fbClass-&gt;facebook; $fbAuth = $fbClass-&gt;authUser($facebook); 差不多了。
    • 您可以发布您的完整 process.php 和 fbClass.php 页面吗?所以我可以了解如何用我的代码实现它?另外,我应该在哪里调用 process.php 页面?我有三个主要页面,index.php(进入应用程序的按钮),enter.php(表单所在的位置等...),thankyou.php(感谢页面。)
    • 好吧,我不能把所有的东西都发给你,因为它是一个很大的应用程序。 request.php 使用 AJAX 调用,但您可以在任何其他文件中使用 request.php 中的代码。在你的情况下,我会在 enter.php 或thankyou.php 中说,这取决于你想要实现的目标。没有太多的实现,因为fbClass.php 是一个标准类,request.php 只是一个带有一些逻辑的文件(我有很多其他不相关的代码)。您是否有任何错误可以帮助我理解您的问题?
    • 感谢您的帮助,我只是有点困惑。基本上我有三个页面,index.php enter.php 和thankyou.php。 enter.php 是将数据导出到 csv 的表单。当用户点击索引页面上的“Enter”时,我需要权限对话框直接出现,如果他们点击接受,它将发布到用户墙,如果他们拒绝,它将把他们带回索引页面。我很困惑将所有代码放在哪里以及如何使用这些类?感谢您的帮助
    【解决方案2】:

    您需要 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",
    );
    

    您可以获得访问令牌here

    【讨论】:

    • 您好,谢谢,生成访问令牌需要什么权限才能发布到其他用户墙?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多