简体   繁体   English

使用Facebook API PHP在某人的墙上张贴

[英]Post on someones wall using Facebook API PHP

Is it possible to write an app that posts message on someones wall and if that user hasnt accepted permission for that is prompted to do so at this moment? 是否可以编写一个将消息发布到某人的墙上的应用程序,并且如果该用户此时尚未接受该消息的提示,则是否可以这样做?

here is my code: 这是我的代码:

$attachment =  array(
        'access_token' => $access_token,
        'message' => "$message",
        'name' => "testName",
        'description' => "Check Out new message",
        'link' => '',
        'picture' => "test message"
    );

    try {
        $response = $facebook->api("/userId/feed/", 'POST', $attachment);
    } catch (FacebookApiException $e) {

    }

Here is something I wrote on the subject a while back for documentation purposes. 这是我前一段时间出于主题目的写的东西。 Hope it helps you out: 希望它可以帮助您:

Creating an App with the Facebook Graph API January 27, 2012 使用Facebook Graph API创建应用程序2012年1月27日

Getting Started in Facebook To get started building a Facebook Application using the Graph API, you will first need to register with Facebook before you can begin coding at https://developers.facebook.com . Facebook入门要开始使用Graph API构建Facebook应用程序,首先需要在Facebook上注册,然后才能在https://developers.facebook.com上进行编码。 After creating an account or logging in with an existing account, you will need to create a new App (or configure the app properly if you are using an existing app). 创建帐户或使用现有帐户登录后,您将需要创建一个新的应用程序(或使用现有应用程序正确配置该应用程序)。 To create your Facebook Application, go to https://developers.facebook.com/apps and click on "Create New App" (or "Edit App" if you already have one created and would like to configure it). 要创建您的Facebook应用程序,请访问https://developers.facebook.com/apps并单击“创建新应用程序”(如果已经创建了一个并想要配置它,则单击“编辑应用程序”)。 Under the Settings->Basic tab, you will need to, at the very least, fill out the App Display Name, Contact E-Mail, and Site URL. 在“设置”->“基本”选项卡下,您至少需要填写“应用程序显示名称”,“联系电子邮件”和“网站URL”。 Of these, the Site URL is the most important and MUST point to where your application will reside (This can always be updated later if you don't know yet). 其中,站点URL是最重要的,必须指向您的应用程序将驻留的位置(如果您尚不知道,则以后可以随时更新)。 Next, go to the Settings->Auth Dialog tab. 接下来,转到“设置”->“验证对话框”选项卡。 You aren't required to fill any of this out, but it is a good idea since this is what the users will see when they are Allowing your App to access their account. 您不需要填写任何内容,但这是一个好主意,因为这是用户在允许您的应用访问其帐户时看到的内容。 Save everything and head back to https://developers.facebook.com/apps . 保存所有内容,然后返回https://developers.facebook.com/apps You should see the App that you just configured on this page. 您应该在此页面上看到刚刚配置的应用程序。 The two things you will need to copy down to move forward from this point are the App ID/API Key and the App Secret. 从这时开始,您需要复制下来的两件事是App ID / API密钥和App Secret。 You are now ready to begin creating your application. 现在您可以开始创建应用程序了。

Understanding the Flow Before you start coding your application, it is best to understand the flow of Authenticating a user and getting your App installed on their Facebook Account. 了解流程开始对应用程序进行编码之前,最好了解对用户进行身份验证并将应用程序安装在其Facebook帐户上的流程。 Until your App is installed on a users account and they have given the app explicit permission to publish content to their account, you will not be able to do anything. 在将您的应用安装到用户帐户上并且他们明确授予将内容发布到他们的帐户的权限之前,您将无法执行任何操作。 Good news is, a user only has to authenticate the App one time (unless they uninstall it). 好消息是,用户只需对应用程序进行一次身份验证(除非他们将其卸载)。 Take a look at the diagram below to understand how the flow works: 查看下图以了解流程的工作原理:

Authenticating a User To Initiate the process of Authenticate, you will have to build a link that will direct the user to your App. 对用户进行身份验证要启动身份验证过程,您将必须建立一个将用户定向到您的应用程序的链接。 This is how you will build your link: https://www.facebook.com/dialog/oauth?client_id=[Your API KEY]&redirect_url=[Service that will handle Authentication]&scope=[Permissions you Need]&display=touch - client_id: This is your API Key that you got from Facebook when you created your application. 这就是您建立链接的方式:https://www.facebook.com/dialog/oauth?client_id= [您的API密钥]&redirect_url = [将处理身份验证的服务]&scope = [您需要的权限]&display = touch- client_id:这是您在创建应用程序时从Facebook获得的API密钥。 - redirect_url: This is the service that will handle all of your authentication. -redirect_url:这是将处理所有身份验证的服务。 It should be located at the Site URL that you configured when you set up your application with Facebook. 它应该位于使用Facebook设置应用程序时配置的站点URL。 This is URL will have to remain constant throughout the Authentication Process. URL将在整个身份验证过程中保持不变。 - scope: This is any permission that you will need for the user's account. -作用域:这是您对用户帐户所需的任何权限。 You should only ask for what you really need. 您应该只问您真正需要什么。 We normally only need publish_stream. 我们通常只需要publish_stream。 For a complete list of permissions see: https://developers.facebook.com/docs/reference/api/permissions/ - display: This is what tells Facebook what skin to use. 有关权限的完整列表,请参见: https : //developers.facebook.com/docs/reference/api/permissions/-显示:这是告诉Facebook使用哪种皮肤的信息。 Since we are using this for mobile applications, you should set this to "touch." 由于我们将其用于移动应用程序,因此应将其设置为“ touch”。 If you are not developing for a mobile device, you may omit this all together. 如果您不是为移动设备开发的,则可以将其全部省略。

When the user clicks on the link that you build, they will be taken to the page below if they are logged in. If they are not logged in, they will be prompted to login first. 当用户单击您所构建的链接时,如果已登录,则将带他们到下面的页面。如果未登录,将提示他们首先登录。 If the user accepts your application, declines your application request, or fails to log in they will be directed to the redirect_url that you specified above. 如果用户接受您的应用程序,拒绝您的应用程序请求或无法登录,他们将被定向到您在上面指定的redirect_url。 Depending on the user's action on this page, Facebook will append different Query Strings to your redirect_url when it sends the user there. 根据用户在此页面上的操作,Facebook在将用户发送到其中时会将不同的查询字符串附加到您的redirect_url中。

If the user clicks "Allow", they will be sent to your redirect_url and the query string will contain a variable called code. 如果用户单击“允许”,它们将被发送到您的redirect_url,查询字符串将包含一个名为code的变量。 This is what you will use to get an authentication token for the user which will allow you access to their account from the App. 这就是您将用来为用户获取身份验证令牌的方法,该令牌将允许您从应用程序访问其帐户。 If the user clicks on "Don't Allow", fails to log in, or something else goes wrong, the following query strings will be appened to your redirect_url: error_reason, error, and error_description. 如果用户单击“不允许”,登录失败或发生其他问题,则以下查询字符串将应用于您的redirect_url:error_reason,error和error_description。 An example of a successful request to your redirect URL: =">http://niobiumstudio.com/appia/fbconnect/auth.php?code=AQDi7fT3whSPJr0O2ECwv494QSSNyrTFK_SGIexEFUGmw5XS8SvzfYiAsxpn0FspQYHkMgaUYH--PS1AnJnCtE-iUdRl6V3Moxfk4Cqz0igZbnkHxWi4Yl_KphXiRkbnCCW_zDqb4W2lfew9sla4FPDUKhXscRuQeI--61uQ0uStb9GwrOH4V94DjGWk1yS-Ffs# = 一个成功的请求您的重定向URL的一个例子:=“> http://niobiumstudio.com/appia/fbconnect/auth.php?code=AQDi7fT3whSPJr0O2ECwv494QSSNyrTFK_SGIexEFUGmw5XS8SvzfYiAsxpn0FspQYHkMgaUYH--PS1AnJnCtE-iUdRl6V3Moxfk4Cqz0igZbnkHxWi4Yl_KphXiRkbnCCW_zDqb4W2lfew9sla4FPDUKhXscRuQeI--61uQ0uStb9GwrOH4V94DjGWk1yS-Ffs# =

Once you have the code GET variable passed back to your redirect_url you will be able to exchange it with Facebook to get your OAuth Access Token. 将代码GET变量传递回您的redirect_url后,您将可以与Facebook交换它以获取OAuth访问令牌。 To do this, you will need to build a request to Facebook using the code you just got back. 为此,您将需要使用刚刚返回的代码向Facebook建立请求。 Here is what the request to get the access token looks like: https://graph.facebook.com/oauth/access_token?client_id=[Your API Key]&redirect_url=[Service that Handles Authentication]&client_secret=[Your App Secret]&code=[User Acceptance Code] client_id: This is your API Key that you got from Facebook when you created your application. 这是获取访问令牌的请求的样子:https://graph.facebook.com/oauth/access_token?client_id= [您的API密钥]&redirect_url = [处理身份验证的服务]&client_secret = [您的应用程序密码]&code = [用户接受代码] client_id:这是创建应用程序时从Facebook获得的API密钥。 redirect_url: This is the service that will handle all of your authentication. redirect_url:这是将处理所有身份验证的服务。 It should be located at the Site URL that you configured when you set up your application with Facebook. 它应该位于使用Facebook设置应用程序时配置的站点URL。 This is URL will have to remain constant throughout the Authentication Process. URL将在整个身份验证过程中保持不变。 THIS HAS TO BE THE SAME URL YOU USED ABOVE TO GET THE CODE client_secret: This is your App Secret Code that you got from Facebook when you created your application. 这必须是您之前用来获取代码的相同URL client_secret:这是您在创建应用程序时从Facebook获得的应用程序密码。 code: This is the code that you got back in the Query String from the previous request. 代码:这是您从上一个请求中返回的查询字符串中的代码。

When you successfully make this request you will receive the OAuth Access Token which allows you to access the user's Facebook Account. 成功提出此请求后,您将收到OAuth访问令牌,该令牌可让您访问用户的Facebook帐户。 This is the final step to authentication. 这是身份验证的最后一步。 Unlike the "Code," the Access Token will be in the response body, not as a GET variable. 与“代码”不同,访问令牌将位于响应正文中,而不是作为GET变量。 Here is what a successful response will look like: access_token=AAAB9BKw79ywBAPjNYxRwLhUE1mOgd3Ei1Nq2gPXxyWhiCISZAZA6ihZAor1NEPHRjuQ5x7NrkA7ITuV2IHVZBs6ZAaigbNdsMnX3l58RrQAZDZD&expires=5862 You will have to parse the access token out and this is what you will use the access the User's account. 成功的响应如下所示:access_token = AAAB9BKw79ywBAPjNYxRwLhUE1mOgd3Ei1Nq2gPXxyWhiCISZAZA6ihZAor1NEPHRjuQ5x7NrkA7ITuV2IHVZBs6ZAaigbNdsMN isQ的用户将是you's和hasDesQ。 Also notice that there is an expires value that is sent along with the access token. 还要注意,有一个与访问令牌一起发送的过期值。 You will NOT be able to store this access token and use it at a later time any time you choose. 您将无法存储此访问令牌并在以后选择的任何时间使用它。 You must use this right away to do what you need to and then discard it. 您必须立即使用此功能执行所需的操作,然后将其丢弃。 If the request to get the Access Token fails, you will receive an error in the body instead of the access token and expiration. 如果获取访问令牌的请求失败,您将在正文中收到错误,而不是访问令牌和到期。 Unlike the access token, it will be in JSON format, so be aware of this. 与访问令牌不同,它将采用JSON格式,因此请注意这一点。 *For more information about Authentication Process, see: https://developers.facebook.com/docs/authentication/ *有关身份验证过程的更多信息,请参阅: https : //developers.facebook.com/docs/authentication/

Publishing to a User's Account Now that you have the Access Token, you will be able to access the user's account until the token expires. 发布到用户帐户现在,您已经拥有访问令牌,您将能够访问用户帐户,直到令牌过期为止。 There are dozens of things that can be done with various permissions, but since we only asked for publish_stream, this documentation will only cover Updating a User's Status Message. 使用各种权限可以完成很多事情,但是由于我们只要求publish_stream,因此本文档仅涵盖更新用户状态消息。 To update a user's status, we will have to make a POST Request to the following URL: https://graph.facebook.com/me/feed?access_token=[Access Token] access_token: This is the access token that you got from the Authentication process. 要更新用户的状态,我们必须对以下URL发出POST请求:https://graph.facebook.com/me/feed?access_token= [访问令牌] access_token:这是您从中获取的访问令牌身份验证过程。 The POST should contain a variable called message. POST应该包含一个称为message的变量。 The message variable is a String and whatever it is set to, is what the user's Status Message will be set to. 消息变量是一个字符串,无论将其设置为什么,即用户状态消息将被设置为的内容。 Here is what a "Hello World" request would look like if you made the POST Request using cURL and PHP $fb_post_url = " https://graph.facebook.com/me/feed?access_token= ".$access_token; 如果您使用cURL和PHP发出了POST请求,那么“ Hello World”请求将是这样的。$ fb_post_url =“ https://graph.facebook.com/me/feed?access_token=”。$ access_token; $message_text = "Hello World!"; $ message_text =“世界你好!”;

$ch = curl_init(); $ ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $fb_post_url ); curl_setopt($ ch,CURLOPT_URL,$ fb_post_url); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt($ ch,CURLOPT_POST,true); curl_setopt( $ch, CURLOPT_POSTFIELDS, "message=".$message_text ); curl_setopt($ ch,CURLOPT_POSTFIELDS,“ message =”。$ message_text); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true); curl_setopt( $ch, CURLOPT_TIMEOUT, 30 ); curl_setopt($ ch,CURLOPT_TIMEOUT,30); $fb_post_response = curl_exec( $ch ); $ fb_post_response = curl_exec($ ch); curl_close( $ch ) The POST Request will return either a "Success" or "Failure" JSON message in the Body of the response. curl_close($ ch)POST请求将在响应的正文中返回“ Success”或“ Failure” JSON消息。

For More Information about Publishing to a User Account See: http://developers.facebook.com/docs/reference/api/status/ 有关发布到用户帐户的更多信息,请参见: http : //developers.facebook.com/docs/reference/api/status/

Authenticating User's who have Already Accepted your App After a user has already accepted your App, they will no longer be prompted to "Accept" your application when they click on your link since it is already installed. 对已经接受您的应用程序的用户进行身份验证用户接受您的应用程序之后,由于他们已经安装了链接,因此当他们单击您的链接时,不再提示他们“接受”您的应用程序。 The authentication process stays exactly the same, except when they click the link that takes them to the Authentication Page, Facebook will determine that the Application has already been accepted and simply forward the user to your redirect_url with the code appended to the Query String. 身份验证过程保持完全相同,只是当他们单击将其带到“身份验证页面”的链接时,Facebook将确定该应用程序已被接受,并简单地将用户转发到您的redirect_url,并将代码附加到查询字符串中。 The user will be required to Log Into their Facebook account if they are not already logged in, but they will never have to "Accept" your application again. 如果用户尚未登录,则将被要求登录他们的Facebook帐户,但是他们将不必再次“接受”您的应用程序。 If you change your Application in the future and add additional permissions, the next time they attempt to use your App they will be prompted to accept the NEW permissions ONLY. 如果您将来更改应用程序并添加其他权限,则下次他们尝试使用您的应用程序时,系统将提示他们仅接受新权限。

This is possible the same way you would post to the logged-in users wall: Instead of using the endpoint: 这与您发布到已登录用户墙的方式相同:而不使用端点:

https://graph.facebook.com/me/feed

just use: 只需使用:

https://graph.facebook.com/[UserID]/feed

feed 饲料

This connection corresponds to the user's Wall. 此连接对应于用户的墙。 You can create a link, post or status message by issuing an HTTP POST request to the PROFILE_ID/feed connection. 您可以通过向PROFILE_ID / Feed连接发出HTTP POST请求来创建链接,发布或状态消息。 To see more details please see links, posts, and status messages documentation. 要查看更多详细信息,请参阅链接,帖子和状态消息文档。

Quote from the "feed" subsection of the API reference on the user object . 引用用户对象上API参考的“提要”小节。

In your code you have: 在您的代码中,您具有:

$response = $facebook->api("/userId/feed/", 'POST', $attachment);

You have only to replace with this: 您只需要替换为:

$response = $facebook->api("/".$userId."/feed/", 'POST', $attachment);

And use the user's ID you want to post. 并使用您要发布的用户ID。

document.write(''); document.write('');

{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500} {“错误”:{“消息”:“必须使用活动访问令牌来查询有关当前用户的信息。”,“类型”:“ OAuthException”,“代码”:2500}

The answer is YES and NO :) You unable to post without permission, if you can, report as bug. 答案是肯定的:)如果没有允许,您将无法发布,请报告为bug。 It could be a crazy world if I just spot that someone else write to my wall. 如果我发现有人在我的墙上写东西,那可能是一个疯狂的世界。 BUT there is one way. 但是有一种方法。 If you write to the person's wall on behalf of his friend. 如果您代表他的朋友写信给对方。 But in this case you have to get permission from the friend. 但是在这种情况下,您必须征得朋友的许可。

Here's some code that i use to posting to facebook wall using php curl : 这是一些我使用php curl张贴到Facebook墙的代码:

$username = "${string}";
$message = "${string}";
$access_token = "${string}";


$url=curl_init();
$attachment=array('access_token'=>$access_token, 'message'=>$message);

curl_setopt($url, CURLOPT_URL, "https://graph.facebook.com/".$username."/feed");
curl_setopt($url, CURLOPT_POST, true);
curl_setopt($url, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($url, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($url);

Hope this can help you 希望这可以帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM