Reputation: 31
I have an ASP.NET MVC5 application that use instagram via Instasharp. My app worked good even 2 weeks after being approved by Instagram. But suddenly stopped working and by checking line by line of codes using instasharp I found that after receiving Code from instagram when i want te get Access-Code by running code below I get null Value from instagram.
...
var config = InstagramInfo.Info.Config;
var auth = new OAuth(config);
var instagramauthInfo = await auth.RequestToken(code);
....
aut.RequestToken(code) returns null. Even When i use OAuth.ResponseType.Token instad of code , It completely returns null from instagram after authenticating and redirecting to RedirectUrl. Solutions I have tried but didnt help me are :
Pleas help me to solve this problem.:(
Upvotes: 3
Views: 471
Reputation:
Try to check your code according to https://github.com/InstaSharp/InstaSharp/tree/master/src/InstaSharp.Sample.Mvc and change OAuth method. It should works.
public async Task<ActionResult> OAuth(string code)
{
var values = new Dictionary<string, string>
{
{ "client_id", config.ClientId },
{ "client_secret", config.ClientSecret },
{ "grant_type", "authorization_code" },
{ "redirect_uri", config.RedirectUri },
{ "code", code }
};
var content = new FormUrlEncodedContent(values);
var response = await new HttpClient().PostAsync("https://api.instagram.com/oauth/access_token", content);
if (!response.IsSuccessStatusCode)
throw new Exception("Auth failed");
var responseString = await response.Content.ReadAsStringAsync();
dynamic data = System.Web.Helpers.Json.Decode(responseString);
var oauthResponse = new OAuthResponse
{
AccessToken = data.access_token,
User = new InstaSharp.Models.UserInfo
{
FullName = data.user.full_name,
Id = long.Parse(data.user.id),
ProfilePicture = data.user.profile_picture,
Username = data.user.username
}
};
Session.Add("InstaSharp.AuthInfo", oauthResponse);
return RedirectToAction("Index");
}
Upvotes: 0