php 在不在线/登录的情况下发布到用户的墙 - 使用 Graph API 的 Facebook 共享

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4814432/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:29:01  来源:igfitidea点击:

Publishing To User's Wall Without Being Online/Logged-in - Facebook Sharing Using Graph API

phpfacebookfacebook-graph-apifacebook-php-sdk

提问by Harish

Hi i'm new to facebook sharing. I want to make a user log in using his uid and then store these for further uses

嗨,我是 facebook 分享的新手。我想让用户使用他的 uid 登录,然后存储这些以供进一步使用

The primary purpose is to share links in the user's wall

主要目的是在用户的墙上分享链接

next time onwards without showing the facebook window i want to post in to his wall through ajax.

下次不显示 facebook 窗口,我想通过 ajax 发布到他的墙上。

How is it possible any help regarding this will be appreciated!

怎么可能对此有任何帮助将不胜感激!

EDIT

编辑

I used the example given and then tried curl function

我使用了给出的例子,然后尝试了 curl 函数

got this error

得到这个错误

HTTP/1.1 403 Forbidden
Cache-Control: no-store
Content-Type: text/javascript; charset=UTF-8
Expires: Sat, 01 Jan 2000 00:00:00 GMT
P3P: CP="Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p"
Pragma: no-cache
WWW-Authenticate: OAuth "Facebook Platform" "insufficient_scope" "(#200) 
This API call requires a valid app_id."
Set-Cookie: datr=g4JBTb4OsRZxBAztV7iIkpWg; expires=Sat, 26-Jan-2013 14:34:43 GMT;
path=/; domain=.facebook.com; httponly
X-Cnection: close
Date: Thu, 27 Jan 2011 14:34:43 GMT
Content-Length: 93

{"error":{"type":"OAuthException","message":"(#200) 
This API call requires a valid app_id."}}

EDIT SO THAT SOMEONE MAY FIND USEFUL

编辑以便有人可能会觉得有用

FACEBOOK CONNECTION

脸书连接

$facebook_connect =  array(
   'appId'  => 'YOUR APP_ID',
   'secret' => 'YOUR SEC KEY',
          'access_token'=>"USER'S ACCESS TOKEN",
   'cookie' => true
 );

POST TO USER WALL

发布到用户墙

$facebook->api("/PROFILE_ID/feed", "post", array(
"message"=>"Hi Friends Join ****",
"name"=>"You're invited to join ****!",
"link"=>"www.****.com",
"description"=>"Great site",
"picture"=>"http://www.****.com/logo.jpg",
"caption"=>"Join *****"
)

回答by ifaour

I suggest you start learning how Facebook Graph API works first.

我建议您首先开始学习 Facebook Graph API 的工作原理。

  1. Facebook will NEVERshare the user password with you!
  2. If you just need to give the user the possibility to share link, then just use the like plugin. You may also find more interesting social pluginsto use in your website.
  3. If you use the like plugin, it won't open any popups and it would post the link directly to the user's wall.
  4. You could always use the Feed Dialog
  5. Start reading the Facebook Documentation
  1. Facebook永远不会与您分享用户密码!
  2. 如果您只需要让用户可以共享链接,那么只需使用like 插件。您还可以在您的网站中找到更多有趣的社交插件
  3. 如果您使用类似插件,它不会打开任何弹出窗口,而是将链接直接发布到用户的墙上。
  4. 您可以随时使用Feed Dialog
  5. 开始阅读Facebook 文档


Now to post on the user's wall (on his behalf) withouthim being logged-in, you need the following:

现在要在用户的墙上(代表他)在没有登录的情况下发帖,您需要以下内容:

  1. app access_token
  2. publish_streampermission, NO NEEDfor the long-lived access token:
  1. 应用程序 access_token
  2. publish_stream权限,不需要长期访问令牌:

Enables your app to post content, comments, and likes to a user's stream and to the streams of the user's friends. This is a superset publishing permission which also includes publish_actions. However, please note that Facebook recommends a user-initiated sharing model. Please read the Platform Policies to ensure you understand how to properly use this permission. Note, you do not need to request the publish_stream permission in order to use the Feed Dialog, the Requests Dialog or the Send Dialog.

使您的应用能够向用户的信息流和用户朋友的信息流发布内容、评论和喜欢。这是一个超集发布权限,其中还包括 publish_actions。但是,请注意 Facebook 推荐用户发起的共享模式。请阅读平台政策以确保您了解如何正确使用此权限。请注意,您无需请求 publish_stream 权限即可使用 Feed Dialog、Requests Dialog 或 Send Dialog。

Require the permission:
This can be done in multiple of ways:
Using the Login Plugin:

需要权限:
这可以通过多种方式完成:
使用登录插件

<div class="fb-login-button" data-show-faces="true" data-width="200" data-scope="publish_stream" data-max-rows="1"></div>

Server-side login(Redirect to the OAuth Dialog):

服务器端登录(重定向到 OAuth 对话框):

https://www.facebook.com/dialog/oauth?
     client_id=YOUR_APP_ID
     &redirect_uri=YOUR_URL
     &scope=publish_stream
     &state=SOME_ARBITRARY_BUT_UNIQUE_STRING

PHP-SDK:

PHP-SDK:

$loginUrl = $facebook->getLoginUrl(array("scope"=>"publish_stream"));

JS-SDK through the FB.loginmethod:

JS-SDK 通过FB.login方法:

 FB.login(function(response) {
   if (response.authResponse) {
     console.log('Welcome!  Fetching your information.... ');
     FB.api('/me', function(response) {
       console.log('Good to see you, ' + response.name + '.');
       FB.logout(function(response) {
         console.log('Logged out.');
       });
     });
   } else {
     console.log('User cancelled login or did not fully authorize.');
   }
 }, {scope: 'publish_stream'});

Publishing:

出版:

$USER_ID = "XXXXXXXX"; // Connected once to your APP and not necessary logged-in at the moment
$args = array(
    'message'   => 'Hello from app',
    'link'      => 'http://www.masteringapi.com/',
    'caption'   => 'Visit MasteringAPI.com For Facebook API Tutorials!'
);
$post_id = $facebook->api("/$USER_ID/feed", "post", $args);

Note:
While it's possible to post without the user's presence always remember Facebook recommends a user-initiated sharing model

注意:
虽然可以在用户不在场的情况下发帖,但请始终记住 Facebook 推荐用户发起的共享模型