使用 Facebook API PHP 在某人的墙上张贴 [英] Post on someones wall using Facebook API PHP

查看:20
本文介绍了使用 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?

这是我的代码:

$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:

使用 Facebook Graph API 创建应用2012 年 1 月 27 日

Creating an App with the Facebook Graph API January 27, 2012

Facebook 入门要开始使用 Graph API 构建 Facebook 应用程序,您首先需要在 Facebook 注册,然后才能开始在 https://developers.facebook.com.创建帐户或使用现有帐户登录后,您需要创建一个新应用程序(如果您使用的是现有应用程序,请正确配置该应用程序).要创建您的 Facebook 应用程序,请转到 https://developers.facebook.com/apps 并单击创建新应用程序"(或编辑应用程序",如果您已经创建并希望对其进行配置).在设置->基本选项卡下,您至少需要填写应用程序显示名称、联系人电子邮件和站点 URL.其中,站点 URL 是最重要的,并且必须指向您的应用程序所在的位置(如果您还不知道,可以随时更新).接下来,转到设置"->身份验证对话框"选项卡.您不需要填写任何内容,但这是一个好主意,因为这是用户在允许您的应用访问其帐户时将看到的内容.保存所有内容并返回 https://developers.facebook.com/apps.您应该会在此页面上看到您刚刚配置的应用程序.从这一点开始,您需要向下复制两件事,即 App ID/API Key 和 App Secret.您现在可以开始创建应用程序了.

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. 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). 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. 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). 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. 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. You are now ready to begin creating your application.

了解流程在开始编写应用程序之前,最好先了解对用户进行身份验证并将您的应用程序安装到他们的 Facebook 帐户的流程.在您的应用程序安装在用户帐户上并且他们明确授予应用程序向其帐户发布内容的权限之前,您将无法执行任何操作.好消息是,用户只需验证应用程序一次(除非他们卸载它).查看下图以了解流程的工作原理:

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. 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:

验证用户要启动身份验证过程,您必须构建一个链接,将用户定向到您的应用程序.这是您建立链接的方式:https://www.facebook.com/dialog/oauth?client_id=[您的 API 密钥]&redirect_url=[将处理身份验证的服务]&scope=[您需要的权限]&display=touch- client_id:这是您在创建应用程序时从 Facebook 获得的 API 密钥.- redirect_url:这是将处理您所有身份验证的服务.它应该位于您在使用 Facebook 设置应用程序时配置的站点 URL.这是 URL 必须在整个身份验证过程中保持不变.- 范围:这是用户帐户所需的任何权限.你应该只询问你真正需要的东西.我们通常只需要publish_stream.有关权限的完整列表,请参阅:https://developers.facebook.com/docs/参考/api/权限/- 显示:这是告诉 Facebook 使用什么皮肤.由于我们将其用于移动应用程序,因此您应该将其设置为触摸".如果您不是为移动设备开发,则可以完全忽略这一切.

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. - redirect_url: This is the service that will handle all of your authentication. It should be located at the Site URL that you configured when you set up your application with Facebook. This is URL will have to remain constant throughout the Authentication Process. - 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. 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. Since we are using this for mobile applications, you should set this to "touch." If you are not developing for a mobile device, you may omit this all together.

当用户点击你建立的链接时,如果他们已经登录,他们将被带到下面的页面.如果他们没有登录,他们会被提示先登录.如果用户接受您的申请、拒绝您的申请请求或未能登录,他们将被定向到您在上面指定的 redirect_url.根据用户在此页面上的操作,Facebook 会在将用户发送到该页面时将不同的查询字符串附加到您的 redirect_url.

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. 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.

如果用户点击允许",他们将被发送到您的 redirect_url 并且查询字符串将包含一个名为 code 的变量.这是您将用于为用户获取身份验证令牌的内容,这将允许您从应用程序访问他们的帐户.如果用户单击不允许"、无法登录或出现其他问题,则会将以下查询字符串附加到您的 redirect_url:error_reason、error 和 error_description.成功请求重定向 URL 的示例:=> http://niobiumstudio.com/appia/fbconnect/auth.php?code=AQDi7fT3whSPJr0O2ECwv494QSSNyrTFK_SGIexEFUGmw5XS8SvzfYiAsxpn0FspQYHkMgaUYH--PS1AnJnCtE-iUdRl6V3Moxfk4Cqz0igZbnkHxWi4Yl_KphXiRkbnCCW_zDqb4W2lfew9sla4FPDUKhXscRuQeI--61uQ0uStb9GwrOH4V94DjGWk1yS-Ffs# =

If the user clicks "Allow", they will be sent to your redirect_url and the query string will contain a variable called 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. 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#=

一旦您将代码 GET 变量传递回您的 redirect_url,您就可以与 Facebook 交换它以获取您的 OAuth 访问令牌.为此,您需要使用刚刚返回的代码向 Facebook 发出请求.获取访问令牌的请求如下所示:https://graph.facebook.com/oauth/access_token?client_id=[您的 API 密钥]&redirect_url=[处理身份验证的服务]&client_secret=[您的应用程序秘密]&code=[用户接受代码]client_id:这是您在创建应用程序时从 Facebook 获得的 API 密钥.redirect_url:这是将处理您所有身份验证的服务.它应该位于您在使用 Facebook 设置应用程序时配置的站点 URL.这是 URL 必须在整个身份验证过程中保持不变.这必须与您在上面用于获取代码的网址相同client_secret:这是您在创建应用程序时从 Facebook 获得的应用程序密码.代码:这是您从上一个请求返回的查询字符串中的代码.

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. To do this, you will need to build a request to Facebook using the code you just got back. 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. redirect_url: This is the service that will handle all of your authentication. It should be located at the Site URL that you configured when you set up your application with Facebook. This is URL will have to remain constant throughout the Authentication Process. 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. code: This is the code that you got back in the Query String from the previous request.

当您成功提出此请求时,您将收到 OAuth 访问令牌,它允许您访问用户的 Facebook 帐户.这是身份验证的最后一步.与代码"不同,访问令牌将在响应正文中,而不是作为 GET 变量.以下是成功响应的样子:access_token=AAAB9BKw79ywBAPjNYxRwLhUE1mOgd3Ei1Nq2gPXxyWhiCISZAZA6ihZAor1NEPHRjuQ5x7NrkA7ITuV2IHVZBs6ZAaigbNdsMnX3l58RrQAZD2ZD&您必须解析访问令牌,这就是您将使用访问用户帐户的内容.另请注意,有一个随访问令牌一起发送的 expires 值.您将无法存储此访问令牌并在以后选择任何时间使用它.您必须立即使用它来做您需要做的事情,然后将其丢弃.如果获取访问令牌的请求失败,您将在正文中收到错误,而不是访问令牌和过期时间.与访问令牌不同,它将采用 JSON 格式,因此请注意这一点.*有关身份验证流程的更多信息,请参阅:https://developers.facebook.com/docs/authentication/

When you successfully make this request you will receive the OAuth Access Token which allows you to access the user's Facebook Account. This is the final step to authentication. Unlike the "Code," the Access Token will be in the response body, not as a GET variable. 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. 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. *For more information about Authentication Process, see: https://developers.facebook.com/docs/authentication/

发布到用户的帐户现在您拥有访问令牌,您将能够访问用户的帐户,直到令牌过期.有很多事情可以通过各种权限完成,但由于我们只要求 publish_stream,因此本文档将仅涵盖更新用户的状态消息.要更新用户的状态,我们必须向以下 URL 发出 POST 请求:https://graph.facebook.com/me/feed?access_token=[访问令牌]access_token:这是您从身份验证过程中获得的访问令牌.POST 应该包含一个名为 message 的变量.消息变量是一个字符串,无论它被设置为什么,用户的状态消息都将被设置为什么.如果您使用 cURL 和 PHP 发出 POST 请求,Hello World"请求将如下所示$fb_post_url = "https://graph.facebook.com/me/feed?access_token=".$access_token;$message_text = "Hello World!";

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. 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. The POST should contain a variable called 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; $message_text = "Hello World!";

$ch = curl_init();curl_setopt( $ch, CURLOPT_URL, $fb_post_url );curl_setopt( $ch, CURLOPT_POST, true );curl_setopt( $ch, CURLOPT_POSTFIELDS, "message=".$message_text );curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );curl_setopt( $ch, CURLOPT_TIMEOUT, 30 );$fb_post_response = curl_exec( $ch );curl_close( $ch )POST 请求将在响应正文中返回成功"或失败"JSON 消息.

$ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $fb_post_url ); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS, "message=".$message_text ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_TIMEOUT, 30 ); $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.

有关发布到用户帐户的更多信息,请参阅:http://developers.facebook.com/docs/reference/api/status/

For More Information about Publishing to a User Account See: http://developers.facebook.com/docs/reference/api/status/

验证已接受您的应用程序的用户在用户已经接受您的应用程序后,当他们点击您的链接时将不再提示他们接受"您的应用程序,因为它已经安装.身份验证过程保持完全相同,除了当他们单击将他们带到身份验证页面的链接时,Facebook 将确定该应用程序已被接受,并简单地将用户转发到您的 redirect_url,并将代码附加到查询字符串.如果用户尚未登录,则需要登录他们的 Facebook 帐户,但他们将永远不必再次接受"您的应用程序.如果您以后更改您的应用程序并添加其他权限,则他们下次尝试使用您的应用程序时,系统会提示他们仅接受新权限.

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. 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. 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.

这篇关于使用 Facebook API PHP 在某人的墙上张贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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