2
votes

I am currently using the OAuthBase.cs helper class available on oauth.net to implement OAuth to talk to the Yahoo! fantasy API. I am currently stuck on Step 5 (Using the access token/access secret to call the API Services).

I successfully completed Step 4 but cannot seem to create the actual service call. The documentation is limited; what parameters am I supposed to be using? I'm getting either a 401 or 400 http error. I'm generating my signature by:

url = new Uri("http://query.yahooapis.com/v1/public/yql?q=select * from fantasysports.teams.roster.stats where team_key='nba.l.52669.t.5' and week='5' and stats_type='week' and stats_week='5'&format=json");

signature = oauth.GenerateSignature(url, string.Empty, consumerKey, consumerSecret, accessToken, accessTokenSecret, "GET", time, string.Empty, nonce, OAuth.OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out normalizedRequestParameters);

using (var y = WebRequest.Create(string.Format("{0}?{1}&oauth_signature={2}", normalizedUrl, normalizedRequestParameters, signature)).GetResponse())
{
    ....
}

where url is the api call i'm trying to make, consumerKey / consumerSecret are the keys given to me when I signed up for Yahoo!, and accessToken / accessTokenSecret are the response returned from request_auth in Step 4. What am I doing wrong?

Thanks in advance

EDIT: 12/14 - for those not familiar with OAuthBase, it is essentially a library that generates the signature by 1. Consolidating all the url/parameters (consumerkey, token, tokenSecret, httpMethod, nonce, time etc), sorting it, and normalizing the url/parameters; 2. Encodes the consumerSecret '&' tokenSecret as the HMACSHA1 key; 3. computing the hash of the hmacsha1 key

1

1 Answers

5
votes

Here is some working code for accessing a Yahoo API using OAuth (in this case the BOSS Geo API)

    [Test]
    public void MakeCallToBossGeoApi()
    {
        string result;
        var uri = new Uri(@"http://yboss.yahooapis.com/geo/placefinder?country=SE&flags=J&locale=sv_SE&postal=41311");
        var o = new OAuthBase();
        string nonce = o.GenerateNonce();
        var timestamp = o.GenerateTimeStamp();

        string normalizedUrl;
        string normalizedParameters;
        string signature = HttpUtility.UrlEncode(
            o.GenerateSignature(uri,
                                "yourconsumerkeyhere",
                                "yourconsumersecrethere", null, null, "GET",
                                timestamp, nonce, out normalizedUrl,
                                out normalizedParameters));

        uri = new Uri(normalizedUrl +"?"+ normalizedParameters + "&oauth_signature=" + signature );

        using (var httpClient = new WebClient())
        {
            result = httpClient.DownloadString(uri.AbsoluteUri);
        }

        Console.WriteLine(result);
    }