C# 在页面加载时检索instagram访问令牌

C# 在页面加载时检索instagram访问令牌,c#,instagram,C#,Instagram,我想在我的网站上显示我的用户源,我想做的是在每次用户访问页面时验证我自己的用户帐户,这样就可以购买用户必须登录其instagram帐户的通行证 我的问题是,我很难通过HttpWebRequest检索instagram访问令牌 请参阅以下非工作代码示例: HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.instagram.com/oauth/authorize?client_id=xxx

我想在我的网站上显示我的用户源,我想做的是在每次用户访问页面时验证我自己的用户帐户,这样就可以购买用户必须登录其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粘贴到浏览器中,我会得到一个重定向,一切看起来都很好,但是当我尝试执行上面的代码时,我无法检索到正确的uri,因为在最终url之前有一些中间重定向


任何帮助都将非常感谢。

我建议您使用图书馆。InstaSharp是一个C#库,它封装了Instagram API,使使用Instagram数据编写应用程序变得非常简单。它有一个非常简单的方法为用户获取访问令牌。检查其API。

不幸的是,当前提供的Instasharp文档有一些错误。也就是说,当这样一个类不存在时,文档中会显示OAuthInfo。 下面是一些适合我的代码。 请注意,您似乎根本不需要传递用户对象(不确定为什么需要这样做) 还要注意,已验证和未验证的方法允许传递不同的参数,count是最重要的参数。我注意到,无论您通过多少次,都会返回任意数量的结果,例如,对于同一搜索项,对于已验证,返回33次,对于已验证,返回13次。InstagramResult是我的对象包装类,Config保存InstagramaAuthorisationModel,InstagramaAuthorisationModel保存注册开发人员帐户时创建的静态密钥

 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();
    }
公共类InstagramService:IInstagramService
...
公共InstagramConfig配置
{
获取{返回新的InstagramConfig(“https://api.instagram.com/v1", "https://api.instagram.com/oauth“,instagramauthoritionmodel.ApplicationId,instagramauthoritionmodel.Secret,instagramauthoritionmodel.RedirectUri);}
}
私有AuthInfo UserAuthInfo()
{
返回新的AuthInfo()
{
//User=new UserInfo(){},
Access_Token=GetInstagramAccessToken()
};
}
公共字符串GetInstagramAccessToken()
{
return _socialMediaRepository.GetInstagramAccessToken(_userApiKey);
}
公共列表搜索(字符串搜索标记,整数计数)
{
var auth=UserAuthInfo();
var tags=new InstaSharp.Endpoints.tags.Authenticated(Config,auth);
var searchresult=tags.Recent(searchTag);
返回searchresult.Data.Select(媒体=>new InstagramResult()
{
媒体=媒体,
image=media.Images.lowsresolution.Url
})
.ToList();
}

..

您是否使用了此代码?我的代码中出现了完全相同的问题,无法找到解决方案。。