【问题标题】:Foursquare API Python Access Token ErrorFoursquare API Python 访问令牌错误
【发布时间】:2013-02-20 09:20:09
【问题描述】:

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

我正在使用来自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 库还是其他原因!

有人有解决办法吗?

【问题讨论】:

  • 你能围绕这个调用发布更多代码吗?为了确定,您实际上是在传递代码(可以作为查询字符串参数找到),而不是字符串 'XX_CODE_RETURNED_IN_REDIRECT_XX',对吗?
  • @octopi 我已经尝试过log.hckr.org/2012/02/01/foursquare-api-wrapper-for-python中提到的代码
  • 实际记录的错误是什么?

标签: python api oauth-2.0 foursquare httplib2


【解决方案1】:

您选择的包装器似乎已过时。

如果您愿意接受更通用的 OAuth 消费者解决方案,我可以推荐 rauth。 Rauth 是一个围绕请求构建的简单 OAuth 1.0/a 和 2.0 客户端。基本上你想要做的是将 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 参数,然后它才能正常运行。祝你好运如果你决定试一试,请告诉我进展如何!

完全公开,我保持沉默。

【讨论】:

  • 截至发表此评论时,包装器仍受支持并通过所有实时单元测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-01
  • 2023-03-07
  • 1970-01-01
  • 2014-04-18
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
相关资源
最近更新 更多