1

I can't see what I'm doing wrong, but that's probably because it's my first tango with oauth and I'm sure I'm newb-ing it up somewhere. I'm currently using the oauth library hosted by google code and linked on oauth's website (http://oauth.googlecode.com/svn/code/csharp/)

For the following code, the variable "YQL" is an "OAuthBase" object that's declared within the scope of my class as protected like this:

private OAuthBase YQL;

and initialized like this:

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

And here is where all the actual non-functionality occurs (the string "key" is my Consumer Key and "secret" is my Consumer 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;
    }

I've tried everything I can think of and gone over this page (http://developer.yahoo.com/oauth/guide/oauth-requesttoken.html) dozens of times. To make sure I covered all my bases I've also tried changing the two lines below back and forth just so see if there was any change.

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

Thanks anyone!

Niko
  • 4,158
  • 9
  • 46
  • 85

1 Answers1

2

So I'm not sure exactly why this worked. My question was exactly (as far as I'm aware) to the YQL's documentation specifications. Instead of making a POST request, I made a GET request and just did everything in the request's header. It's based on this: 401 Unauthorized using Yahoo OAuth

I don't know why what he did didn't work for him, but worked for me; either way here's the resulting code that worked. I didn't actually get a token from it, but I was able to make YQL requests:

/// <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;
}
Community
  • 1
  • 1
Niko
  • 4,158
  • 9
  • 46
  • 85
  • and it still works! I would never guess to use oauth_signature_method PLAINTEXT and secret for auth_signature, thank you. – vlscanner Jun 10 '15 at 23:34