我有一个程序可以在Foursquare上自动签入。直到大约两天前它开始返回Response [403]时,它的工作情况一直很好。奇怪的是,我发现一些示例代码可通过PHP进行相同的操作,并且运行良好。
PHP和Python版本都使用相同的Oauth令牌。
PHP版本:
<?php
$fields_string = http_build_query(array(
'oauth_token'=>'***REDACTED***',
'venueId'=> '4e769e971838f9188a5e2a03',
'v'=>'20180801',
'broadcast'=>'public'
));
$url = "https://api.foursquare.com/v2/checkins/add";
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$checkin = curl_exec($ch);
// Check if any error occured
if(curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
//close connection
curl_close($ch);
$json=json_decode($checkin);
echo $checkin;
echo $json->meta->code;
?>
Python版本:
import json, requests
checkinURL = 'https://api.foursquare.com/v2/checkins/add'
checkinParams = dict(
oauth_token = '***REDACTED***',
venueId = '4e769e971838f9188a5e2a03',
v = 20180801,
broadcast = 'public'
)
checkin = requests.post(url=checkinURL, params=checkinParams)
print (checkin)
这是从Python进行的调试:
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.foursquare.com:443
send: b'POST /v2/checkins/add?oauth_token=***REDACTED***&ll=21.281656%2C-157.677465&limit=1&venueId=4e769e971838f9188a5e2a03&shout=Testing&v=20180801&broadcast=public HTTP/1.1\r\nHost: api.foursquare.com\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n'
reply: 'HTTP/1.1 403 Unauthorized\r\n'
header: Server: Varnish
header: Retry-After: 0
header: Content-Length: 2713
header: Content-Type: text/html
header: Accept-Ranges: bytes
header: Date: Mon, 29 Oct 2018 06:46:48 GMT
header: Via: 1.1 varnish
header: Connection: close
header: X-Served-By: cache-bur17527-BUR
header: X-Cache: MISS
header: X-Cache-Hits: 0
DEBUG:urllib3.connectionpool:https://api.foursquare.com:443 "POST /v2/checkins/add?oauth_token=***REDACTED***&ll=21.281656%2C-157.677465&limit=1&venueId=4e769e971838f9188a5e2a03&shout=Testing&v=20180801&broadcast=public HTTP/1.1" 403 2713
<Response [403]>
答案 0 :(得分:0)
解决了!
我添加了
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
然后添加
headers=headers
访问requests.post。