【问题标题】:Retrieve instagram access token on page load在页面加载时检索 Instagram 访问令牌
【发布时间】:2014-07-16 17:33:21
【问题描述】:

我想在我的网站上显示我的用户提要,我打算在每次用户访问该页面时验证我自己的用户帐户,并以这种方式购买用户必须登录到他的 instagram 帐户.

我的问题是我很难通过 HttpWebRequest 检索 instagram 访问令牌。

请参阅以下非工作代码示例:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.instagram.com/oauth/authorize?client_id=xxxxxxxxxxxxxxxxxxxxxx&redirect_uri=http://mywebsite.com&response_type=token");
    request.Method = "POST";
    request.AllowAutoRedirect = false;

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    string redirectUrl = response.ResponseUri.ToString();

    HttpContext.Current.Response.Write(redirectUrl);
    HttpContext.Current.Response.End();

如果我将 url 粘贴到浏览器中,我会重定向到 http://mysite.com/#access_token=xxxxxxxxxxxxxx,一切似乎都很好,但是当我尝试执行上面的代码时,由于在最终到达网址。

任何帮助将不胜感激..

【问题讨论】:

  • 你让这段代码工作了吗?现在在我的代码中遇到了完全相同的问题,但找不到解决方案..

标签: c# instagram


【解决方案1】:

我推荐你使用Instasharp 库。 InstaSharp 是一个 C# 库,它封装了 Instagram API,使使用 Instagram 数据编写应用程序变得容易。它有一个非常简单的方法来为用户获取访问令牌。检查它的 API。

【讨论】:

    【解决方案2】:

    很遗憾,目前提供的 Instasharp 文档存在一些错误。 IE。当这样的类不存在时,文档说 OAuthInfo。 这是一些对我有用的代码。 请注意,您似乎根本不需要传递用户对象(不知道为什么仍然需要) 另请注意,经过身份验证和未经身份验证的方法允许您传递不同的参数,计数是最重要的参数。我注意到,无论您通过多少计数,都会返回任意数量的结果,例如33 表示已认证,13 表示同一搜索词已认证。 InstagramResult 是我的对象包装类,Config 保存 InstagramAuthorisationModel,InstagramAuthorisationModel 保存在注册开发者帐户时创建的静态密钥。

     public class InstagramService : IInstagramService
     ...
     public InstagramConfig Config
        {
            get{return new InstagramConfig("https://api.instagram.com/v1", "https://api.instagram.com/oauth", InstagramAuthorisationModel.ApplicationId, InstagramAuthorisationModel.Secret, InstagramAuthorisationModel.RedirectUri);}
        }
      private AuthInfo UserAuthInfo()
        {
            return new AuthInfo()
             {
                 // User =new UserInfo(){},
                 Access_Token = GetInstagramAccessToken()
             };
        }
    
       public string GetInstagramAccessToken()
        {
            return _socialMediaRepository.GetInstagramAccessToken(_userApiKey);
        }
    
       public List<InstagramResult> Search(string searchTag, int count)
        {
            var auth = UserAuthInfo();
            var tags = new InstaSharp.Endpoints.Tags.Authenticated(Config, auth); 
            var searchresult = tags.Recent(searchTag);
            return searchresult.Data.Select(media => new InstagramResult()
            {
                Media = media,
                image = media.Images.LowResolution.Url
            })
            .ToList();
        }
    

    ..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-05
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      相关资源
      最近更新 更多