C#尝试在401中检索yahoo oauth请求令牌结果

时间:2012-10-23 02:34:20

标签: c# oauth request token yahoo

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

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

private OAuthBase YQL;

并初始化如下:

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

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

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

谢谢任何人!

1 个答案:

答案 0 :(得分: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;
}