获取授权令牌

时间:2013-10-08 18:25:34

标签: ontime

我正在尝试使用asp.net mvc获取OnTime OnDemand API的授权令牌。下面是我获取令牌的简单操作方法。

public ActionResult Index()
        {
            string eURL = HttpUtility.UrlEncode("http://localhost:2574/OnTimeClient/AuthorizationCodeCallback");
            return Redirect("https://iss.ontimenow.com/auth?response_type=code&client_id=6457eb4e-42c7-4c19-ad25-73fc0d016e5a&redirect_uri=" +eURL+"&scope=read%20write");

        }

        public ActionResult AuthorizationCodeCallback(string code, string error)
        {
            return Redirect("https://iss.ontimenow.com/api/oauth2/token?grant_type=authorization_code&code="+code+"&redirect_uri=www.yahoo.com&client_id=6457eb4e-42c7-4c19-ad25-73fc0d016e5a&client_secret=8538c23b-211f-4351-bfe9-f533e81c97bf");
        }

我收到{“error”:“invalid_grant”}。

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。两个方法中的redirect_uri应该相同。

public ActionResult Index()
        {
if(Request.QueryString["code"]==null)
{
           string eURL = HttpUtility.UrlEncode("http://localhost:2574/OnTimeClient/AuthorizationCodeCallback");
            return Redirect("https://iss.ontimenow.com/auth?response_type=code&client_id=6457eb4e-42c7-4c19-ad25-73fc0d016e&redirect_uri=" +eURL+"&scope=read%20write");
}
else
{
  //process after obtaining the token
}

        }

        public ActionResult AuthorizationCodeCallback(string code, string error)
        {
string eURL = HttpUtility.UrlEncode("http://localhost:2574/OnTimeClient/AuthorizationCodeCallback");
            return Redirect("https://iss.ontimenow.com/api/oauth2/token?grant_type=authorization_code&code="+code+"&redirect_uri="+eURL+"&client_id=6457eb4e-42c7-4c19-ad25-73fc0d016e&client_secret=8538c23b-211f-4351-bfe9-f533e81c97bf");
        }
相关问题