【问题标题】:C# attempt to retrieve yahoo oauth request token results in 401C# 尝试检索 yahoo oauth 请求令牌导致 401
【发布时间】:2012-10-23 02:34:20
【问题描述】:

我看不出我做错了什么,但这可能是因为这是我第一次使用 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";

谢谢大家!

【问题讨论】:

    标签: c# oauth request token yahoo


    【解决方案1】:

    所以我不确定为什么会这样。我的问题正是(据我所知)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;
    }
    

    【讨论】:

    • 它仍然有效!我永远不会猜到使用 oauth_signature_method PLAINTEXT 和 secret 作为 auth_signature,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 2017-03-14
    • 2011-11-16
    • 1970-01-01
    相关资源
    最近更新 更多