如何使用 Python 通过 FourSquare 获取用户特定数据(带身份验证)

发布于 2024-12-04 12:51:02 字数 2910 浏览 6 评论 0原文

我正在尝试查询 FourSquare API 以获取特定于用户的信息和以场地为中心的信息。我已经成功地获得了以场地为中心的数据,但是我对以用户为中心的信息一无所知。我尝试按照此链接。 但是 request_token url 不返回任何内容。 :(。 如果有人能指出那就太好了。
1)我可能出错的地方。
2)另外,如果我的方法完全错误,请建议正确的方法。

编辑-这是代码片段中的一些修改后的代码,

     <!-- language: python -->
                     ### GET A REQUEST TOKEN ###

consumer=oauth.Consumer(key="NN3TBSPRBCVJMWCDPN1WELO1LOQLXAA31Q40WHW2L1BI5L1X", secret="RVMP1GUQCMFLX31ME5DJKKMZDZFBNYYVV5BKGVXZNWN2MDVM")

request_token_url = 'http://foursquare.com/oauth/request_token'

client = oauth.Client(consumer)
resp, content = client.request(request_token_url, "GET")

request_token = dict(urlparse.parse_qsl(content))

print 'Request tokens are',request_token.keys()
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])


### CREATE A SIGNED CONSUMER REQUEST ###

# Set the API endpoint 
url = "http://api.foursquare.com/v1/authexchange"

# Set the base oauth_* parameters along with any other parameters required
# for the API call.
params = {
'oauth_version': "1.0",
'oauth_nonce': oauth.generate_nonce(),
'oauth_timestamp': int(time.time()),
'fs_username': 'MyFourSquare Username',
'fs_password': 'MyFourSquarePassword',
'oauth_token': request_token['oauth_token'],
'oauth_consumer_key': consumer.key,
}

req = oauth.Request(method="GET", url=url, parameters=params)

# Sign the request.
signature_method = oauth.SignatureMethod_HMAC_SHA1()
req.sign_request(signature_method, consumer, token)


### Make the auth request ###

test = 'http://api.foursquare.com/v1/test.json'

resp, content = client.request(test, "GET")

print resp
print content # prints 'ok'

当我运行代码时,出现以下错误。

Request tokens are []
Traceback (most recent call last):
File "testingFour.py", line 60, in <module>
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
KeyError: 'oauth_token'

据我所知,请求令牌返回为空,知道为什么会发生这种情况吗?

-----------------------------------第二--编辑: ---------- -------------------------------------------------- --------------
这是我尝试打印 resp,content 时遇到的错误。我知道我调用了错误的 url ,但显然用 ouath2 代替 oauth 并没有帮助,

 {'status': '400', 'content-length': '91', 'expires': 'Thu, 15 Sep 2011 12:04:57 UTC', 'server': 'nginx/0.8.52', 'connection': 'keep-alive', 'pragma': 'no-cache', 'cache-control': 'no-cache, private, no-store', 'date': 'Thu, 15 Sep 2011 12:04:57 GMT', 'content-type': 'text/plain; charset=utf-8'} AS PART OF THE APIV1 SUNSET, OAUTH V1 HAS BEEN DIABLED. SEE HTTP://DEVELOPER.FOURSQUARE.COM
Request tokens are []
Traceback (most recent call last):
File "testingFour.py", line 60, in <module>
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
KeyError: 'oauth_token'

谢谢您的宝贵时间!

I'm trying to query the FourSquare API to obtain user-specific information and venue centric information . I've been successfully able to get the venue centric data , however I'm clueless about the user centric information . I tried following this link.
However the request_token url returns nothing . :(.
It would be great if someone could point out .
1) Where I'm possibly going wrong .
2) Also , suggest the right approach if my approach is completely wrong.

EDIT - Here's some of the modified code from the snippet

     <!-- language: python -->
                     ### GET A REQUEST TOKEN ###

consumer=oauth.Consumer(key="NN3TBSPRBCVJMWCDPN1WELO1LOQLXAA31Q40WHW2L1BI5L1X", secret="RVMP1GUQCMFLX31ME5DJKKMZDZFBNYYVV5BKGVXZNWN2MDVM")

request_token_url = 'http://foursquare.com/oauth/request_token'

client = oauth.Client(consumer)
resp, content = client.request(request_token_url, "GET")

request_token = dict(urlparse.parse_qsl(content))

print 'Request tokens are',request_token.keys()
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])


### CREATE A SIGNED CONSUMER REQUEST ###

# Set the API endpoint 
url = "http://api.foursquare.com/v1/authexchange"

# Set the base oauth_* parameters along with any other parameters required
# for the API call.
params = {
'oauth_version': "1.0",
'oauth_nonce': oauth.generate_nonce(),
'oauth_timestamp': int(time.time()),
'fs_username': 'MyFourSquare Username',
'fs_password': 'MyFourSquarePassword',
'oauth_token': request_token['oauth_token'],
'oauth_consumer_key': consumer.key,
}

req = oauth.Request(method="GET", url=url, parameters=params)

# Sign the request.
signature_method = oauth.SignatureMethod_HMAC_SHA1()
req.sign_request(signature_method, consumer, token)


### Make the auth request ###

test = 'http://api.foursquare.com/v1/test.json'

resp, content = client.request(test, "GET")

print resp
print content # prints 'ok'

I get the following error when I run the code .

Request tokens are []
Traceback (most recent call last):
File "testingFour.py", line 60, in <module>
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
KeyError: 'oauth_token'

From what I can make out the request tokens are being returned empty, any idea why this could be happening?

-----------------------------------SECOND--EDIT: --------------------------------------------------------------------------
This is the error I get when I try to print resp,content. I know that I'm calling the wrong url , but clearly substituting ouath2 for oauth isn't helping

 {'status': '400', 'content-length': '91', 'expires': 'Thu, 15 Sep 2011 12:04:57 UTC', 'server': 'nginx/0.8.52', 'connection': 'keep-alive', 'pragma': 'no-cache', 'cache-control': 'no-cache, private, no-store', 'date': 'Thu, 15 Sep 2011 12:04:57 GMT', 'content-type': 'text/plain; charset=utf-8'} AS PART OF THE APIV1 SUNSET, OAUTH V1 HAS BEEN DIABLED. SEE HTTP://DEVELOPER.FOURSQUARE.COM
Request tokens are []
Traceback (most recent call last):
File "testingFour.py", line 60, in <module>
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
KeyError: 'oauth_token'

Thank you for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兔姬 2024-12-11 12:51:02

您正在尝试连接到 FourSquare API v1。但现在这已被禁用:

AS PART OF THE APIV1 SUNSET, OAUTH V1 HAS BEEN DIABLED. SEE HTTP://DEVELOPER.FOURSQUARE.COM

显然您使用的示例已经过时了。

You are trying to connect to FourSquare API v1. But this has been disable by now:

AS PART OF THE APIV1 SUNSET, OAUTH V1 HAS BEEN DIABLED. SEE HTTP://DEVELOPER.FOURSQUARE.COM

Apparently the example your are using is outdated.

肩上的翅膀 2024-12-11 12:51:02

正如 plaes 所提到的,foursquare 的 V1 库已被弃用并关闭。

foursquare 的 V2 api 文档位于:https://developer.foursquare.com/docs/

foursquare Python 库可以在这里找到: https://github.com/mLewisLogic/foursquare

Pythonic,简单,满的单元测试。 免责声明:我编写了该库

As plaes mentioned, foursquare's V1 library is deprecated and shut down.

Docs for foursquare's V2 api are here: https://developer.foursquare.com/docs/

A comprehensive foursquare Python library can be found here: https://github.com/mLewisLogic/foursquare

Pythonic, simple, full unit-testing. Disclaimer: I wrote the library

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文