使用c sharp将短期用户访问令牌转换为长期用户访问令牌

时间:2015-04-14 09:01:56

标签: c# .net json facebook facebook-graph-api

我在下面的代码中获得了短期用户访问令牌。

请指导我应该如何转换并获得长期用户访问令牌。

var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new
{
    client_id = "done",
    client_secret = "done",
    grant_type = "client_credentials"
});

fb.AccessToken = result.access_token;
Label1.Text = fb.AccessToken;

2 个答案:

答案 0 :(得分:1)

服务器端webservice1.acmx

 [WebMethod]
    public string getlonToken(string accessToken)
    {
        var fb = new FacebookClient(accessToken);
        dynamic result = fb.Get("oauth/access_token", new
            {
                client_id = yourid,
                client_secret = "yoursec",
                grant_type = "fb_exchange_token",
                fb_exchange_token = accessToken,
                scope = "user_about_me,friends_about_me,user_activities,friends_activities,user_birthday,friends_birthday,user_checkins,friends_checkins,user_education_history,friends_education_history,user_events,friends_events,user_groups,friends_groups,user_hometown,friends_hometown,user_interests,friends_interests,user_likes,friends_likes,user_location,friends_location,user_notes,friends_notes,user_photos,friends_photos,user_relationships,friends_relationships,user_relationship_details,friends_relationship_details,user_religion_politics,friends_religion_politics,user_status,friends_status,user_videos,friends_videos,user_website,friends_website,email,manage_pages,publish_stream,read_stream,read_page_mailboxes,read_insights,ads_management"
            });

        fb.AccessToken = (string)result["access_token"];
        return fb.AccessToken;
    }

客户端。

   function statusChangeCallback(response) {
        console.log('statusChangeCallback');
        console.log(response);

        if (response.status === 'connected') {

            {
              event.preventDefault();
                FB.login(function (response) {
                    if (response.authResponse) {
                        var profileId = response.authResponse.userID;
                        var accessToken = response.authResponse.accessToken;
                        var e = response.authResponse.accessToken;
                        document.getElementById('token').innerHTML = e;
                        var profileName = "";
                        var pagesList = "";
                        var isPage = 0;
                        var type = "fb";

                        $.ajax({
                            url: "WebService1.asmx/getlonToken",
                            type: "POST",
                            dataType: 'json',
                            data: '{accessToken:"' + accessToken + '"}',
                            contentType: "application/json; charset=utf-8",
                            async: true,
                            success: function (response) {
                                accessToken = response.d;
                                document.getElementById('status').innerHTML = accessToken;
                            },
                            error: function (e) {
                                alert('Error' + e);
                            }
                        });

                  }
                },  { scope: 'user_about_me,friends_about_me,user_activities,friends_activities,user_birthday,friends_birthday,user_education_history,friends_education_history,user_events,friends_events,user_groups,friends_groups,user_hometown,friends_hometown,user_interests,friends_interests,user_likes,friends_likes,user_location,friends_location,user_notes,friends_notes,user_photos,friends_photos,user_relationships,friends_relationships,user_relationship_details,friends_relationship_details,user_status,friends_status,user_videos,friends_videos,user_website,friends_website,email,manage_pages,publish_stream,read_stream,read_page_mailboxes,read_insights, read_mailbox' });

            }
        }
        else if (response.status === 'not_authorized') {
            document.getElementById('status').innerHTML = 'Please log ' +
                 'into this app.';
        } else {

            document.getElementById('status').innerHTML = 'Please log ' +
        'into Facebook.';
        }
    }

  FB.getLoginStatus(function (response) {

            statusChangeCallback(response);
        });

答案 1 :(得分:0)

这就是我这样做的方式,我不是说它是最好的,但它运作正常。

public void FacebookShortToLongToken(string shortToken)
        {
            FacebookClient User = new FacebookClient(shortToken);
            string url = "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id="
                    + {Your app id}
                    + "&client_secret=" + {Your app secret id} 
                    + "&fb_exchange_token=" + shortToken;                   
            JObject LongTokenResponse = JObject.Parse(User.Get(url).ToString());
            string LongLiveToken = LongTokenResponse["access_token"].ToString();
        }