Skip to main content
 首页 » 编程设计

python之Foursquare API Python 访问 token 错误

2025年05月04日8jackei

我正在尝试通过 Python 从 Foursquare API 获取访问 token 。

我正在使用来自 https://github.com/mLewisLogic/foursquare 的包装器 并且,遵循 http://log.hckr.org/2012/02/01/foursquare-api-wrapper-for-python 中提到的相同方法

但是,对于 access_token = client.oauth.get_token('XX_CODE_RETURNED_IN_REDIRECT_XX') 。我收到以下错误

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 

我不确定错误是因为 httplib2 库还是其他原因!

谁有解决办法?

请您参考如下方法:

看起来您选择的包装器可能已过时。

如果您对更通用的 OAuth 消费者解决方案持开放态度,我可以推荐 rauth . Rauth 是一个简单的 OAuth 1.0/a 和 2.0 客户端,围绕 Requests 构建。基本上,您要做的就是将 FourSquare 设置为一项服务。我们可以像这样修改我们为 rauth 提供的 Facebook 示例:

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'] 

我还没有运行这段代码,但可以随意试一试(记住,你必须将自己的凭据放入服务构造函数中,即 client_id 和 client_secret参数,然后它才能正常运行。祝你好运,如果你决定试一试,请告诉我进展情况!

完全公开,我保持原样。