instasharp中的oAuth.RequestToken(code)无法使用并返回null。

3

我有一个使用Instasharp通过Instagram的ASP.NET MVC5应用程序。申请被Instagram批准后的2周内,我的应用程序运行良好。但是突然停止工作,并且在使用instasharp逐行检查代码时,我发现在从instagram接收代码后,当我想要通过运行下面的代码获取Access-Code时,我从instagram得到了空值。

 ...
var config = InstagramInfo.Info.Config;
            var auth = new OAuth(config);
            var instagramauthInfo = await auth.RequestToken(code);
....

aut.RequestToken(code)返回null。 即使我使用OAuth.ResponseType.Token代替code,它在将用户验证并重定向到RedirectUrl后仍然从Instagram完全返回null。 我尝试过的解决方法但没有帮助我的是:
1. 使用最新版本的Instasharp 2. 使用Https(在Instagram的某些位置中,如果您不使用https,则可能会导致Instagram返回访问令牌为Null) 3. 在一个主题中,有人说他设置了CONTENT TYPE,但我不知道在instasharp中应该在哪里设置content-type 请帮助我解决这个问题。:(
1个回答

0

尝试根据https://github.com/InstaSharp/InstaSharp/tree/master/src/InstaSharp.Sample.Mvc检查您的代码,并更改OAuth方法。它应该可以工作。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接