GithubHelp home page GithubHelp logo

timgates42 / foursquare-python Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wiseman/foursquare-python

0.0 1.0 0.0 22 KB

Python module to interface with the foursquare API.

License: MIT License

Python 100.00%

foursquare-python's Introduction

Copyright 2009 John Wiseman
Covered by the MIT License, see LICENSE.txt.

foursquare

This Python module lets you access the foursquare API. It supports unauthenticated access, basic HTTP authentication, and OAuth authorization. This code is based on a similar module for Fire Eagle made by Steve Marshall.

It supports all the v1 foursquare API methods as of 2009-12-17.

This module requires Leah Culver's oauth module, oauth.py.

Foursquare API method names are the same in Python, except for methods like friend/requests, which are translated to names like friend_requests in Python.

All method arguments are keyword arguments, though required arguments come first and are in the order listed by the API documentation.

All methods return the parsed Python equivalent of the JSON response returned by the corresponding API method, if there is a response.

Examples

No authentication:

>>> import foursquare
>>> fs = foursquare.Foursquare()
>>> fs.cities()
{'cities': [{'geolat': 52.378900000000002, 'name': 'Amsterdam', ...}]}

Basic HTTP authentication:

>>> import foursquare
>>> fs = foursquare.Foursquare(foursquare.BasicCredentials(username, password))
>>> fs.switchcity(23)
{'data': {'status': '1', 'message': 'City switched successfully'}}
>>> fs.switchcity(34)
{'data': {'status': '1', 'message': 'City switched successfully'}}
>>> fs.user()
{'user': {'city': {'geolat': 34.0443, 'name': 'Los Angeles', ...}}}

OAuth:

When foursquare added support for the oauth_callback parameter to specify a callback URL, they made oauth_verifier argument a required argument to the access_token method. The oauth_verifier that you need to pass to access_token comes from the URL that foursquare redirects the user to after authorization:

>>> import foursquare
>>> credentials = foursquare.OAuthCredentials(oauth_key, oauth_secret)
>>> fs = foursquare.Foursquare(credentials)
>>> app_token = fs.request_token(oauth_callback='http://myapp.example/')
>>> auth_url = fs.authorize(app_token)

# Go to auth_url and authorize.  Once you've authorized, foursquare
# will redirect you to a URL that looks like this:
#
#   http://myapp.example/?oauth_verifier=1234&oauth_token=abc9
#
# Take the oauth_verifier parameter value and pass it to
# access_token.

>>> oauth_verifier = '1234'
>>> user_token = fs.access_token(app_token, oauth_verifier)
>>> credentials.set_access_token(user_token)
>>> fs.user()
{'user': {'city': {'geolat': 34.0443, 'name': 'Los Angeles', ...}}}

The above is the most correct method according to the OAuth 1.0A spec. But foursquare supports a less stringent mode if you don't pass a oauth_callback argument, in which case you don't need to pass an oauth_verifier to access_token:

>>> import foursquare
>>> credentials = foursquare.OAuthCredentials(oauth_key, oauth_secret)
>>> fs = foursquare.Foursquare(credentials)
>>> app_token = fs.request_token(oauth_callback='http://myapp.example/')
>>> auth_url = fs.authorize(app_token)

# Go to auth_url and authorize.  Once you've authorized, foursquare
# will redirect you to your app's registered callback URL.  You don't
# need that URL; we're going to call the access_token method
# directly.
#
# Note that we're passing an empty string for the oauth_verifier.

>>> user_token = fs.access_token(app_token, '')
>>> credentials.set_access_token(user_token)
>>> fs.user()
{'user': {'city': {'geolat': 34.0443, 'name': 'Los Angeles', ...}}}

foursquare-python's People

Contributors

wiseman avatar harrisony avatar johl avatar decause avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.