简体   繁体   English

如何从ac #web服务发布到Facebook墙?

[英]How can I post to a facebook wall from a c# web service?

I've tried a bunch of things and can't figure out how to post content to a facebook wall from a web service. 我尝试了很多东西,无法弄清楚如何通过网络服务将内容发布到Facebook墙上。 Does anyone know if it's even possible? 有谁知道它是否可能? It seems like the only way to post content is to include a sign-in page within a web site, which isn't what I'd like to do. 发布内容的唯一方法似乎是在网站中包含一个登录页面,这不是我想要做的。 For twitter, I was able to tweet a message in under 10 lines of code; 对于twitter,我能够在10行代码中发送一条消息; just add the username/password into a request header and stream the message. 只需将用户名/密码添加到请求标头中并流式传输消息。 I guess what I'm asking is there a similar simple way to do this for facebook? 我想我要问的是有一个类似的简单方法来为Facebook做这个吗? Thanks in advance! 提前致谢!

You need to create an application on Facebook first. 您需要先在Facebook上创建一个应用程序 The important settings when setting this up on facebook are: 在Facebook上设置时的重要设置是:

Connect -> Facebook Connect Settings -> Connect URL 连接 - > Facebook Connect设置 - >连接URL

This url serves two purposes. 此网址有两个用途。 First, that URL will be used as the base for your Cross Domain Communication Channel where you place your xd_receiver.htm file. 首先,该URL将用作放置xd_receiver.htm文件的跨域通信通道的基础。

So if your the url is: http://yoursite.com then you have to have a file at the url http://yoursite.com/xd_receiver.htm which allows the JavaScript Client Library for Facebook to run. 因此,如果您的网址是:http: http://yoursite.com那么您必须在网址http://yoursite.com/xd_receiver.htm上有一个文件,该文件允许FacebookJavaScript客户端库运行。

However, I strongly recommend that you do not use your root url. 然而,我强烈建议你不要用你的根URL。 Choose a subdirectory which you don't link from on your page. 选择您未在页面上链接的子目录。 Something like http://yoursite.com/ex/fb would do nicely (I like the ex for "external" but it's completely subjective). http://yoursite.com/ex/fb这样的东西会做得很好(我喜欢ex为“外部”,但它完全是主观的)。

While you don't necessarily need the JavaScript Client Library to perform the actual posting. 虽然您不一定需要 JavaScript客户端库来执行实际发布。 However, you do need it to give yourself the rights you need to post to the wall. 但是,您确实需要它来为自己提供发布到墙上所需的权利。

Advanced -> Advanced Settings -> Application Type 高级 - >高级设置 - >应用程序类型

This is set to " Desktop ". 这被设置为“ 桌面 ”。

Once your application is set up, take note of the following things (from the main page of your application): 设置好应用程序后,请注意以下事项(从应用程序的主页面):

  1. Your API key 您的API密钥
  2. The url of your xd_receiver.htm file xd_receiver.htm文件的url

With those two things in hand, you want to create a web page on the site that uses the JavaScript Client Library and FBML to give your application the right to post to its wall (or a page's wall). 有了这两件事,你想在网站上创建一个使用JavaScript Client Library和FBML的网页,让你的应用程序有权发布到它的墙(或页面的墙)。

In this page, you want to initialize the JavaScript Client Library using your API key as well as the path to your xd_receiver.htm file. 在此页面中,您希望使用API​​密钥以及xd_receiver.htm文件的路径初始化JavaScript客户端库。 I have an ASP.NET page set up like this: 我有一个像这样设置的ASP.NET页面:

<%@ Page Language="C#"%>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
  <script 
    src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/
      FeatureLoader.js.php" type="text/javascript"></script>
</head>

<body>
  <fb:login-button></fb:login-button>

  <hr>

  <fb:prompt-permission perms="offline_access">Click to grant offline 
    access permission.</fb:prompt-permission>
  <fb:prompt-permission perms="publish_stream">Click to grant publish 
    stream permission.</fb:prompt-permission>
  <fb:prompt-permission perms="read_stream">Click to grant read stream
    permission.</fb:prompt-permission>

  <input type="button" value="Get session key" onclick="
    document.all('apikey').value=FB.Facebook.apiClient.
    get_session().session_key;" />

  <input type="text" size="100px" id="apikey" />

  <script type="text/javascript">
  FB_RequireFeatures(["XFBML"], function () {
    FB.Facebook.init("API Key", "xd_receiver.htm URL");
  });
</script>

</body>

</html>

The two things of note are: 需要注意的两点是:

  • You are requesting the offline_access extended permission . 您正在请求offline_access 扩展权限 This is key to allowing you to post to the wall of the user when they are not online. 这是允许您在不在线时发布到用户墙上的关键。
  • The read_stream permission, I don't know if this is necessary to post to the stream, but it doesn't hurt to have. read_stream权限,我不知道是否有必要发布到流,但它没有伤害。
  • Obviously, the publish_stream permission . 显然,是publish_stream权限

Now, when you go to this page, the JavaScript Client Library will take care of rendering the tags with the fb namespace. 现在,当您转到此页面时,JavaScript客户端库将负责使用fb命名空间呈现标记。

When you first see it, you will see a Facebook Connect button. 当您第一次看到它时,您将看到Facebook Connect按钮。 Click on it and log in using the credentials of the user who's wall you want to post to. 单击它并使用要发布到的墙的用户的凭据登录。

Once you login, the page will be reloaded and the fb:prompt-permission tags will be rendered as links. 登录后,将重新加载页面,并将fb:prompt-permission标记呈现为链接。 Clicking on the links tells Facebook that the user allows the application access to those things about their profile. 点击链接告诉Facebook用户允许应用程序访问有关其个人资料的内容。

After you have granted your application all of the permissions (some dialog boxes might appear along the way), click the "Get session key" button. 在您的应用程序授予所有权限后(可能会出现一些对话框),单击“获取会话密钥”按钮。

This will populate the textbox with a session key. 这将使用会话密钥填充文本框。 Because your application has requested the offline_access, this session key will be recognized by Facebook whenever you use it. 由于您的应用程序已请求offline_access,因此只要您使用此会话密钥,Facebook就会识别该会话密钥。

Now, you have the following pieces of information: 现在,您有以下信息:

  • The application API key 应用程序API密钥
  • The application secret key (which you gain from your application page) 应用程序密钥(您从应用程序页面获得)
  • The id of the user who's page you want to post to (you should be able to get this from your Facebook url) 您想要发布到的页面的用户的ID(您应该可以从您的Facebook网址获取此内容)

At this point, you should be able to use any of the Facebook APIs for .NET. 此时,您应该可以使用任何适用于.NET的Facebook API。 I prefer the Facebook Developer Toolkit . 我更喜欢Facebook Developer Toolkit

Create a simple Windows Forms application with your components of choice and se the application API and the application secret key. 使用您选择的组件创建一个简单的Windows窗体应用程序,然后选择应用程序API和应用程序密钥。 Also, set the session key to the session key from above before you make the call to login in your Windows Forms app. 此外, 在您调用 Windows窗体应用程序中的登录之前,将会话密钥设置为会话密钥。 Set a breakpoint after the login call. 在登录调用后设置断点。

Run your app. 运行你的应用程序 A browser window should show up prompting you to login. 应显示一个浏览器窗口,提示您登录。 Once you login, you should hit your breakpoint. 登录后,您应该点击断点。 At this point, your application secret (if using the Facebook Developer Toolkit) should be different (I call this the client secret . Write this down. This is the fourth, and last piece of information you should need. 此时,您的应用程序秘密(如果使用Facebook Developer Toolkit)应该是不同的 (我称之为客户端秘密 。写下来。这是您应该需要的第四个,也是最后一条信息。

Now you have all the pieces you need: 现在你拥有了所需的所有部分:

  • Application API 应用程序API
  • Client secret 客户秘密
  • Session key 会话密钥
  • User id of the wall you want to post to 要发布到的墙的用户ID

Now, with all of these, when you make your API calls, use the client secret in place of the application secret , as well as your session key, and you shold be able to make calls to write to the wall of the user, (or a page that the user owns, if you have that id) from your service, without any UI logins. 现在,通过所有这些,当您进行API调用时,使用客户端密钥代替应用程序密钥以及会话密钥,并且您可以拨打电话写入用户的墙,(或者用户拥有的页面(如果您有该ID)来自您的服务,没有任何UI登录。

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

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