python - 如何使用 pycurl 在 foursquare 中获取 field 请求?

标签 python foursquare pycurl

我想使用 foursquare Python api 获取 field 的详细信息。在我的浏览器上,我会点击以下网址:

https://api.foursquare.com/v2/venues/1738652/?oauth_token=FKJBZCPQNFMVBDNPBMYFVXPDTMV0RWWKJGNAORPCNFERGBIG&v=20131011

...并获得适当的响应。

我在这段代码中使用 pycurl 库:

c = pycurl.Curl()
c.setopt(c.URL, 'https://api.foursquare.com/v2/venues/1738652/')
c.setopt(c.POSTFIELDS, '?oauth_token=FKJBZCPQNFMVBDNPBMYFVXPDTMV0RWWKJGNAORPCNFERGBIG&v=20131011')
c.perform()

但出现以下错误:

{"meta":{"code":405,"errorType":"other","errorDetail":"This endpoint only supports GET."},"response":{}}

然后我尝试使用 GET 请求:

import pycurl
import cStringIO

buf = cStringIO.StringIO()

c = pycurl.Curl()
c.setopt(c.URL, 'https://api.foursquare.com/v2/venues/1738652/?oauth_token=FKJBZCPQNFMVBDNPBMYFVXPDTMV0RWWKJGNAORPCNFERGBIG&v=20131011')
c.setopt(c.WRITEFUNCTION, buf.write)
c.perform()

print buf.getvalue()
buf.close()

...但得到了同样的错误。

最佳答案

您可以对此进行试验。获取请求。启用 DebugFunction 以跟踪请求表单链接时发生的情况。

import pycurl

def body(buf):
    for item in buf.strip().split('\n'):
        if item.strip():
            print item

def test(debug_type, debug_msg):
    if len(debug_msg) < 300:
        print "debug(%d): %s" % (debug_type, debug_msg.strip())

conn = pycurl.Curl()  
#conn.setopt(pycurl.USERNAME, username)
#conn.setopt(pycurl.PASSWORD, password)
#conn.setopt(pycurl.SSL_VERIFYPEER, False)
#conn.setopt(pycurl.CUSTOMREQUEST, 'LIST *.mp3')
conn.setopt(pycurl.NOBODY, False)
conn.setopt(pycurl.HEADER, True)
conn.setopt(pycurl.VERBOSE, True)
conn.setopt(pycurl.URL, 'https://api.foursquare.com/v2/venues/1738652/?oauth_token=FKJBZCPQNFMVBDNPBMYFVXPDTMV0RWWKJGNAORPCNFERGBIG&v=20131011')  
conn.setopt(pycurl.DEBUGFUNCTION, test)
conn.setopt(pycurl.WRITEFUNCTION, body)
conn.perform()
conn.close()

输出

debug(0): About to connect() to api.foursquare.com port 443 (#0)
debug(0): Trying 185.31.16.185...
debug(0): Connected to api.foursquare.com (185.31.16.185) port 443 (#0)
debug(0): Initializing NSS with certpath: sql:/etc/pki/nssdb
debug(0): CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
debug(0): SSL connection using SSL_RSA_WITH_RC4_128_SHA
debug(0): Server certificate:
debug(0): subject: CN=*.a.ssl.fastly.net,O="Fastly, Inc.",L=San Francisco,ST=California,C=US
debug(0): start date: Jan 18 00:00:00 2013 GMT
debug(0): expire date: Jan 23 12:00:00 2014 GMT
debug(0): common name: *.a.ssl.fastly.net
debug(0): issuer: CN=DigiCert High Assurance CA-3,OU=www.digicert.com,O=DigiCert Inc,C=US
debug(2): GET /v2/venues/1738652/?oauth_token=FKJBZCPQNFMVBDNPBMYFVXPDTMV0RWWKJGNAORPCNFERGBIG&v=20131011 HTTP/1.1
User-Agent: PycURL/7.29.0
Host: api.foursquare.com
Accept: */*
debug(1): HTTP/1.1 200 OK
HTTP/1.1 200 OK
debug(1): Date: Wed, 30 Oct 2013 22:29:20 GMT
Date: Wed, 30 Oct 2013 22:29:20 GMT
debug(1): Server: nginx
Server: nginx
debug(1): Content-Type: application/json; charset=utf-8
Content-Type: application/json; charset=utf-8
debug(1): Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
debug(1): Tracer-Time: 168
Tracer-Time: 168
debug(1): X-RateLimit-Limit: 500
X-RateLimit-Limit: 500
debug(1): X-RateLimit-Remaining: 496
X-RateLimit-Remaining: 496
debug(1): Strict-Transport-Security: max-age=864000
Strict-Transport-Security: max-age=864000
debug(1): X-ex: fastly_cdn
X-ex: fastly_cdn
debug(1): Content-Length: 6420
Content-Length: 6420
debug(1): Accept-Ranges: bytes
Accept-Ranges: bytes
debug(1): Via: 1.1 varnish
Via: 1.1 varnish
debug(1): Age: 0
Age: 0
debug(1): X-Served-By: cache-am72-AMS
X-Served-By: cache-am72-AMS
debug(1): X-Cache: MISS
X-Cache: MISS
debug(1): X-Cache-Hits: 0
X-Cache-Hits: 0
debug(1): X-Timer: S1383172159.278296471,VS0,VE813
X-Timer: S1383172159.278296471,VS0,VE813
debug(1): Vary: Accept-Encoding,User-Agent,Accept-Language
Vary: Accept-Encoding,User-Agent,Accept-Language
debug(1): 
{"meta":{"code":200},"notifications":[{"type":"notificationTray","item":{"unreadCount":0}}],"response":{"venue":{"id":"4ba2857ff964a520ad0038e3","name":"SUBWAY","contact":{"phone":"+17179751975","formattedPhone":"+1 717-975-1975","twitter":"subway"},"location":{"address":"418 Market St","lat":40.245755,"lng":-76.89601481,"postalCode":"17043","cc":"US","city":"Lemoyne","state":"PA","country":"United States"},"canonicalUrl":"https:\/\/foursquare.com\/v\/subway\/4ba2857ff964a520ad0038e3","categories":[{"id":"4bf58dd8d48988d1c5941735","name":"Sandwich Place","pluralName":"Sandwich Places","shortName":"Sandwiches","icon":{"prefix":"https:\/\/ss1.4sqi.net\/img\/categories_v2\/food\/sandwiches_","suffix":".png"},"primary":true}],"verified":false,"stats":{"checkinsCount":333,"usersCount":115,"tipCount":2},"url":"http:\/\/www.subway.com","price":{"tier":1,"message":"Cheap"},"likes":{"count":0,"groups":[]},"like":false,"dislike":false,"menu":{"type":"Menu","label":"Menu","anchor":"See menu","url":"https:\/\/foursquare.com\/v\/subway\/4ba2857ff964a520ad0038e3\/menu","mobileUrl":"https:\/\/foursquare.com\/v\/4ba2857ff964a520ad0038e3\/device_menu"},"specials":{"count":0,"items":[]},"photos":{"count":2,"groups":[{"type":"venue","name":"Venue photos","count":2,"items":[{"id":"4e7a5ad91495668e6038ce76","createdAt":1316641497,"source":{"name":"foursquare for iPhone","url":"https:\/\/foursquare.com\/download\/#\/iphone"},"prefix":"https:\/\/irs2.4sqi.net\/img\/general\/","suffix":"\/DDYVIMDUJCS1ANMTICM2IFVQU0TKZPIZ3KSUA2CS5INLZNJ3.jpg","width":537,"height":720,"user":{"id":"8287910","firstName":"Andi","lastName":"H.","gender":"female","photo":{"prefix":"https:\/\/irs3.4sqi.net\/img\/user\/","suffix":"\/H1K2KGQPCWG0JS12.jpg"}},"visibility":"public"},{"id":"4e7a5b59483ba42a8feeb650","createdAt":1316641625,"source":{"name":"foursquare for iPhone","url":"https:\/\/foursquare.com\/download\/#\/iphone"},"prefix":"https:\/\/irs0.4sqi.net\/img\/general\/","suffix":"\/IWFRAETDTSN0DQ0AJ3JV4LW0YE30S4MZG0TMZRQP0RM3SP01.jpg","width":537,"height":720,"user":{"id":"13883535","firstName":"Skylar","lastName":"J.","gender":"female","photo":{"prefix":"https:\/\/irs2.4sqi.net\/img\/user\/","suffix":"\/AEIFIRAAY0IU5YIW.jpg"}},"visibility":"public"}]}]},"hereNow":{"count":0,"summary":"0 people here","groups":[]},"reasons":{"count":0,"items":[]},"createdAt":1268942207,"mayor":{"count":2,"user":{"id":"2480586","firstName":"Richard","lastName":"M.","gender":"male","photo":{"prefix":"https:\/\/irs0.4sqi.net\/img\/user\/","suffix":"\/LRIRRS1FAAJXE5C5.jpg"}}},"tips":{"count":2,"groups":[{"type":"others","name":"Tips from others","count":2,"items":[{"id":"4c2a64f6197e95210b72628d","createdAt":1277846774,"text":"I'm pretty sure I got food poisoning from this Subway last year. Had a high fever and intestinal distress of a rather strong sort for about 24 hours.","canonicalUrl":"https:\/\/foursquare.com\/item\/4c2a64f6197e95210b72628d","likes":{"count":1,"groups":[{"type":"others","count":1,"items":[]}],"summary":"1 like"},"like":false,"todo":{"count":0},"user":{"id":"1838130","firstName":"Chris","lastName":"B.","gender":"male","photo":{"prefix":"https:\/\/irs0.4sqi.net\/img\/user\/","suffix":"\/VNHRUMW1ZSCQ33IC.jpg"}}},{"id":"4c16608ca5eb76b0a0bac4b7","createdAt":1276534924,"text":"Skip the corn chowder","canonicalUrl":"https:\/\/foursquare.com\/item\/4c16608ca5eb76b0a0bac4b7","likes":{"count":1,"groups":[{"type":"others","count":1,"items":[]}],"summary":"1 like"},"like":false,"todo":{"count":0},"user":{"id":"28211","firstName":"Charles","lastName":"P.","gender":"male","photo":{"prefix":"https:\/\/irs0.4sqi.net\/img\/user\/","suffix":"\/YCHVMOLMKAIPYLUK.jpg"}}}]}]},"tags":[],"shortUrl":"http:\/\/4sq.com\/9uKowE","timeZone":"America\/New_York","listed":{"count":1,"groups":[{"type":"others","name":"Lists from other people","count":1,"items":[{"id":"4e69666081306988e64efc64","name":"Best places in New Cumberland, PA","description":"","type":"others","user":{"id":"13466057","firstName":"Kelsi","lastName":"K.","gender":"female","photo":{"prefix":"https:\/\/irs2.4sqi.net\/img\/user\/","suffix":"\/DPLEF4FZE55KG11A.jpg"}},"editable":false,"public":true,"collaborative":false,"url":"\/user\/13466057\/list\/best-places-in-new-cumberland-pa","canonicalUrl":"https:\/\/foursquare.com\/user\/13466057\/list\/best-places-in-new-cumberland-pa","canonicalPath":"\/user\/13466057\/list\/best-places-in-new-cumberland-pa","createdAt":1315530336,"updatedAt":1315530392,"followers":{"count":0},"listItems":{"count":5,"items":[{"id":"v4ba2857ff964a520ad0038e3","createdAt":1315530336}]}}]}]},"phrases":[{"phrase":"corn chowder","sample":{"entities":[{"indices":[9,21],"type":"keyPhrase"}],"text":"Skip the corn chowder"},"count":1}],"popular":{"status":"Likely open","isOpen":true,"timeframes":[{"days":"Today","includesToday":true,"open":[{"renderedTime":"6:00 AM\u20138:00 AM"},{"renderedTime":"10:00 AM\u20131:00 PM"},{"renderedTime":"4:00 PM\u20138:00 PM"}],"segments":[]},{"days":"Thu","open":[{"renderedTime":"6:00 AM\u20138:00 AM"},{"renderedTime":"10:00 AM\u20131:00 PM"},{"renderedTime":"4:00 PM\u20136:00 PM"}],"segments":[]},{"days":"Fri","open":[{"renderedTime":"6:00 AM\u20138:00 AM"},{"renderedTime":"10:00 AM\u20131:00 PM"},{"renderedTime":"4:00 PM\u20137:00 PM"}],"segments":[]},{"days":"Sat","open":[{"renderedTime":"7:00 AM\u20138:00 AM"},{"renderedTime":"11:00 AM\u20132:00 PM"},{"renderedTime":"4:00 PM\u20136:00 PM"}],"segments":[]},{"days":"Sun","open":[{"renderedTime":"10:00 AM\u201311:00 AM"},{"renderedTime":"2:00 PM\u20133:00 PM"},{"renderedTime":"6:00 PM\u20137:00 PM"}],"segments":[]},{"days":"Mon","open":[{"renderedTime":"6:00 AM\u20138:00 AM"},{"renderedTime":"11:00 AM\u20131:00 PM"},{"renderedTime":"4:00 PM\u20137:00 PM"}],"segments":[]},{"days":"Tue","open":[{"renderedTime":"6:00 AM\u20138:00 AM"},{"renderedTime":"11:00 AM\u20131:00 PM"},{"renderedTime":"4:00 PM\u20139:00 PM"}],"segments":[]}]},"pageUpdates":{"count":0,"items":[]},"attributes":{"groups":[{"type":"price","name":"Price","summary":"$","count":1,"items":[{"displayName":"Price","displayValue":"$","priceTier":1}]},{"type":"serves","name":"Menus","summary":"Dinner, Breakfast & more","count":6,"items":[{"displayName":"Breakfast","displayValue":"Breakfast"},{"displayName":"Brunch","displayValue":"Brunch"},{"displayName":"Lunch","displayValue":"Lunch"},{"displayName":"Dinner","displayValue":"Dinner"}]}]}}}}
debug(0): Connection #0 to host api.foursquare.com left intact
[Finished in 1.6s]

关于python - 如何使用 pycurl 在 foursquare 中获取 field 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19320779/

相关文章:

获取列表中最大项目的 Pythonic 方法

foursquare - 搜索端点开始返回组

Foursquare Explore API 半径参数不起作用

python - pycurl 失败但 curl(来自 bash)在 ubuntu 中工作

linux - (QNAP NAS)Linux - 安装 pycurl (libcurl.a 未找到)

Python:将文件分块上传到服务器,包括附加字段

python - 如何在python中解析重组文本?

python - 如何在Python IDE Spyder中安装 "graphics"python库/模块?

list - 使用 Foursquare API 添加项目到列表

python - 使用 scapy 指定数据包长度