未捕获的 OAuthException: (#200),当尝试在墙上发帖时 [英] Uncaught OAuthException: (#200), when trying to post on wall

查看:13
本文介绍了未捕获的 OAuthException: (#200),当尝试在墙上发帖时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将消息从我的数据库简单地发布到我的 facebook 墙上.使用 php-SDK:

I want to implement a simple posting of messages from my DB to my facebook's wall. Using php-SDK:

require_once 'lib/facebook.php';

$appID = 'xxx';
$secret = 'yyy';
$facebook = new Facebook(array(
    'appId'     =>  $appID,
    'secret'    =>  $secret,
));

$user = $facebook->getUser();
if (empty($user)) {
    header("Location: ". $facebook->getLoginUrl(array(
        'req_perms' =>  'publish_stream',
    )));
    exit();
}

$params = array(
    'message' => 'Hello, every one!',
);
$post_id = $facebook->api('/'. $user .'/feed', 'post', $params);

而且我有未捕获的 OAuthException: (#200) 用户尚未授权应用程序执行在 ... 中抛出的此操作".

And i have "Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action thrown in ...".

如果我没有登录,那么在运行这个脚本时,重定向到 facebook 登录.С确认未给出数据的用途.

If I'm not logged in, then when run this script, redirecting to the facebook login. Сonfirm the use of the data is not given.

推荐答案

这是一个工作片段,但请确保您已下载最新的 PHP SDK,即 3.0.1如果您还没有,请从这里下载:https://github.com/facebook/php-sdk/

Here's a working snippet, but make sure you've downloaded the latest PHP SDK i.e. 3.0.1 If you don't have already, then download it from here: https://github.com/facebook/php-sdk/

如果你仔细阅读下面的代码,你就会大致了解它的作用,所以我想我不需要再解释你了吗?

If you go through the codes below, you'll pretty much understand what it does, so i don't think i have to explain you more?

<?php

// Requires Facebook PHP SDK 3.0.1: https://github.com/facebook/php-sdk/
require ('facebook.php');

define('FACEBOOK_APP_ID',"YOUR-APP-ID");
define('FACEBOOK_SECRET',"YOUR-APP-API-SECRET");

$user = null;

$facebook = new Facebook(array(
    'appId' => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET,
    'cookie' => true
));

$user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.

if($user == 0) {

    /**
     * Get a Login URL for use with redirects. By default, full page redirect is
     * assumed. If you are using the generated URL with a window.open() call in
     * JavaScript, you can pass in display=popup as part of the $params.
     * 
     * The parameters:
     * - redirect_uri: the url to go to after a successful login
     * - scope: comma separated list of requested extended perms
     */

    $login_url = $facebook->getLoginUrl($params = array('scope' => "publish_stream"));

    echo ("<script> top.location.href='".$login_url."'</script>");

} else {

    try {
            $params = array(
                'message'       =>  "Hurray! This works :)",
                'name'          =>  "This is my title",
                'caption'       =>  "My Caption",
                'description'   =>  "Some Description...",
                'link'          =>  "http://stackoverflow.com",
                'picture'       =>  "http://i.imgur.com/VUBz8.png",
            );

            $post = $facebook->api("/$user/feed","POST",$params);

            echo "Your post was successfully posted to UID: $user";

        }
        catch (FacebookApiException $e) {
           $result = $e->getResult();
        }

}

?>

这篇关于未捕获的 OAuthException: (#200),当尝试在墙上发帖时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆