简体   繁体   English

图形API,C#,可将应用程序中的数据共享到我的墙上

[英]Graph api, C# to share data from app to my wall

i am trying to develop an app which can allow me to share data on Facebook wall. 我正在尝试开发一个可以让我在Facebook墙上共享数据的应用程序。

For eg: consider the Facebook Canvasapp "Run with Friends". 例如:考虑使用Facebook Canvasapp“与朋友一起跑步”。 This is a canvas app using Python and Php. 这是使用Python和Php的画布应用程序。 https://developers.facebook.com/docs/samples/ . https://developers.facebook.com/docs/samples/ I will be able to share data from this app to my wall./me 我将能够从此应用程序将数据共享到我的墙上。

Before doing this v need a access token. 在执行此操作之前,v需要访问令牌。 I was able to get the authorization, accesstoken using Graph api, Oauth2.0, C#, asp.net 我能够使用Graph api,Oauth2.0,C#,asp.net获得授权,访问令牌

 public string AuthorizationLinkGet(bool bUserInfo, bool bFriends, bool bfeed, bool bPhotos, bool bEvents, bool bMessages)
    {
        string url = string.Format("{0}?client_id={1}&redirect_uri={2}", AUTHORIZE, this.ConsumerKey, CALLBACK_URL);
        if (bUserInfo == true || bFriends == true || bfeed == true || bEvents == true || bPhotos == true || bMessages == true)
        {
            url += "&scope=email";

            if (bUserInfo == true)
            {
                url += ",user_about_me,user_interests,user_likes,user_location,user_notes,user_education_history,user_hometown";
            }
            if (bFriends == true)
            {
                url += ",read_friendlists,user_groups";
            }
            if (bfeed == true)
            {
                url += ",read_stream";
            }
            if (bEvents == true)
            {
                url += ",user_events";
            }
            if (bEvents == true)
            {
                url += ",user_photo_video_tags";
            }
            if (bMessages == true)
            {
                url += ",read_mailbox";
            }
        }
        return url;
    }

 <add key="APIKey" value="*************************"/>
<add key="Secret" value="**********************"/>

So now how do I be able to share data from the app onto my FB wall. 因此,现在我如何能够将应用程序中的数据共享到我的FB墙上。 I've tried Sharekit but look like Sharekit is for mobile apps. 我已经尝试过Sharekit,但看起来Sharekit适用于移动应用程序。

  public void AccessTokenGet(string authToken)
    {
        this.Token = authToken;
        string accessTokenUrl = string.Format("{0}?client_id={1}&redirect_uri={2}&client_secret={3}&code={4}",
        ACCESS_TOKEN, this.ConsumerKey, CALLBACK_URL, this.ConsumerSecret, authToken);

        string response = WebRequest(Method.GET, accessTokenUrl, String.Empty);

        if (response.Length > 0)
        {
            //Store the returned access_token
            NameValueCollection qs = HttpUtility.ParseQueryString(response);

            if (qs["access_token"] != null)
            {
                this.Token = qs["access_token"];
            }
        }
    }

does any one know how to develop this canvas app in C# using Graph Api 有谁知道如何使用Graph Api在C#中开发此canvas应用程序

I have no idea abt all the curls and things in Php Also, I see people using REST, facebookservice, facebook SDK. 我不知道Php中的所有卷发和事物。此外,我看到人们在使用REST,facebookservice,facebook SDK。 Where would these fit it? 这些适合哪里?

Thanks 谢谢

Sun 太阳

  1. Ask for the publish stream permission. 要求发布流许可。
  2. You need to POST to https://graph.facebook.com/USER_ID/feed/access_token=lerolero , posting a json like this: 您需要发布到https://graph.facebook.com/USER_ID/feed/access_token=lerolero ,发布一个像这样的json:

    {"name": "name" "link": "http://www.link.com/", "caption": "title", "description": "description", "picture": "http://www.link.com/image.jpg"} {“名称”:“名称”“链接”:“ http://www.link.com/”,“标题”:“标题”,“描述”:“描述”,“图片”:“ http:// www.link.com/image.jpg“}

http://developers.facebook.com/docs/reference/api/ , read the publishing section, you can send a lot of other information. http://developers.facebook.com/docs/reference/api/ ,阅读发布部分,您可以发送许多其他信息。

Edit: 编辑:

I see two possibilities. 我看到两种可能性。

  1. If the facebook integration its not part of your bussiness rules (if you will not use the user information), you can just add the SHARE widget, in this case, the user will se a button, and when he click the button, the facebook authentication page will open, and when the authentication is done, the share widget will popup automatically. 如果facebook集成不是您的业务规则的一部分(如果您不使用用户信息),则只需添加SHARE小部件,在这种情况下,用户将选择一个按钮,当他单击该按钮时,facebook身份验证页面将打开,身份验证完成后,共享小部件将自动弹出。

  2. If you gonna access the user information (if will be somehow attached to the user account), then, when the user clicks the button, you have to check the user account forward a facebook account, if theres not one, you have to open a authentication popup, if the user succeeds to login into facebook, the authentication popup will return you a verification code. 如果您要访问用户信息(如果将以某种方式附加到用户帐户中),则当用户单击按钮时,您必须检查该用户帐户是否转发了一个Facebook帐户,如果没有,则必须打开一个身份验证弹出窗口,如果用户成功登录Facebook,身份验证弹出窗口将返回验证码。 You must exchange the verication code for a access token and attach the access token to the user account in your database. 您必须将验证码交换为访问令牌,并将访问令牌附加到数据库中的用户帐户。 In the authentication step, you have to ask for two special permissions, the stream publish permission and the offline access permission. 在身份验证步骤中,您必须要求两个特殊权限,即流发布权限和脱机访问权限。 The first one is about the resource to publish in the user wall, and the second one its about the access token life cycle (if you dont ask for this permission, the access token will have a short life cycle, and you will need to reauthenticate the user into facebook again and again, we dont want that). 第一个与要在用户墙中发布的资源有关,第二个与访问令牌的生命周期有关(如果您不要求此许可,则访问令牌的生命周期很短,您需要重新进行身份验证用户一次又一次地进入Facebook,我们不希望这样)。

Into the code: 进入代码:

When the user clicks a button, call a ajax function that checks if there's a access token in the user account. 当用户单击按钮时,调用ajax函数以检查用户帐户中是否有访问令牌。 If theres, return the access token (take care here, if you create a ajax that return a access tokem based on a user id, you will have a very bad security issue), if theres not, return false. 如果存在,则返回访问令牌(请注意,如果您创建一个基于用户ID返回访问密钥的ajax,则将遇到非常严重的安全性问题),否则,返回false。

If the code returned is false, call a function that opens a popup with the facebook authentication url. 如果返回的代码为false,则调用一个函数,该函数使用facebook身份验证URL打开一个弹出窗口。 In the facebook authentication popup callback url, just exchange the verification code for a access token and store it in the database. 在facebook身份验证弹出窗口回调URL中,只需将访问令牌的验证码交换并将其存储在数据库中即可。 You need to keep listening the popup.closed state, to know when the process is done and call another ajax function that checks if now theres a access token in the user account or not, if not, the user just closed the popup. 您需要继续侦听popup.closed状态,以了解何时完成该过程,并调用另一个ajax函数,该函数检查用户帐户中现在是否存在访问令牌,如果没有,则用户只是关闭了弹出窗口。

If the code returned is an access token, call a ajax function that post in the user wall, its very easy, like this: 如果返回的代码是访问令牌,则调用在用户墙上发布的ajax函数,这非常简单,如下所示:

string facebookurl = "https://graph.facebook.com/me/feed?access_token=32423"
string parameters = "message=aaaa&link=bbbb";
WebRequest req = WebRequest.Create(facebookurl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(parameters);
req.ContentLength = bytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();

WebResponse res = req.GetResponse();
StreamReader streamReader = new StreamReader(res.GetResponseStream());

If you need any other piece of code, let me know. 如果您需要其他任何代码,请告诉我。

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

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