0

Python Question

Hi,

I am a beginner and am using python-oauth2. I followed the Twitter Three-legged OAuth Example here: https://github.com/simplegeo/python-oauth2. And am able to successfully get the oauth_token, oauth_token_secrety, and oauth_session_handle. The Example then states: "You may now access protected resources using the access tokens above."

Can someone post a simple example of the python-oauth2 calls that one would make to access, for example, all the teams in a fantasy baseball league? The Yahoo! documentation is clear, I just kind find any examples or documentation to use python-oauth2.

thanks,
Mike

by
2 Replies
  • QUOTE (mike_bishop @ Mar 22 2011, 10:51 PM) <{POST_SNAPBACK}>
    Hi,

    I am a beginner and am using python-oauth2. I followed the Twitter Three-legged OAuth Example here: https://github.com/simplegeo/python-oauth2. And am able to successfully get the oauth_token, oauth_token_secrety, and oauth_session_handle. The Example then states: "You may now access protected resources using the access tokens above."

    Can someone post a simple example of the python-oauth2 calls that one would make to access, for example, all the teams in a fantasy baseball league? The Yahoo! documentation is clear, I just kind find any examples or documentation to use python-oauth2.

    thanks,
    Mike

    I'm not much with Python, but I think you'd just want to plug in those values as the token/secret values in the "Signing the Request" example they have a little earlier up on the page? ie:

    CODE
    import oauth2 as oauth
    import time

    # Set the API endpoint
    url = "http://fantasysports.yahooapis.com/fantasy/v2/game/nfl"

    # Set the base oauth_* parameters along with any other parameters required
    # for the API call.
    params = {
    'oauth_version': "1.0",
    'oauth_nonce': oauth.generate_nonce(),
    'oauth_timestamp': int(time.time())
    }

    # Set up instances of our Token and Consumer. The Consumer.key and
    # Consumer.secret are given to you by the API provider. The Token.key and
    # Token.secret is given to you after a three-legged authentication.
    token = oauth.Token(key="tok-test-key", secret="tok-test-secret")
    consumer = oauth.Consumer(key="con-test-key", secret="con-test-secret")

    # Set our token/key parameters
    params['oauth_token'] = token.key
    params['oauth_consumer_key'] = consumer.key

    # Create our request. Change method, etc. accordingly.
    req = oauth.Request(method="GET", url=url, parameters=params)

    # Sign the request.
    signature_method = oauth.SignatureMethod_HMAC_SHA1()
    req.sign_request(signature_method, consumer, token)

    # Pulled from example code
    client = SimpleOAuthClient(SERVER, PORT, REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZATION_URL)
    print 'parameters: %s' % str(req.parameters)
    params = client.access_resource(req)
    print 'GOT'
    print 'non-oauth parameters: %s' % params


    I'm guessing that last part based on the example code in the examples folder:

    CODE
    ...
    # access some protected resources
    print '* Access protected resources ...'
    pause()
    parameters = {'file': 'vacation.jpg', 'size': 'original'} # resource specific params
    oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_method='POST', http_url=RESOURCE_URL, parameters=parameters)
    oauth_request.sign_request(signature_method_hmac_sha1, consumer, token)
    print 'REQUEST (via post body)'
    print 'parameters: %s' % str(oauth_request.parameters)
    pause()
    params = client.access_resource(oauth_request)
    print 'GOT'
    print 'non-oauth parameters: %s' % params
    pause()


    Heh, does that even begin to help?
    0
  • Here is the code I use in my Python desktop app to access the Yahoo Fantasy Sports API through OAuth. It's very unpolished. Maybe it will help though. https://github.com/mleveck/YHandler

    0

Recent Posts

in Fantasy Sports API