1

我看不出我做错了什么,但这可能是因为这是我第一次使用 oauth 探戈,而且我确信我在某个地方对其进行了新的尝试。我目前正在使用由谷歌代码托管并链接到 oauth 网站 (http://oauth.googlecode.com/svn/code/csharp/) 的 oauth 库

对于以下代码,变量“YQL”是一个“OAuthBase”对象,它在我的类范围内声明为受保护,如下所示:

private OAuthBase YQL;

并像这样初始化:

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

这是所有实际非功能发生的地方(字符串“key”是我的消费者密钥,“secret”是我的消费者秘密)

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

谢谢任何人!

4

1 回答 1

2

所以我不确定为什么会这样。我的问题正是(据我所知)YQL 的文档规范。我没有发出 POST 请求,而是发出了一个 GET 请求,只是做了请求标头中的所有操作。它基于: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;
}
于 2012-11-03T03:18:46.873 回答