0

Fantasy League Website

My league has a fantasy website in addition to our yahoo league site. We are looking at trying to merge the two together. As of now we would like to get the stats, standings points, and other things of that nature on our website. Would I need to authenticate with Oauth to accomplish this?

I am having trouble with the Oauth tutorial that is on the documentation. I get this message

"error": {
"lang": "en-US",
"description": "Please provide valid credentials. OAuth oauth_problem=\"signature_invalid\", realm=\"yahooapis.com\""

I have gotten token and access key, but when i try to run the script to show the JSON it comes back invalid.

I am new at programming, so I guess my better question would be to where to start if I just want to get the stats for our website

by
15 Replies
  • Yes, you'd definitely need to be authenticating through OAuth in order to pull out that information. It does take a bit of work to get started with (the price we pay for maintaining security!), so I don't think there's any sort of drop-in solution I can give you.

    One suggestion: if you're only trying to pull data from your league, and there's only one league that you're working with, it might make things easier to make things a bit more hardcoded instead of trying to get each and every one of your league members to authenticate through your site. I guess what I'm trying to say is that, if you want to show information only from leagues that you personally are part of on your site, you can just get an access token for your own Yahoo! ID, and then keep reusing that to make all of your requests from that point on. Let me give you a PHP example:

    Step 1: Manually acquire a request token
    a) Set up an application with Fantasy Sports R/W access: https://developer.apps.yahoo.com/dashboard/createKey.html
    B)(if you wanted to actually be able to pull out data for other people, you'd take the first two scripts and effectively run those for each user that hits your site, if that makes any sense)
    0
  • Sean,

    Thanks so much...This is exactly what I have been looking for
    0
  • Ok I have the data, Everything works perfectly...only one question and this might be a beginner question...but what next? How do I post the data that I have into a webpage?
    0
  • QUOTE (gmoney @ Jul 28 2010, 09:14 AM) <{POST_SNAPBACK}>
    Ok I have the data, Everything works perfectly...only one question and this might be a beginner question...but what next? How do I post the data that I have into a webpage?

    I guess that's a slightly more generic problem and I'm not sure that I can provide a comprehensive answer to it. :P You generally need a machine and a webserver running on it that can output HTML. You could buy up a domain name and get it pointing to some box in your basement, or buy space on some commercial webhosting platform, in order to take care of the machine part of things. For the webserver, I typically tend towards Apache, but there are tons of other options out there, both in terms of different software if you're working off of your own machine or more integrated packages that you can buy from webhosting companies -- you basically just need some way to run some programming language (PHP in my case) that can make these backend requests for data and also output text to be interpreted as HTML by browsers. I'm really not sure how to point you in the right direction much more than that -- I think you would just need to do a bit of research and figure out what works for you.
    0
  • What I needed to accomplish for page one of my simple app was a league scoreboard. Here is some php code that does it. From Sean's last example, I removed everything from the line of " print $o->getLastResponse();" down and replaced it with:

    CODE
        $sarah = $o->getLastResponse(); echo $team->name;

    $xml = new SimpleXMLElement($sarah);


    echo "Week #" . $xml->league->scoreboard->week;
    echo "<br>";
    //echo $sarah;

    foreach ($xml->league->scoreboard->matchups as $team)
    {
    echo $team->matchup[0]->teams->team[0]->name, ' ', $team->matchup[0]->teams->team[0]->team_points->total, ' versus ', $team->matchup[0]->teams->team[1]->name, ' ', $team->matchup[0]->teams->team[1]->team_points->total, '<br>', PHP_EOL;
    echo $team->matchup[1]->teams->team[0]->name, ' ', $team->matchup[1]->teams->team[0]->team_points->total, ' versus ', $team->matchup[1]->teams->team[1]->name, ' ', $team->matchup[1]->teams->team[1]->team_points->total, '<br>',PHP_EOL;
    echo $team->matchup[2]->teams->team[0]->name, ' ', $team->matchup[2]->teams->team[0]->team_points->total, ' versus ', $team->matchup[2]->teams->team[1]->name, ' ', $team->matchup[2]->teams->team[1]->team_points->total, '<br>',PHP_EOL;
    echo $team->matchup[3]->teams->team[0]->name, ' ', $team->matchup[3]->teams->team[0]->team_points->total, ' versus ', $team->matchup[3]->teams->team[1]->name, ' ', $team->matchup[3]->teams->team[1]->team_points->total, '<br>',PHP_EOL;
    echo $team->matchup[4]->teams->team[0]->name, ' ', $team->matchup[4]->teams->team[0]->team_points->total, ' versus ', $team->matchup[4]->teams->team[1]->name, ' ', $team->matchup[4]->teams->team[1]->team_points->total, '<br>',PHP_EOL;
    echo $team->matchup[5]->teams->team[0]->name, ' ', $team->matchup[5]->teams->team[0]->team_points->total, ' versus ', $team->matchup[5]->teams->team[1]->name, ' ', $team->matchup[5]->teams->team[1]->team_points->total, '<br>',PHP_EOL;
    }


    echo "<br>";

    print "<br>";
    } else {
    print "Couldn't fetch\n";
    }
    } catch( OAuthException $e ) {
    print 'Error: ' . $e->getMessage() . "\n";
    print 'Error code: ' . $e->getCode() . "\n";
    print 'Response: ' . $e->lastResponse . "\n";
    }
    ?>


    This takes the xml response, wraps it in an array named $sarah and then uses simple xml to parse the name and total points from the 6 matchups in my 12 team league.

    Hopefully this will give you an idea of one possible method. Also, the echo $sarah that is commented out is for testing. when you you need change your request, you can un-comment that so you can see the raw xml response. Once you have that, you can re-comment out that line and start working on your parse.
    0
  • Thanks guys I am going to try this out.
    0
  • My code and data will show up fine when i query in the terminal. when i try to print out into the browser i get this response on the web page

    "making the request failed (Peer certificate cannot be authenticated with known CA certificates)"

    here is my code:

    CODE
    <?php

    require('globals.php');

    $access_token='TOKEN';
    $access_token_secret = 'SECRET';
    $session_handle = 'HANDLE';
    $verifier = NULL;

    $o = new OAuth( $consumer_key, $consumer_secret,
    OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI );

    $o->enableDebug();

    // Get a new access_token every time.
    try {
    $o->setToken( $access_token, $access_token_secret );
    $response = $o->getAccessToken( 'https://api.login.yahoo.com/oauth/v2/get_token', $session_handle, $verifier );
    } catch( OAuthException $e ) {
    print $e->getMessage() . "\n";
    print $e->lastResponse . "\n";
    exit;
    }

    // Actually try to fetch a URL
    $url = 'http://fantasysports.yahooapis.com/fantasy/v2/league/222.l.62975/standings/';

    try {
    $o->setToken( $response['oauth_token'], $response['oauth_token_secret'] );
    if( $o->fetch( $url ) ) {

    $stand = $o->getLastResponse();

    $xml = new SimpleXMLElement($stand);

    echo "<table>" . "<tbody>";

    foreach($xml->league->standings->teams->team as $child)
    {
    echo "<tr>" . "<td>";
    echo $child->name;
    echo "</td>" . "<td>";
    echo $child->team_points->total;
    echo "</td>" . "<td>";
    echo $child->team_standings->outcome_totals->wins . " - " . $child->team_standings->outcome_totals->losses . " - " . $child->team_standings->outcome_totals->ties;
    echo "</td>" . "</tr>";
    }

    echo "</tbody>" . "</table>";

    print "Success";
    } else {
    print "Couldn't fetch\n";
    }
    } catch( OAuthException $e ) {
    print 'Error: ' . $e->getMessage() . "\n";
    print 'Error code: ' . $e->getCode() . "\n";
    print 'Response: ' . $e->lastResponse . "\n";
    }

    ?>


    Am I missing something. I do not know how to physically put this into a website. Once again, when I run $ php reqdata.php i can see my table, but i do not know how to take it out of that, if that makes since. I really appreciate all your help.
    0
  • Hm. It might help if you clarify what type of web server your machine is running (is it Apache?), and what URL you're pointing at to try to get your box to spit out code. For instance, say I had a DNS entry for www.smock514.com pointing at my machine, and I placed reqdata.php in my Apache docroot, and I made sure my Apache was set up to interpret PHP code -- I should be able to just hit http://www.smock514.com/reqdata.php and have it spit out the output generated by the php script. But I don't know how closely that matches up to how your setup might be working.
    0
  • I am running it locally on my machine right now using MAMP. I do have apache.
    0
  • QUOTE (gmoney @ Aug 6 2010, 04:12 AM) <{POST_SNAPBACK}>
    I am running it locally on my machine right now using MAMP. I do have apache.

    Okay, word. Are you able to get static HTML pages to load? ie, do you get the same certificate error if you hit a page that's not doing this OAuth stuff? If you get the same error, are you able to put an exception into your browser for that page? What URL are you hitting to try to access your script?
    0
  • I am only having problems with my OAuth things. I have other sites that I am testing with MAMP. I guess I am not sure what url you are talking about. I am able to access it in the script, just not the browser
    0
  • QUOTE (gmoney @ Aug 10 2010, 09:33 AM) <{POST_SNAPBACK}>
    I am only having problems with my OAuth things. I have other sites that I am testing with MAMP. I guess I am not sure what url you are talking about. I am able to access it in the script, just not the browser

    I recently wrote an application in PHP (that also uses the PECL OAuth extension) and posted it online, and also developed it on Mac. Maybe seeing if it works on your system would be a decent test. Note: The yahoo messages API changed today (or, in the next couple days) so eventually I'll test/update it for that. Regardless, it should at least do _something_ :)
    0
  • im needing something similar to this as well :/ unfortunately my webhost wont let me install oauth. anyone know of another solution? im just needing to pull matchup information (scoreboard) for a private league.
    0
  • QUOTE (john p @ Aug 25 2010, 05:24 PM) <{POST_SNAPBACK}>
    im needing something similar to this as well :/ unfortunately my webhost wont let me install oauth. anyone know of another solution? im just needing to pull matchup information (scoreboard) for a private league.

    As long as you have curl, you can make all the requests yourself. For instance, here's how to get a request token:

    CODE
    <?php
    $consumer_key = $consumer_data[$scope]['key'];
    $consumer_secret = $consumer_data[$scope]['secret'];

    $request_url = 'https://api.login.yahoo.com/oauth/v2/get_request_token';

    $oauth_signature_method = 'plaintext';
    $oauth_nonce = rand( 0, 100000 );
    $oauth_timestamp = time();
    $oauth_version = '1.0';
    $xoauth_lang_pref = 'en-us';
    $oauth_callback = 'oob';

    $method = 'GET';
    $params = '';
    $params .= '&oauth_callback=' . urlencode( $oauth_callback );
    $params .= '&oauth_consumer_key=' . urlencode( $consumer_key );
    $params .= '&oauth_nonce=' . urlencode( $oauth_nonce );
    $params .= '&oauth_signature_method=' . urlencode( $oauth_signature_method );
    $params .= '&oauth_timestamp=' . urlencode( $oauth_timestamp );
    $params .= '&oauth_version=' . urlencode( $oauth_version );

    // For plaintext, oauth signature is urlencode( "${consumer_secret}&" )
    $oauth_secret = $consumer_secret . '&';
    $params .= '&oauth_signature=' . urlencode( $oauth_secret );

    $url = $request_url . '?' . $params;

    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1 );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0 );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0 );
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0 );

    $timeout = 2; // seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout );
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout );

    $contents = curl_exec($ch);
    $errno = curl_errno($ch);
    $error_str = curl_error($ch);

    curl_close($ch);

    if( $errno || $error_str ) {
    print 'Error: ' . $error_str . ' (' . $errno . ")\n";
    print 'Contents: ' . $contents . "\n";
    } else {
    print "Successful\n";
    print $contents . "\n";

    $pairs = explode( '&', $contents );
    foreach( $pairs as $pair ) {
    $expanded = explode( '=', $pair );
    if( count( $expanded ) == 2 ) {
    $key = $expanded[0];
    $value = $expanded[1];
    print ' ' . $key . ' => ' . urldecode( $value ) . "\n";
    }
    }
    }

    ?>


    I put a similar example of how you might construct an actual request over here:
    http://developer.yahoo.net/forum/index.php...ost&p=18335

    And all the manual steps are actually described in a pretty good amount of detail, starting here:
    http://developer.yahoo.com/oauth/guide/oauth-auth-flow.html
    0
  • Heh, oops, it looks like the forum update ate some  of the previous sample code that had been posted.

    http://developer.yahoo.com/fantasysports/guide/GettingStarted.html#GettingStarted-sample

    Does that help at all? Basically, that's a PHP script built on top of a standard PHP OAuth library that'll basically do all the work necessary to pull down a certain API URL for whichever user you can log in as. Are you able to try that out?
    0

Recent Posts

in Fantasy Sports API