php 如何使用 FB Graph 在提要(墙)上发布消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2783582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Howto use FB Graph to post a message on a feed (wall)
提问by qualbeen
I have created an app, and now i want to post a message on one of my friends wall with use of the new Graph API. Is this do-able?
我已经创建了一个应用程序,现在我想使用新的 Graph API 在我的一个朋友墙上发布一条消息。这能行吗?
I am already using oAuth and the Graph-api to get a list of all my friends. The API at http://developers.facebook.com/docs/apitells me to cURL https://graph.facebook.com/[userid]/feedto read the feed, but it also tells me howto post a message:
我已经在使用 oAuth 和 Graph-api 来获取我所有朋友的列表。http://developers.facebook.com/docs/api 上的 API告诉我 cURL https://graph.facebook.com/[userid]/feed来阅读提要,但它也告诉我如何发布消息:
curl -F 'access_token=[...]' -F 'message=Hello, Arjun. I like this new API.' https://graph.facebook.com/arjun/feed
Ofcourse this doesn't work! And I can't find out why..
这当然行不通!我不知道为什么..
Here are my PHP-code:
这是我的 PHP 代码:
require_once 'facebook.php'; // PHP-SDK downloaded from http://github.com/facebook/php-sdk
$facebook = new Facebook(array(appId=>123, secret=>'secret'));
$result = $facebook->api(
'/me/feed/',
array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..')
);
This code does not throws any error, and I know my access_token are correct (otherwise i could't run $facebook->api('/me?access_token='.$this->access_token); to get my userobject.
此代码不会引发任何错误,并且我知道我的 access_token 是正确的(否则我无法运行 $facebook->api('/me?access_token='.$this->access_token); 来获取我的用户对象。
Have anyone out there sucsessfully posted a message using Graph-api? Then i needyour help! :-)
有没有人使用 Graph-api 成功发布消息?那我需要你的帮助!:-)
回答by qualbeen
Okay, I finally solved this. Thanx to phpfour for your help :-)
好的,我终于解决了这个问题。感谢 phpfour 的帮助:-)
First: My connection-url looks like this (with "publish_stream"):
第一:我的连接 url 看起来像这样(带有“publish_stream”):
$connectUrl = $this->getUrl(
'www',
'login.php',
array_merge(array(
'api_key' => $this->getAppId(),
'cancel_url' => $this->getCurrentUrl(),
'req_perms' => 'publish_stream',
'display' => 'page',
'fbconnect' => 1,
'next' => $this->getCurrentUrl(),
'return_session' => 1,
'session_version' => 3,
'v' => '1.0',
), $params)
);
Second; I tried to post to facebook via
第二; 我试图通过
$result = $facebook->api(
'/me/feed/',
array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..')
);
But the correct way to do this is include one more parameter ('post'):
但正确的做法是再包含一个参数('post'):
$result = $facebook->api(
'/me/feed/',
'post',
array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..')
);
回答by Mohammad Emran Hasan
You'll need the "publish_stream" extended permission in order to write to the feed. Here is a complete list of them: http://developers.facebook.com/docs/authentication/permissions.
您需要“publish_stream”扩展权限才能写入提要。这是它们的完整列表:http: //developers.facebook.com/docs/authentication/permissions。
In order to get the extended permission, get the authorization token in this way:
为了获得扩展权限,通过这种方式获取授权令牌:
https://graph.facebook.com/oauth/authorize?
client_id=...&
redirect_uri=http://www.example.com/callback&
scope=publish_stream
回答by Saboor
In addition to chf,
除了chf,
After posting:
发帖后:
$getLinkToken='https://graph.facebook.com/oauth/access_token'.
'?client_id=YOUR_APPID'.
'&redirect_uri=YOUR_SITE'.
'&client_secret=YOUR_SECRET'.
'&code=CODE_KEY';
I got the response:
我得到了回应:
https://graph.facebook.com/oauth/access_token?
client_id=xxxxxxxxxxxxxx
&redirect_uri=myurl
&client_secret=xxxxxxxxxxxxxx
&code=xxxxxxxxxxxxxx
no which one is access_token, client_secretor code
没有哪个是access_token,client_secret或代码
$facebook->api( '/YOUR_APPID/feed/', 'post',
array('access_token' => $this->access_token,
'message' => 'Playing around with FB Graph..'));
回答by Konstantinos
As the link says: enter link description here
正如链接所说:在此处输入链接描述
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_POST_LOGIN_URL";
$code = $_REQUEST["code"];
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=email";
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
$graph_url="https://graph.facebook.com/me/permissions?".$access_token;
echo "graph_url=" . $graph_url . "<br />";
$user_permissions = json_decode(file_get_contents($graph_url));
print_r($user_permissions);
?>
回答by edibleEnergy
To clarify, 'post' here refers to the HTTP method, as in GET/POST. See https://github.com/facebook/php-sdk/blob/master/src/base_facebook.php: protected function _graph($path, $method = 'GET', $params = array())
澄清一下,这里的“post”指的是 HTTP 方法,如 GET/POST。请参阅https://github.com/facebook/php-sdk/blob/master/src/base_facebook.php:受保护的函数 _graph($path, $method = 'GET', $params = array())
$result = $facebook->api( '/me/feed/', 'post', array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..') );
$result = $facebook->api( '/me/feed/', 'post', array('access_token' => $this->access_token, 'message' => '玩 FB Graph..') ) ;
回答by codercat
Instead of using the below code
而不是使用下面的代码
[facebook dialog:@"feed"
andParams:params
andDelegate:self];
Use the following solution
使用以下解决方案
[facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
回答by chf
That is old way to get acces. In GRAPH first i generated codekey with:
这是获得访问权限的旧方法。在 GRAPH 中,我首先生成了代码密钥:
$getLinkCode ='https://graph.facebook.com/oauth/authorize'.
'?client_id=YOUR_APPID'.
'&redirect_uri=YOUR_SITE'.
'&scope=publish_stream';
And now when we have codekey we can generate access_tokenfrom link:
现在,当我们拥有代码密钥时,我们可以从链接生成access_token:
$getLinkToken='https://graph.facebook.com/oauth/access_token'.
'?client_id=YOUR_APPID'.
'&redirect_uri=YOUR_SITE'.
'&client_secret=YOUR_SECRET'.
'&code=CODE_KEY';
But this access_token post your message as USER not APPLICATION... WHY?!
但是这个 access_token 将您的消息发布为 USER 而不是 APPLICATION ... 为什么?!
If you want post on application wall use:
如果您想在应用程序墙上张贴,请使用:
$facebook->api( '/YOUR_APPID/feed/', 'post', array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..'));

