How to use the FIRST Inspires API

I’m having trouble working with the first inspires api. I can’t seem to get it to output what I want it to. Here’s a code sample:

auth = "my auth token"

import http.client

conn = http.client.HTTPSConnection("frc-api.firstinspires.org")
payload = ''
headers = {
  'Authorization': 'Basic {auth}',
  'If-Modified-Since': ''
}
conn.request("GET", "/v3.0/2023/awards/team/2815", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

I’ve replaced the auth token with the one that was emailed to me after I registered for API access but the print statement still doesn’t show anything:

PS C:\Users\Elliot Scher\Desktop\Work\FRC-2815\Stats> & "C:/Users/Elliot Scher/AppData/Local/Programs/Python/Python310/python.exe" "c:/Users/Elliot Scher/Desktop/Work/FRC-2815/Stats/Collector.py"

PS C:\Users\Elliot Scher\Desktop\Work\FRC-2815\Stats> 

Could anyone help me with this? Thanks!!

Are you encoding your token? From the documentation:

sampleuser:7eaa6338-a097-4221-ac04-b6120fcc4d49

This string must then be encoded using Base64 Encoded to form the Token, which will be the same length as the example above, but include letters and numbers. For our example, we would have:

c2FtcGxldXNlcjo3ZWFhNjMzOC1hMDk3LTQyMjEtYWMwNC1iNjEyMGZjYzRkNDk=

You also aren’t specifying what return type you want (application/xml or application/json).

A known working snippet in Java that you should be able to translate is below. Of note, the path is the full URL to the desired endpoint, and clearAuth is the username:password that was emailed by FIRST.

            URL url = new URL (path);
            
            String encodedAuth = new String(Base64.encodeBase64(clearAuth.getBytes()));
            
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty  ("Authorization", "Basic " + encodedAuth);
            connection.setRequestProperty("Accept", "application/xml");
            connection.setHostnameVerifier(new Verifier());
            stream = (InputStream)connection.getInputStream();

'Authorization': 'Basic {auth}',

You probably want an f string for this. Right now your authorization header is literally just 'Basic {auth}', instead of using the actual value of the auth variable. Try using 'Authorization: f'Basic {auth}' instead.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.