【问题标题】:oAuth.RequestToken(code) in instasharp does not work and returns nullinstasharp 中的 oAuth.RequestToken(code) 不起作用并返回 null
【发布时间】:2016-08-17 08:23:48
【问题描述】:

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

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

aut.RequestToken(code) 返回 null。 即使当我使用 OAuth.ResponseType.Token instad of code 时,在验证并重定向到 RedirectUrl 后,它也会从 instagram 完全返回 null。 我尝试过但没有帮助的解决方案是:

  1. 使用最新版本的 Instasharp
  2. 使用 Https(在 Instagram 位置的一部分中,如果您不使用 https instagram 可能会为访问令牌返回 Null)
  3. 在一个主题中有人说他设置了内容类型,但我不知道在 instasharp 中在哪里设置内容类型

请帮我解决这个问题。:(

【问题讨论】:

    标签: oauth instagram instasharp


    【解决方案1】:

    尝试根据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");
          }
    

    【讨论】:

      猜你喜欢
      • 2022-08-11
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      相关资源
      最近更新 更多