2013-10-04 47 views
0

我想授權一個控制檯應用程序使用oAuth 2.0的API。我試過DotNetOpenAuth沒有成功。oAuth 2與開放API

第一步是獲取授權碼,這是我的,但我無法找到授權碼繼續步驟2(授權使用預定義的授權碼,帶有用戶ID和用戶密碼)。我從文檔 https://sandbox-connect.spotware.com/draftref/GetStartAccounts.html#accounts使用了以下授權頭文件?

在響應中我沒有得到任何驗證代碼的頭文件,我該如何解決這個問題?

string sAuthUri = "https://sandbox-connect.spotware.com/oauth/v2/auth"; 
string postData ="access_type=online&approval_prompt=auto&client_id=7_5az7pj935owsss8kgokcco84wc8osk0g0gksow0ow4s4ocwwgc&redirect_uri=http%3A%2F%2Fwww.spotware.com%2F&response_type=code&scope=accounts"; 

     string sTokenUri = "https://sandbox-connect.spotware.com/oauth/v2/token"; 

     var webRequest = (HttpWebRequest)WebRequest.Create(sAuthUri); 

     webRequest.Method = "POST"; 
     webRequest.ContentType = "application/x-www-form-urlencoded"; 

     try 
     { 

      using (Stream s = webRequest.GetRequestStream()) 
      { 
       using (StreamWriter sw = new StreamWriter(s)) 
        sw.Write(postData.ToString()); 
      } 

      using (WebResponse webResponse = webRequest.GetResponse()) 
      { 
       using (var reader = new StreamReader(webResponse.GetResponseStream())) 
       { 
        response = reader.ReadToEnd(); 
       } 
      } 

      var return = response; 
     } 
     catch (Exception ex) 
     { 

     } 

回答

0

使用的OAuth2的Auhtorization碼流,碼應該進來重定向REDIRECT_URI的查詢字符串。

我認爲問題出在調用方法(應該是GET),postData爲QueryString,而redirect_uri必須是系統的URL(而不是Spotware)。授權碼應該從webResponse的查詢字符串中檢索。

我建議您使用DotNetOpenAuth庫輕鬆實現OAuth2請求。