Engr Nouman Engr Nouman - 2 months ago 24
C# Question

convert short term user access token to long term user access token using c sharp

I got short term user access token in the code below.

Kindly guide how should I convert and get long term user access token.

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;

Answer

server side 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;
    }

client side.

   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);
        });