Fla*_*ira 2 c# asp.net facebook asp.net-mvc-3
我假装将我的网站与facebook集成,当用户与Web应用程序交互时,想要自动发布(在特定的Facebook帐户上).
有没有办法让这个操作像一个webservice方式?,验证和调用一个网址,发布我直接在Facebook墙上发送的信息?
我正在使用asp.net mvc3 C#我找到了一个facebook开发者工具包库,这是正确的启动方式或我应该怎么做?
什么是必要的只是在Facebook帐户上自动写一个帖子,例如当我在我的网站上写一篇新文章(新闻)时,它将自动发布在fb上.
有什么想让我开始吗?
我做了类似的事情,当用户点击我的mvc应用程序上的"共享"按钮时,它会在他的墙上发布一些内容.使用oauth对话框的问题是,它会将浏览器重定向到Facebook站点,以便用户登录并接受应用程序权限.
在"分享"按钮上,我将其链接到此网址:
<a href=""https://www.facebook.com/dialog/oauth?client_id=[YOUR_APP_ID]&redirect_uri=[THE_REDIRECT_PAGE]/&scope=publish_stream"">
<img src='@Url.Content("~/Images/facebook_share.png")' alt="Share on Facebook!" style="height:28px" />
</a>
Run Code Online (Sandbox Code Playgroud)
YOUR_APP_ID是您的Facebook应用程序ID.THE_REDIRECT_PAGE是您网站上的公共页面,一旦用户登录并接受了权限,Facebook就会自动重定向.当facebook重定向时,它会向其附加一个名为"code"的查询字符串参数.注意:重定向页面必须以"/"结尾,它不能以文档结尾,否则它不起作用!
一旦用户接受了您的请求,您必须向facebook询问另一个代码,称为访问代码,用于在用户的墙上发布.
此代码位于重定向页面上:
public ActionResult Index(string code)
{
string fbAuthCode = Request["code"]; //The authorization code.
string fbAppId = "XXXXXXX"; //Your fb application id.
string fbSecretAppId = "XXXXXXXXXXXXXXXXXXXXX"; //Your fb secret app id, it is found on the fb application configuration page.
string redirectUrl = string.Format("[THE_REDIRECT_PAGE]", locationPointId, entryLocationId); //The redirect url. THIS MUST BE THE EXACT SAME REDIRECT URL USED ON THE JAVASCRIPT LINK!
string fbUrl = "https://graph.facebook.com/oauth/access_token?client_id=" + fbAppId + "&redirect_uri=" + redirectUrl + "&client_secret=" + fbSecretAppId + "&code=" + fbAuthCode; //Url used to post.
string accessToken = string.Empty;
try
{
WebClient client = new WebClient();
using (Stream stream = client.OpenRead(fbUrl))
using (StreamReader reader = new StreamReader(stream))
{
accessToken = reader.ReadToEnd().Split('&')[0].Replace("access_token=", string.Empty);
reader.Close();
}
}
catch (Exception ex)
{
throw new Exception("An error ocurred while trying to get the fb token in " + fbUrl, ex);
}
Run Code Online (Sandbox Code Playgroud)
获得访问令牌后,您可以发布到用户墙:
string postUrl = "https://graph.facebook.com/me/feed";
string postParameters;
postParameters = string.Format("message={0}&picture={1}&name={2}&caption={2}&description={3}&link={4}&access_token={5}",
"[Message]",
"[PictureUrl]",
"[Name]",
"[Caption]",
"[Link]",
accessToken);
try
{
System.Net.WebRequest req = System.Net.WebRequest.Create(postUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postParameters);
req.ContentLength = bytes.Length;
using (System.IO.Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length); //Push it out there
os.Close();
using (WebResponse resp = req.GetResponse())
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
ViewBag.PostResult = sr.ReadToEnd().Trim();
sr.Close();
}
os.Close();
}
}
catch (Exception ex)
{
throw new Exception("An error ocurred while posting data to the user's wall: " + postUrl + "?" + postParameters, ex);
}
return RedirectToAction(XXXXXXXXXXXxx....); //Then i redirect to another page.
Run Code Online (Sandbox Code Playgroud)
您可以在异常中看到我抛出已发布的URL(用于调试目的).使用该URL,您通常可以访问facebook Graph API Explorer或Linter并检查实际错误.
我不知道这是不是你想要的,但希望它给你一个启动...我已经用这几天挣扎了,因为开放图上的facebook文档还不是很好,至少对于我们来说不要用curl :)
https://developers.facebook.com/docs/opengraph/tutorial/ https://developers.facebook.com/docs/opengraph/
希望能帮助到你.公吨.
| 归档时间: |
|
| 查看次数: |
10923 次 |
| 最近记录: |