morteza
morteza

Reputation: 31

oAuth.RequestToken(code) in instasharp does not work and returns null

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 :

  1. Using latest version of Instasharp
  2. Using Https ( in a part of Instagram location it says for some situation if you dont use https instagram may retrn Null for access-token)
  3. in a topic someone said he set the CONTENT TYPE , bu i have no idea where to set content-type in instasharp

Pleas help me to solve this problem.:(

Upvotes: 3

Views: 471

Answers (1)

user8055986
user8055986

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

Related Questions