API Request 403 Error

188 views
Skip to first unread message

Antonio Papa

unread,
Feb 27, 2016, 11:49:36 PM2/27/16
to thebluealliance-developers
Hello,

I'm trying to use the TBA API in Java and am running into some issues.

I tried this library (linked at bottom of http://www.thebluealliance.com/apidocs):

When I run it I get a 403 error (yes I set the App ID):

"java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.thebluealliance.com/api/v2/team/frc254"



I also tried my own code and got the same error:

public static void main(String[] args) throws MalformedURLException, IOException {
  URLConnection conn = (new URL("http://www.thebluealliance.com/api/v2/match/2016scmb_qm27")).openConnection();
  conn.setRequestProperty("X-TBA-App-Id", "AntonioPapa:test:v2");
  InputStream is = conn.getInputStream();
 }



The error occurs on the 'InputStream is = conn.getInputStream()" line.


Error 403 seems to represent that I'm 'forbidden' from access. Am I (and this library) doing something incorrect with trying to access the API? Is there a better/easy way to do this in Java?


Thanks,
Antonio

Phil Lopreiato

unread,
Feb 28, 2016, 12:15:31 AM2/28/16
to theblueallian...@googlegroups.com
Antonio,

What's really strange is that an invalid app id throws a 400 error, not a 403 like you're seeing. In fact, I don't think we ever explicitly throw a 403...

I'm able to hit the same endpoints with curl without error - can you reproduce the error with a different tool (e.g. curl or some other http library)?

The TBA Android app uses Square's Retrofit library to interact with the API. You can see the service interface for the API here: https://github.com/the-blue-alliance/the-blue-alliance-android/blob/master/android/src/main/java/com/thebluealliance/androidclient/datafeed/retrofit/APIv2.java. I wrote a simple Retrofit library while building the new datafeed for the app, you can test it out if you like here: https://github.com/phil-lopreiato/tba-apiv2-java

This is an interesting on, anybody else got any ideas?

Phil



Phil Lopreiato
--
You received this message because you are subscribed to the Google Groups "thebluealliance-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to thebluealliance-dev...@googlegroups.com.
To post to this group, send email to theblueallian...@googlegroups.com.
Visit this group at https://groups.google.com/group/thebluealliance-developers.
For more options, visit https://groups.google.com/d/optout.

Antonio Papa

unread,
Feb 28, 2016, 3:24:05 PM2/28/16
to thebluealliance-developers

Thanks, but I'd rather not install and figure out how to use Gradle and Maven. Is there something I can just open up in Eclipse (or add a jar to libraries) and have work?


I also don't understand what other tools I could use to try this, I don't want to download curl.

I've done some more poking around with this code:

  URL url = new URL("http://cheifdelphi.com");
  HttpURLConnection conn = (HttpURLConnection) (url).openConnection();
  conn.setRequestProperty("X-TBA-App-Id", "AntonioPapa:test:v3");
  int i = conn.getResponseCode();
  System.out.println(i);

If I use the above code and instead try to reach "chiefdelphi.com" or "google.com" I get a response code of 200. Meanwhile the TBA main page, "thebluealliance.com", responds with 403. It doesn't seem to be specifically an API problem. 
The library I was trying to use hasn't been updated since July 2014, has something related to this changed since then?

Thanks so much for your help.

Antonio Papa

unread,
Feb 28, 2016, 3:53:33 PM2/28/16
to thebluealliance-developers
OK, I got it working now. I added a line that makes it look like I'm using a browser.


URL url = new URL("http://www.thebluealliance.com/api/v2/team/frc254?X-TBA-App-Id=AntonioPapa:test:v7");
URLConnection conn = url.openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");

If that like isn't there I get a 403 error. With it everything is fine. My user ID is in the url, so I'm assuming this method is Kosher.
Are there any issues if I use the API this way? 

Phil Lopreiato

unread,
Feb 28, 2016, 4:06:15 PM2/28/16
to theblueallian...@googlegroups.com
Happy to hear you go it!

This is still pretty strange, maybe CloudFlare forbids certain User-Agents as a measure of DDOS protection? Anybody else heard of this before?

Also, it makes no different whether you provide your TBA-App-Id as a request header or url parameter. They are both checked: https://github.com/the-blue-alliance/the-blue-alliance/blob/master/controllers/api/api_base_controller.py#L134

Phil



Phil Lopreiato
To unsubscribe from this group and stop receiving emails from it, send an email to thebluealliance-dev...@googlegroups.com.

Andrew Dickinson

unread,
Feb 29, 2016, 12:59:49 PM2/29/16
to thebluealliance-developers
I've been using the Apache HTTPClient library in Java without setting a user agent. Maybe it has a reasonable default?

Michael Hill

unread,
Mar 3, 2016, 8:15:47 PM3/3/16
to theblueallian...@googlegroups.com
I just noticed this issue on my Python API access as well. Once I added a User-Agent header, it seemed to work. Thanks Antonio for pointing that out.

Michael

On Mon, Feb 29, 2016 at 12:59 PM, Andrew Dickinson <fortknox...@gmail.com> wrote:
I've been using the Apache HTTPClient library in Java without setting a user agent. Maybe it has a reasonable default?
Reply all
Reply to author
Forward
0 new messages