How can I extend the 2-hour access token to a 60-day access token using the C# SDK? Can someone provide a sample code snippet for this specific section where the code has already been substituted with the 2-hour access token?
Background: Despite trying various solutions, I haven't been able to successfully extend the access token after making the /me call to retrieve user information.
The objective of my app is to allow users to create a post and track likes and comments for a specified period. This requires the 60-day token.
I took over this project from someone who used to request a permanent token. Now, it's up to me to fix it and implement the 60-day token system. I might have some basic follow-up questions, so please bear with me!
Current workflow: Users create a post, click a button, authenticate our app/login to Facebook via the Javascript SDK. After logging in, we perform other tasks in Javascript before transferring data to C# to save in our database (including the token) and make the post. While I can obtain the access token smoothly in JavaScript and use the 2-hour token to fetch user details, extending it results in errors, which are outlined below along with the code snippets causing them.
What I've attempted:
Working: I came across this solution in another post here. var fbClient = new FacebookClient(accessToken);
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("client_id", appId);
parameters.Add("redirect_uri", redirectURI);
parameters.Add("client_secret", appSecret);
parameters.Add("code", accessToken);
var result = fbClient.Get("/me", parameters);
Not working: This method was derived from different sources, including here and the SDK docs. Some suggest including the redirect URL to match the one used during the initial token acquisition, but since we did that in Javascript without a redirect URL, confusion arises. Dictionary parameters2 = new Dictionary();
parameters2.Add("client_id", appId);
//parameters2.Add("redirect_uri", redirectURI);
parameters2.Add("client_secret", appSecret);
//parameters2.Add("code", accessToken);
parameters2.Add("grant_type", "fb_exchange_token");
parameters2.Add("fb_exchange_token", accessToken);
var result2 = fbClient.Get("/oauth/access_token", parameters2);
Error: {Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: a. Line 1, position 1. Error2: If I comment out grant_type and fb_exchange_token and uncomment code and the redirecturi I get the same error as the next method...
Not working: I copied this from another post here and customized the variables to match mine. Unable to get long lived expiration token
dynamic result3 = new ExpandoObject();
try
{
dynamic parameters3 = new ExpandoObject();
parameters3.grant_type = "fb_exchange_token";
parameters3.fb_exchange_token = accessToken;
parameters3.client_id = appId;
parameters3.client_secret = appSecret;
result3 = fbClient.Get("oauth/access_token", parameters);
}
catch (FacebookOAuthException err)
{
result3.error = "Error";
result3.message = err.Message;
}
catch (Exception err)
{
result3.error = "Error";
result3.message = err.Message;
}
Error: {(OAuthException) (OAuthException) Invalid verification code format.}