I'm trying to realize server-side authentication by example in IG docs, but get a trouble. When I'm doing my request, I've getting response with status 302 Found, and in Headers Location I haven't new redirect url with code, but if I do same things in browser, all ok.
Here is my code and examples:
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);
}
}
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 you can get more information about your question