0

I have a program which will do automatic checkins on Foursquare. It was working fine up until about a couple days ago when it started returning Response [403]. What is weird is I found some sample code to do the same thing via PHP and it is working fine.

The same Oauth token is being used for both the PHP and Python version.

PHP Version:

<?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 Version:

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)

Here's the debug from 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]>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Bryan H
  • 1
  • 1
  • Possible duplicate of [Foursquare api consumer disabled](https://stackoverflow.com/questions/40725074/foursquare-api-consumer-disabled) – Kosuke Ogawa Oct 30 '18 at 00:34
  • It looks like they introduced request blocking based on user-agent… I had the same issue with curl on CLI and with a Go HTTP client. Setting the user agent to a browser (`Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0`) made the error go away… Seems like the PHP-Curl user agent is white listed by default and curl / Go HTTP client are not… – Knut Oct 30 '18 at 11:07
  • I think you're right. When I added a user-agent to my code it started working again. 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'} – Bryan H Oct 31 '18 at 21:40

1 Answers1

0

Solved!

I added

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'}

Then added

headers=headers

to the requests.post.

Bryan H
  • 1
  • 1