Instagram api 服务器端身份验证 asp.net mvc

Instagram api server-side authentication asp.net mvc

我正在尝试通过IG文档中的示例实现服务器端身份验证,但是遇到了麻烦。 当我执行我的请求时,我收到了状态为 302 Found 的响应,并且在 Headers Location 中我没有使用代码进行新的重定向 url,但是如果我在浏览器中执行相同的操作,一切正常。

这是我的代码和示例:

public JsonResult InstagramTeaser()
    {
        try
        {
            var clientId = Configuration.AuthKeys.InstagramOAuthClientId;
            var clientSecret = Configuration.AuthKeys.InstagramOAuthClientSecret;
            var redirectUrl = "http://localhost";
            var uri = $"http://api.instagram.com/oauth/authorize/?client_id={clientId}&redirect_uri={redirectUrl}&response_type=code";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.AllowAutoRedirect = false;
            string redirUrl = String.Empty;
            using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
            {
                int status = (int)response.StatusCode; // 302 Found
                redirUrl = response.Headers[HttpResponseHeader.Location]; // And here I'm getting my old uri
            }

            return Json(redirUrl, JsonRequestBehavior.AllowGet);
        }
        catch (Exception e)
        {
            return Json(e.Message, JsonRequestBehavior.AllowGet);
        }
    }

Results in browser

The way auth works in MVC is that when you aren't logged in and try to access a secure page it throws a 401 exception. MVC then catches this exception and redirects the user to the login page (which is the 302 you are seeing)

Here您可以获得有关您的问题的更多信息