Facebook API:如何在不登录的情况下发布到自己的应用程序墙 [英] Facebook API: How to post to own application wall without login

查看:26
本文介绍了Facebook API:如何在不登录的情况下发布到自己的应用程序墙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将带有脚本的文本发布到我自己的应用程序墙上,但无需先登录,因为它应该自动完成.我怎么能那样做?我已经试过了:

$fb = new Facebook(array('appId' =>'appid','秘密' =>'appsecret','cookie' =>真的));如果 ($fb->getSession()) {//邮政} 别的 {//记录器//每次我到这里:(}

我需要做什么才能获得使用脚本发布到我自己的应用程序墙的权限?

解决方案

如果你想发布到你自己的应用程序墙,你只需要一个应用程序访问令牌,如果你想在没有登录的情况下发布到用户墙,您还需要此用户的长期访问令牌,为此您必须请求离线访问权限.

发布到您的应用程序墙:

1- 卷曲此链接以获取您的应用程序访问令牌:

https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials

2- 发布到墙上而不检查会话

示例:

'client_credentials','client_id' =>$appid,'client_secret' =>$appsecret);$ch = curl_init();$url = 'https://graph.facebook.com/oauth/access_token';curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_HEADER, false);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, $args);$data = curl_exec($ch);返回 json_encode($data);}//创建FB对象实例$facebook = 新 Facebook(数组('appId' =>$appid,'秘密' =>$appsecret,'cookie' =>错误的,));//获取应用令牌$token = get_app_token($appid, $appsecret);//尝试在墙上发布或捕获Facebook异常尝试 {$attachment = array('message' => '','access_token' =>$代币,'名称' =>'附件名称','标题' =>'附件标题','链接' =>'http://apps.facebook.com/xxxxxx/','说明' =>'描述 .....','图片' =>'http://www.google.com/logo.jpg','动作' =>数组(数组('名称' => '动作文本','链接' =>'http://apps.facebook.com/xxxxxx/')));$result = $facebook->api('/'.$appid.'/feed/', 'post', $attachment);}//如果帖子没有发布,打印错误详情捕获(FacebookApiException $e){echo '

';打印_r($e);echo '</pre>';}

查看此页面中的 APP LOGIN 部​​分以获取更多信息:http://developers.facebook.com/docs/authentication/

I want to post to my own application wall a text with a script but without to login first because it should be done automatically. How could I do that? I tried already:

$fb = new Facebook(array(
    'appId'  => 'appid',
    'secret' => 'appsecret',
    'cookie' => true
));


if ($fb->getSession()) {
    // Post
} else {
    // Logger
    // Every time I get in here :(
}

What do I have to do to get the access to post to my the own app wall with a script?

解决方案

If you want to post to your own Application wall, all you need is an application Access Token, and if you want to publish to an user wall without login, you also need this user long live access token, for that you have to ask for Offline access permission.

To publish to your application wall :

1- Curl this link to get your application access token :

https://graph.facebook.com/oauth/access_token? client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET& grant_type=client_credentials

2- Publish to wall without checking for the session

Example :

<?php
require_once 'facebook.php'

//Function to Get Access Token
function get_app_token($appid, $appsecret)
{
$args = array(
'grant_type' => 'client_credentials',
'client_id' => $appid,
'client_secret' => $appsecret
);

$ch = curl_init();
$url = 'https://graph.facebook.com/oauth/access_token';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);

return json_encode($data);
}

// Create FB Object Instance
$facebook = new Facebook(array(
    'appId'  => $appid,
    'secret' => $appsecret,
    'cookie' => false,
    ));


//Get App Token
$token = get_app_token($appid, $appsecret);

//Try to Publish on wall or catch the Facebook exception
try {
$attachment = array('message' => '',
            'access_token' => $token,
                    'name' => 'Attachment Name',
                    'caption' => 'Attachment Caption',
                    'link' => 'http://apps.facebook.com/xxxxxx/',
                    'description' => 'Description .....',
                    'picture' => 'http://www.google.com/logo.jpg',
                    'actions' => array(array('name' => 'Action Text', 
                                      'link' => 'http://apps.facebook.com/xxxxxx/'))
                    );

$result = $facebook->api('/'.$appid.'/feed/', 'post', $attachment);
}

//If the post is not published, print error details
catch (FacebookApiException $e) {
echo '<pre>';
print_r($e);
echo '</pre>';
}

Check APP LOGIN part in this page for more informations : http://developers.facebook.com/docs/authentication/

这篇关于Facebook API:如何在不登录的情况下发布到自己的应用程序墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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