2012-10-23 47 views
1

我看不到我做錯了什麼,但這可能是因爲這是我的第一個oauth探戈,我確定我在某處將它新建了。我目前使用由google代碼託管的oauth庫,並鏈接到oauth的網站(http://oauth.googlecode.com/svn/code/csharp/)C#嘗試檢索yahoo oauth請求令牌結果401

對於下面的代碼,變量「YQL」是

private OAuthBase YQL; 

像這樣初始化:我班的範圍內聲明的AS保護像這樣的「OAuthBase」對象

public AverageEverydayConstructor() 
{ 
    ... 
    YQL = new OAuthBase(); 
    ... 
} 

這裏是所有實際的非功能時(在字符串「鍵」是我的消費者密鑰,「祕密」是我的消費者密碼)

private string yahooRetrieveToken(string key, string secret) 
    { 
     string tokenRequestUrl = @"https://api.login.yahoo.com/oauth/v2/get_request_token"; 
     string parameters = ""; 

     string timestamp = YQL.GenerateTimeStamp(); 
     string nonce = YQL.GenerateNonce(); 

     parameters += "?oauth_nonce=" + nonce; 
     parameters += "&oauth_timestamp=" + timestamp; 
     parameters += "&oauth_consumer_key=" + key; 
     parameters += "&oauth_signature_method=HMAC-SHA1"; 
     parameters += "&oauth_signature=" + secret; 
     parameters += "&oauth_version=1.0"; 
     parameters += "&xoauth_lang_pref=en-us"; 
     parameters += "&oauth_callback=\"oob\""; 


     string fullUrl = tokenRequestUrl + parameters; 
     Clipboard.SetText(fullUrl); 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUrl); 
     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //this is the line that actually err's with 401. 
     Stream result = response.GetResponseStream(); 

     //And yes, I'm aware I'm not using very good practice and haven't properly closed the stream. I'm just trying to get it to work first, but don't worry I haven't forgotten. 


     string unparsedResult = result.ToString(); 
     return unparsedResult; 
    } 

我用盡了一切我能想到的走了過來這個頁面(http://developer.yahoo.com/oauth/guide/oauth-requesttoken.html)數十次。爲了確保我覆蓋了所有的基地,我也嘗試過來回改變下面兩行,看看是否有任何改變。

 parameters += "&oauth_signature_method=PLAINTEXT"; 
     parameters += "&oauth_signature=" + secret + "%26"; 

謝謝大家!

回答

2

所以我不確定這是爲什麼工作。我的問題正是(據我所知)符合YQL的文檔規範。我做了一個GET請求,而不是發出一個POST請求,只是在請求的頭部做了所有事情。它基於此:401 Unauthorized using Yahoo OAuth

我不知道他爲什麼沒有爲他工作,但爲我工作;無論哪種方式,這是產生的代碼工作。我沒有真正從它得到一個令牌,但我能夠使YQL請求:

/// <summary> 
/// Make a YQL query and return an unformated xml string 
/// </summary> 
/// <param name="key">Application Consumer Key</param> 
/// <param name="secret">Application Consumer Secret</param> 
/// <param name="query">The YQL query you want to run</param> 
/// <returns>Returns formatted xml in the form of a string from YQL</returns> 
protected string yqlQuery(string key, string secret, string query) 
{ 
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"http://query.yahooapis.com/v1/yql?q=" + query); 
    OAuthBase YQL = new OAuthBase(); 
    string nonce = YQL.GenerateNonce(); 
    string timestamp = YQL.GenerateTimeStamp(); 
    request.Headers.Add(
     "Authorization: OAuth " + 
     "realm=\"yahooapis.com\"," + 
     "oauth_consumer_key=\"" + key + "\"," + 
     "oauth_nonce=\"" + nonce + "\"," + 
     "oauth_signature_method=\"PLAINTEXT\"," + 
     "oauth_timestamp=\"" + timestamp + "\"," + 
     "oauth_version=\"1.0\"," + 
     "oauth_signature=\"" + secret + "%26\"" 
    ); 
    string resultString = ""; 
    using (StreamReader read = new StreamReader(request.GetResponse().GetResponseStream(), true)) 
    { 
     resultString = read.ReadToEnd(); 
    } 
    return resultString; 
} 
+0

它仍然工作!我永遠不會猜測使用oauth_signature_method PLAINTEXT和auth_signature的祕密,謝謝。 – vlscanner