Trying to connect to REST-API with python in dynamo

Hey Guys,

I’m trying to connect to the rest API by using python in Dynamo-Revit. I created an acces-token but it seems to be invalid although i should have the right authorisation. The script that i’m using is as shown below. Currently my role is ‘Contributer’ to the workspace. The http error-code i get is 403 “Your token is not valid”. Is this maybe because i should encrypt the token first can it be something else? Thanks in advance for helping me!

header={
    'Authorization': 'Bear {{TOKEN}}',
    'Accept': 'application/json'
  }
url_get="https://app.speckle.systems/api/getobjects/6a87e55c77"

response=requests.post(url=url_get,headers=header)

if response.status_code == 201:
    OUT=response.content
else:
    OUT=response.text

FIRST RULE OF TOKENS: These are passwords, protect them as such.

With that token police message out of the way. You will likely need to add minimum pta scopes for stream:read but also user:read or user-profile:read I forget which.

Thanks for the feeedback. I just changed a couple of the characters but is see how that maybe is not sufficient :man_facepalming:

So i made a new token with the scopes below but i still seem to have the same issue. Could it be something else?

image

Sorry I should have spotted before - that endpoint is expecting a POST request that also includes a list of objects to get.

# Replace with your actual token and ensure no leading/trailing spaces
TOKEN = "your_actual_token_here"
project_id = "6a87e55c77"  # Replace with your project ID

header = {
    'Authorization': f'Bearer {TOKEN}',
    'Accept': 'application/json'
}

url_get = f"https://app.speckle.systems/api/getobjects/{project_id}"

# Replace with actual object IDs you want to request
body = {
    "objects": ["objectId1", "objectId2", "objectId3"]  # Example IDs
}

response = requests.post(url=url_get, headers=header, json=body)

Thanks for the feedback. The problem however seems to be with the authorisation of the token.

When i use the code above i get:
response_code 500: Unexpected token c in JSON at position 0, code: SyntaxError

When i use the code below as header is get a 403 “Your token is not valid” message .

header={
    "Authorization": f"Bear {Token}",
    "Accept": "application/json"
  }

In both situations i now added the body with a valid objectID (i think)

BearER ?

"Authorization": f"Bearer {Token}",  # Ensure correct capitalisation and formatting

Yes when i use this code i get:

response_code 500: Unexpected token c in JSON at position 0, code: SyntaxError

header={
    "Authorization": f"Bearer {Token}",
    "Accept": "application/json"
  }

Were you to send a bare request from commandline:

curl -X POST \
  -H "Authorization: Bearer your_actual_token_here" \
  -H "Accept: application/json" \
  -d '{"objects": ["validObjectId1"]}' \
  https://app.speckle.systems/api/getobjects/6a87e55c77

Do you still see the same?

Yes

curl -X POST ^
More?   -H "Authorization: Bearer my_token" ^
More?   -H "Accept: application/json" ^
More?   -d "{\"objects\": [\"cf02d5566caa6c38c3e2a7d9154168f4\"]}" ^
More?   https://app.speckle.systems/api/getobjects/6a87e55c77
{"error":{"message":"Unexpected token u in JSON at position 0","code":"SyntaxError"}}

The curl statement should have included the Content-Type header to send the data in valid format:

curl -X POST \
   -H "Authorization: Bearer my_token" \
   -H "Accept: application/json" \
   -H "Content-Type: application/json" \
   -d "{\"objects\": \"[\\\"cf02d5566caa6c38c3e2a7d9154168f4\\\"]\"}" \
   https://app.speckle.systems/api/getobjects/6a87e55c77

Note that the content of objects should be a string, not a JSON object.

In Python, I think this would be:

body = {
    "objects": "[\"objectId1\", \"objectId2\", \"objectId3\"]")  # Example IDs in JSON stringified array
}

When adding the Content_Type -key i get the following response:
{“error”:“Your token is not valid.”}

Is it true that i need to use an acces token as generated in this section:

Yes. Please create a new access token with at least streams:read scope to use the /api/getobjects API paths.

You may need other scope depending on any other paths you wish to use your token for, e.g. streams:write if you intend to send objects to Speckle.

The Access Token is a secret and should not be shared with others.

Iain

Apologies, @DonnyRH im looking into this more deeply and am able to reproduce the error with a fully scoped token and the correct endpoint

I’ll report back as I know more

@jonathon and @iainsproat, I seem to get a little bit further. With the code down below i got a valid “200” status_code. The issue now is that i now get an empty list where i was expecting data.

header={
    "Authorization": f"Bearer {Token}",
    "Accept": "application/json",
    "Content-Type": "application/json"
  }
  
body = {
    'objects': ['"cf02d5566caa6c38c3e2a7d9154168f4"']  
}  

url_get=f"https://app.speckle.systems/api/getobjects/{IN[0]}"

with requests.post(url=url_get,headers=header,json=body,stream=True) as r:
    OUT=r

Its unclear to me if i need to use the model URL or Project URL (custom property - Speckle test kolommen | Speckle) (Speckle test kolommen | Speckle) when trying to extract an object.
Also is this the right id to take:
present in the body?