3

I am trying to get access token from Foursquare API via Python.

I'm using the wrapper from https://github.com/mLewisLogic/foursquare And, followed the same approach mentioned on http://log.hckr.org/2012/02/01/foursquare-api-wrapper-for-python

But, for access_token = client.oauth.get_token('XX_CODE_RETURNED_IN_REDIRECT_XX') . I get the following error

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python27\lib\site-packages\foursquare\__init__.py", line 135, in get_token
    response = _request_with_retry(url)
  File "C:\Python27\lib\site-packages\foursquare\__init__.py", line 707, in _request_with_retry
    return _process_request_with_httplib2(url, data)
  File "C:\Python27\lib\site-packages\foursquare\__init__.py", line 733, in _process_request_with_httplib2
    raise FoursquareException(u'Error connecting with foursquare API')
FoursquareException: Error connecting with foursquare API

I'm not sure if the error is because of httplib2 library or something else!

Anyone has any solutions?

Zero
  • 74,117
  • 18
  • 147
  • 154
  • Can you post more code around this call? And just to be sure, you're actually passing in the code (can be found as a query string param), not the string `'XX_CODE_RETURNED_IN_REDIRECT_XX'`, correct? – octopi Mar 05 '13 at 23:28
  • @octopi I have tried the code mentioned on http://log.hckr.org/2012/02/01/foursquare-api-wrapper-for-python – Zero Mar 12 '13 at 18:04
  • What is the error that actually gets logged? – Mike Lewis Jun 26 '13 at 18:03

1 Answers1

1

It looks like the wrapper you've chosen might be out of date.

If you're open to a more generalized OAuth consumer solution, I can recommend rauth. Rauth is a simple OAuth 1.0/a and 2.0 client built around Requests. Basically what you'd want to do is set up FourSquare as a service. We could amend the Facebook example we provide for rauth like so:

from rauth import OAuth2Service

import re
import webbrowser

# Get a real client_id and client_secret from:
# https://developer.foursquare.com/overview/auth#registration

foursquare = OAuth2Service(
    client_id=xxx,
    client_secret=xxx,
    name='foursquare',
    authorize_url='https://foursquare.com/oauth2/authenticate',
    access_token_url='https://foursquare.com/oauth2/access_token',
    base_url='https://api.foursquare.com/v2/')

# This should redirect to your app, may function as a demo
# without updating, but be sure to update once you're done
# experimenting!
redirect_uri = 'https://example.com/'

params = {'response_type': 'token',
          'redirect_uri': redirect_uri}

authorize_url = foursquare.get_authorize_url(**params)

print 'Visit this URL in your browser: ' + authorize_url
webbrowser.open(authorize_url);

url_with_code = raw_input('Copy URL from your browser\'s address bar: ')
access_token = re.search('\#access_token=([^&]*)', url_with_code).group(1)
session = foursquare.get_session(access_token)

user = session.get('users/self').json()['response']['user']

print 'currently logged in as: ' + user['firstName']

I haven't run this code but feel free to give it a go (remember, you gotta put your own credentials in service constructor, i.e. the client_id and client_secret parameters, before it will run properly. Good luck and let me know how it goes if you do decide to give it a shot!

Full disclosure, I maintain rauth.

maxcountryman
  • 1,562
  • 1
  • 24
  • 51