Search code examples
ontime

Obtaining Authorization token


I'm trying to obtain a authorization token for OnTime OnDemand API using asp.net mvc. below are my simple action methods to obtain the token.

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");
        }

I'm receiving {"error":"invalid_grant"}.


Solution

  • I solved the issue. redirect_uri should be the same in both methods.

    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");
            }