2011-02-01 127 views
0

我正在使用C#中的Windows Live SDK嘗試直接調用其他服務來獲取我的oAuth令牌,但是我得到了401錯誤。未經授權。Windows Live SDK授權

我已將我的應用程序註冊爲桌面應用程序https://manage.dev.live.com/並已發佈,因此我擁有自己的客戶端ID和密鑰。

從我的桌面應用程序,我打開一個用於實時登錄的Internet Explorer窗口並獲取我的驗證碼。

與該代碼,密鑰和clientID的我調用AccessToken.aspx在這條路上:

try 
{ 
    string requestUrl = "https://consent.live.com/AccessToken.aspx"; 

    // Request the access token. 
    string postData = string.Format("wrap_client_id={0}&wrap_client_secret={1}&wrap_verification_code={2}", 
             clientId, 
             secretKey, 
             verificationCode); 

    postData = HttpUtility.UrlEncode(postData); 
    byte[] postDataEncoded = System.Text.Encoding.UTF8.GetBytes(postData); 

    WebRequest req = HttpWebRequest.Create(requestUrl); 
    req.Method = "POST"; 
    req.ContentType = "application/x-www-form-urlencoded"; 
    req.ContentLength = postDataEncoded.Length; 

    Stream requestStream = req.GetRequestStream(); 
    requestStream.Write(postDataEncoded, 0, postDataEncoded.Length); 

    WebResponse res = req.GetResponse(); 

    string responseBody = null; 

    using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8)) 
    { 
     responseBody = sr.ReadToEnd(); 
    } 

    // Process FORM POST. 
    NameValueCollection responseCollection = System.Web.HttpUtility.ParseQueryString(responseBody); 

    return responseCollection["wrap_access_token"]; 
} 
catch (Exception ex) 
{ 
    throw ex; 
} 

我做錯了嗎?

在此先感謝您提供的任何幫助。

回答