wpf Facebook 与 C# 桌面应用程序的集成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16443948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Facebook integration with C# desktop application
提问by Kev
im just going to dive straight in and give you a little background on what im trying to do, what i've tried, and the obstacles in my way. so here goes..
我只是要直接深入,并给你一些关于我尝试做的事情、我尝试过的事情以及我前进道路上的障碍的背景知识。所以在这里..
MY GOAL
To post to a facebook profile or wall from a desktop application.
The desktop application will be used by individual users who have their own facebook account.
The aim is to create each user their own facebook app and get the app id and app secret.
And then use the app id and app secret in the desktop application ( Saved somewhere in database or config )
to allow the user of the desktop application to post to their profile without having to enter their email address and password to login first
我的目标
从桌面应用程序发布到 Facebook 个人资料或墙。
桌面应用程序将由拥有自己的 Facebook 帐户的个人用户使用。
目的是为每个用户创建他们自己的 Facebook 应用程序并获取应用程序 ID 和应用程序机密。
然后在桌面应用程序中使用应用程序 id 和应用程序机密(保存在数据库或配置中的某处)以允许桌面应用程序的用户发布到他们的个人资料,而无需先输入他们的电子邮件地址和密码来登录
So in summary, 1 facebook app per facebook account.
The app settings will be saved in each users desktop application
Post to their own facebook via their own facebook app without logging in
所以总而言之,每个 facebook 帐户 1 个 facebook 应用程序。
应用程序设置将保存在每个用户的桌面应用程序中 通过他们自己的 Facebook 应用程序发布到他们自己的 Facebook 应用程序,无需登录
The main emphasis here being that the user does not have to log in manually via a window or browser.
这里的主要重点是用户不必通过窗口或浏览器手动登录。
I have created my facebook app and have set it to live status.. i have also added in all possible permissions and extended permissions in the settings ( Within facebook ) just to make sure i wasnt missing anything. So I do have my AppID and App secret.. I also have my Client Token which it says to use in place of the app secret for 'auth methods'. I use this to get my access token. I tried the app secret and it doesnt return the access token
我已经创建了我的 facebook 应用程序并将其设置为实时状态。我还在设置中添加了所有可能的权限和扩展权限(在 facebook 内),以确保我没有遗漏任何东西。所以我确实有我的 AppID 和应用程序机密。我也有我的客户端令牌,它说用来代替“身份验证方法”的应用程序机密。我用它来获取我的访问令牌。我尝试了应用程序机密,但它没有返回访问令牌
MY ATTEMPTS :
我的尝试:
C# FACEBOOK SDKSo, i started with and am still trying to use the c# sdk
I can retrieve my access token but cannot post.
C# FACEBOOK SDK因此,我开始使用并且仍在尝试使用 c# sdk
我可以检索我的访问令牌但无法发布。
I get he below errors all the time with whatever i try... these are just 2 of the many code examples i have tried.
(OAuthException - #2500) An active access token must be used to query information about the current user.
无论我尝试什么,我都会让他始终低于错误......这些只是我尝试过的许多代码示例中的两个。
(OAuthException - #2500) 必须使用活动访问令牌来查询有关当前用户的信息。
dynamic r = fb.Post("me/feed", new { message = "My seconds wall post using Facebook C# SDK" });
dynamic r = fb.Post("me/feed", new { message = "My seconds wall post using Facebook C# SDK" });
(OAuthException - #190) The client token cannot be used for this API
(OAuthException - #190) 客户端令牌不能用于此 API
dynamic r = fb.Post("kevin.maguire.965/feed", new { message = "My second wall post using Facebook C# SDK" });
dynamic r = fb.Post("kevin.maguire.965/feed", new { message = "My second wall post using Facebook C# SDK" });
I read the following extract from the below link which states my access token is an app token and i need a access token for a user or page? Need Help on OAuthException Code 2500Error 2500 means you have no access token or an app access token but are trying to access /me/ - 'me' is a placeholder for 'current user or page ID' so won't be valid without an access token for a user or page
我从以下链接中阅读了以下摘录,其中指出我的访问令牌是一个应用程序令牌,我需要用户或页面的访问令牌? 需要有关 OAuthException 代码 2500错误 2500 的帮助意味着您没有访问令牌或应用程序访问令牌,但正在尝试访问 /me/ - “me”是“当前用户或页面 ID”的占位符,因此如果没有用户或页面的访问令牌
So, i have tried to get the userID back using the following Answer ( Facebook C# SDK Get Current User)
因此,我尝试使用以下答案(Facebook C# SDK Get Current User)取回用户 ID
var fb = new FacebookClient("access_token");
dynamic result = fb.Get("me", new [] { fields = "id" });
var userId = result.id;
I get the access token which i assume is the app token and not the user token
我得到了我认为是应用程序令牌而不是用户令牌的访问令牌
dynamic result = fb.Get("oauth/access_token", new
{
client_id = this.ApplicationId,
client_secret = this.AppSecret,
grant_type = "client_credentials"
});
fb.AccessToken = result.access_token;
So i have no idea at this moment in time how to post to my profile
所以我现在不知道如何发布到我的个人资料
I am able to achieve the above using twitter where i can use the secret, token, id etc ... they provide and I can successfully post to my twitter account from a desktop application WITHOUT logging into my twitter account.
我能够使用 twitter 实现上述目标,在那里我可以使用秘密、令牌、id 等......他们提供,我可以成功地从桌面应用程序发布到我的 twitter 帐户,而无需登录我的 twitter 帐户。
Another user has also found it quite easy to post to twitter without any real issues. ( facebook c# sdk getting started)
另一位用户也发现在 twitter 上发帖很容易,没有任何实际问题。( facebook c# sdk 入门)
He also seems to have had success which i have not using the same code - this code was uses in June 2012 so there could have been breaking changes released since
then.
I got the message : (OAuthException - #2500) An active access token must be used to query information about the current user... when i used the sdk.
When i tried to get the access token using a web request and then pass that token to the sdk object to create a facebookclient i got this message
(OAuthException - #190) Invalid OAuth access token signature.
他似乎也取得了成功,但我没有使用相同的代码 - 这段代码是在 2012 年 6 月使用的,因此从那时起可能会发布重大更改。
我收到消息:(OAuthException - #2500) 必须使用活动访问令牌来查询有关当前用户的信息...当我使用 sdk 时。当我尝试使用 Web 请求获取访问令牌,然后将该令牌传递给 sdk 对象以创建 facebookclient 时,我收到此消息 (OAuthException - #190) 无效的 OAuth 访问令牌签名。
WebRequest request = WebRequest.Create("https://graph.facebook.com/oauth/access_token? grant_type=client_credentials&client_id=999999999999999&client_secret=edefefr8e09r8e08r080r8er0e");
request.Method = "POST";
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string accesstoken = reader.ReadToEnd();
MessageBox.Show("accesstoken")
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
var fb = new FacebookClient(accesstoken);
dynamic parameters = new ExpandoObject();
parameters.message = "test";
dynamic result = fb.Post("me/feed", parameters);
var id = result.id;
Obviously in the code above i changed the id and secret to dummy values.
显然,在上面的代码中,我将 id 和 secret 更改为虚拟值。
So basically folks...the above links and above code are only a pinch of what I have tried to date in the last few days... i've basically ran out of options and am missing something here which could be averting my attention easily.. or maybe not :) i dont know.
所以基本上伙计们......上面的链接和上面的代码只是我在过去几天里尝试过的一小部分......很容易.. 或者可能不是 :) 我不知道。
If any one would even have a simple windows form, or wpf window application example using the c# sdk or using restsharp or just using WebRequest object with 'POST' method then I would be eternally greatful.
如果任何人甚至有一个简单的 Windows 窗体,或者使用 c# sdk 或使用 restsharp 的 wpf 窗口应用程序示例,或者只是使用带有“POST”方法的 WebRequest 对象,那么我将永远伟大。
Just to iterate again that it is a desktop application and not ASP.net .
再次重申它是桌面应用程序而不是 ASP.net 。
Many thanks folks for your attention and time.
非常感谢大家的关注和时间。








采纳答案by Jurgen Camilleri
From what I can see you are supplying an incorrect access token. Since you haven't provided the code with regards to obtaining the access token, may I suggest you take a look at this linkwhich explains how to build a Facebook application, including obtaining an access token via a WebBrowser control.
据我所知,您提供了不正确的访问令牌。由于您尚未提供有关获取访问令牌的代码,我建议您查看此链接,该链接解释了如何构建 Facebook 应用程序,包括通过 WebBrowser 控件获取访问令牌。
EDIT:You are supplying the app access token but are trying to post as a user. For this operation you need a user access token which you can obtain by following the steps in the link above.
编辑:您正在提供应用程序访问令牌,但正在尝试以用户身份发布。对于此操作,您需要一个用户访问令牌,您可以按照上面链接中的步骤获取该令牌。

