2013-10-08 172 views
2

我正在嘗試使用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

我解決了這個問題。兩種方法中的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"); 
     }