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

查看:283
本文介绍了张贴在使用Facebook的API PHP某人墙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编写一个应用程序,岗位信息上某人的墙上,如果处理不当的用户接受许可被提示在这个时候这样做?

这是我的code:

  $ =连接阵列(
        ACCESS_TOKEN'=> $的access_token,
        '消息'= GT; $消息,
        '名'=> 测试名称,
        '描述'=> 退房新的消息,
        '链接'=> '',
        '图片'=> 测试信息
    );    尝试{
        $响应= $ facebook-> API(/用户ID /饲料/,POST,$附件);
    }赶上(FacebookApiException $ E){    }


解决方案

下面是我关于这个问题写了一个前阵子以作记录。希望它可以帮助你:

与Facebook的图形API创建应用程序
2012年1月27日,

入门的Facebook
要开始建设使用图形API一个Facebook应用程序,首先需要与Facebook进行注册,然后才能开始在 HTTPS编码:/ /developers.facebook.com 。创建帐户或以现有帐户登录后,你需要创建一个新的应用程序(或正确配置的应用程序,如果你使用的是现有的应用程序)。
要创建Facebook应用程序,请 https://developers.facebook.com/apps 并点击创建新的应用程序(或编辑应用程序,如果你已经有一个创建并想配置)。根据设置 - >基本选项卡,您需要,最起码,填写应用程序的显示名称,联系人电子邮件和网站URL。其中,网站URL是最重要的,必须指向你的应用程序将驻留(这可以在以后更新,如果你还不知道)。接下来,进入设置 - >验证对话框选项卡。您不需要填写任何这一点,但它是一个好主意,因为这是用户将看到当他们允许你的应用程序来访问他们的帐户。保存一切,头回 https://developers.facebook.com/apps 。您应该看到您刚才这个页面上配置的应用程序。这两个东西,你将需要复制到移动从这个角度提出是应用程序ID / API密钥和App秘密。您现在可以开始创建你的应用程序。

了解流
你开始编写应用程序之前,最好了解验证用户并得到他们的Facebook帐户安装应用程式的流程。直到安装了应用程序在用户帐户,他们已经给内容发布到自己的帐户应用的明确许可,您将无法做任何事情。好消息是,用户只需要进行身份验证的应用程序一次(除非他们将其卸载)。看看下图,了解流程是如何工作的:

验证用户
要启动身份验证的过程中,你必须建立一个链接,引导用户到你的应用程序。这是你将如何建立你的链接:
https://www.facebook.com/dia​​log/oauth?client_id=[Your API KEY]放大器; REDIRECT_URL = [服务将处理认证]放大器;范围= [权限您需要&安培;显示器=触摸
- CLIENT_ID:这是当您创建的应用程序,你从Facebook得到你的API密钥。
- REDIRECT_URL:这是将处理所有身份验证的服务。它应位于您在设置了Facebook上的应用程序配置的网址。这是URL必须在整个认证过程中保持不变。
- 范围:这是你需要为用户的帐户的任何许可。您应该只问你真正需要的。我们通常只需要publish_stream。对于权限的完整列表,请参阅: https://developers.facebook.com/docs/参考/ API /许可/
- 显示:这是实告诉使用什么样的皮肤。由于我们使用这个移动应用程序,您应该将其设置为接触。如果你没有为移动设备开发,你可以省略这一切都在一起。

当你建立链接,用户点击,他们将在下面,如果他们登录带到页面。如果他们没有登录,系统会提示先登录。如果用户接受你的申请,拒绝你的申请请求,或者无法登录,他们将被引导到你上面指定的REDIRECT_URL。根据用户的此页面上的行动,Facebook将不同的查询字符串追加到REDIRECT_URL时,它会将用户那里。

如果用户点击允许,他们将被发送到您的REDIRECT_URL和查询字符串将包含一个变量,名为code。这是您将用什么来获取,这将让你从App访问他们的帐户的用户的身份验证令牌。如果不允许,用户点击登录失败,或者遇到其他问题,下面的查询字符串将appened您REDIRECT_URL:ERROR_REASON,错误和ERROR_DESCRIPTION。
一个成功的请求,您的重定向URL的一个例子:
=\">http://niobiumstudio.com/appia/fbconnect/auth.php?$c$c=AQDi7fT3whSPJr0O2ECwv494QSSNyrTFK_SGIexEFUGmw5XS8SvzfYiAsxpn0FspQYHkMgaUYH--PS1AnJnCtE-iUdRl6V3Moxfk4Cqz0igZbnkHxWi4Yl_KphXiRkbnCCW_zDqb4W2lfew9sla4FPDUKhXscRuQeI--61uQ0uStb9GwrOH4V94DjGWk1yS-Ffs#=

一旦你传递回你的REDIRECT_URL code GET变量,您就可以与Facebook交流一下,让您的OAuth访问令牌。要做到这一点,你需要建立一个使用code,你刚回来给Facebook的请求。以下是获得访问令牌的要求是这样的:
https://graph.facebook.com/oauth/access_token?client_id=[Your API密钥]放大器; REDIRECT_URL = [服务处理验证]&安培; client_secret = [您的应用程序的秘密]放; code = [用户验收code]
CLIENT_ID:这是当您创建的应用程序,你从Facebook得到你的API密钥。
REDIRECT_URL:这是将处理所有身份验证的服务。它应位于您在设置了Facebook上的应用程序配置的网址。这是URL必须在整个认证过程中保持不变。 这是相同的URL你在上面用于获取code
client_secret:这是你的App秘密code当您创建的应用程序,你从Facebook获得。
code:这是code,你从previous要求回来在查询字符串

当你成功地提出这一要求,您将收到OAuth访问令牌,它允许你访问用户的Facebook帐户。这是向认证的最后一步。不同的是code,访问令牌将在响应体,而不是作为一个GET变量。这是一个成功的响应将是什么样子:
access_token=AAAB9BKw79ywBAPjNYxRwLhUE1mOgd3Ei1Nq2gPXxyWhiCISZAZA6ihZAor1NEPHRjuQ5x7NrkA7ITuV2IHVZBs6ZAaigbNdsMnX3l58RrQAZDZD&expires=5862
你将不得不解析访问令牌了,这是你会用什么样的访问用户的帐户。还要注意,有一个到期是与访问令牌一起发送的值。你将不能够存储这个访问令牌,并在以后使用它,你选择的任何时间。您必须使用这个马上做你需要做什么,然后丢弃它。
如果请求,以获得访问令牌失败,你就会在体内,而不是访问令牌和过期收到一个错误。不同于访问令牌,这将是JSON格式,所以要注意这一点。
*有关身份验证过程的详细信息,请参阅: https://developers.facebook.com/docs/authentication/

发布到用户的帐户
现在,你有访问令牌,你就能直到标记过期访问用户的帐户。有几十个的东西,可以用不同的权限来完成,但由于我们只要求publish_stream,这个文件将只包括更新用户的状态信息。
要更新用户的状态,我们将不得不作出一个POST请求到以下网址:
https://graph.facebook.com/me/feed?access_token=[Access令牌]
的access_token:这是你从认证过程获得访问令牌。
该职位应包含一个称为消息变量。消息变量是一个字符串和任何它设置为,是用户的状态信息将被设置为。
这里是一个Hello World的要求会是什么样子,如果你是用卷曲和PHP POST请求
$ 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,真正的);
curl_setopt($ CH,CURLOPT_POSTFIELDS,消息=$ MESSAGE_TEXT);
curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
curl_setopt($ CH,CURLOPT_TIMEOUT,30);
$ fb_post_response = curl_exec($ CH);
curl_close($ CH)
POST请求将在响应主体返回一个成功或失败JSON消息。

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

验证用户的谁已经接受了您的应用程序
当用户已经接受了您的应用程序,他们将不再提示接受您的应用程序时,他们的点击,因为它已安装。认证过程保持完全一样的,只是当他们点击他们带到认证页面的链接,Facebook将确定该申请已经被接受,并简单地把用户与附加到查询中的code你REDIRECT_URL串。用户将需要登录到自己的Facebook账户,如果它们尚未登录,但他们将永远不会有再接受您的应用程序。如果你改变你的应用程序在未来,并添加额外的权限,他们试图使用你的应用程序,他们将被提示是否接受新的权限下一次只。

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

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.

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:

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.

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.

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#=

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.

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/

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 ) The POST Request will return either a "Success" or "Failure" JSON message in the Body of the response.

For More Information about Publishing to a User Account See: 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. 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天全站免登陆