python - Foursquare API Python 访问 token 错误

标签 python api oauth-2.0 foursquare httplib2

我正在尝试通过 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_idclient_secret参数,然后它才能正常运行。祝你好运,如果你决定试一试,请告诉我进展情况!

完全公开,我保持原样。

关于python - Foursquare API Python 访问 token 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15232863/

相关文章:

python - 有没有办法撤消递归正则表达式操作?

python - 在没有 elasticsearch-py 的情况下将 pandas 数据框索引到 Elasticsearch

python - 拆分python并根据接收到的变量进行计算

python - CNTK python api - 继续训练模型

api - 如何以编程方式从 LinkShare 获取我的 LinkShare 商家列表?

ios - 使用 GTMOAuth2SignIn headless 获取 token

java - 具有隐式授予类型 Spring boot 应用程序的 OAuth 2.0

python + appengine + oauth2 + 谷歌联系人 api

python - FFmpeg 在确认后返回“必须指定输出”

api - 无法使用curl使用docker API从tarball加载docker镜像