While getting the access token , error found

Hi Everyone,

I am facing an issue while getting the access token from Python file, I have generated the access token working with the robot file test_suites = [“test_suite_1.robot”, “test_suite_2.robot”] , but I need to run individual robot files, by setting to environment variables, but it says as access token not found,

os.environ[“access_token”] = access_token
access_token = json.loads(response.text).get("access_token")
return access_token


def main() :
access_token = get_access_token()
# Define the Robot Framework script names or file paths
robot_files = [“GET_Installedproduct.robot”]


# Run the Robot Framework scripts in sequence
for robot_file in robot_files:
    run_robot_script(access_token,robot_file)
    time.sleep(300)


if **name** == ‘**main**’:
main( )

-this works, however, When I try the below code doesn’t work
os.environ[“access_token”] = access_token

# Extract the access token
access_token = json.loads(response.text).get("access_token")
return access_token


def main() :
access_token = get_access_token()

if **name** == ‘**main**’:
main()

robot file :

${access_token}= Get Environment Variable access_token
Log Access Token: ${access_token}

Please help me with this issue.

Thanks,
Bhuvi

Hi Bhuvi,

i cannot directly answer to your question as I have a bit of trouble understanding the code snippets.
For example, I cannot really see what run_robot_script does and how it passes the arguments like access_token into the robot.run command.

But anyway, I have a different example here which solved a similar problem a bit differently:
We retrieve a token in one step in the beginning and later share it to other test cases.

There, the Keyword Authenticate as Admin is executed as a Suite Setup and the retrieved ${token} is shared as a Suite Variable to other test cases (for example it is used in the Delete Booking Test Case).

Would such a solution work in your case as well?

You can also define a global Suite Setup which is executed once for multiple Test Suites using __init__.robot files.

Just like @Many said, code you shared does not really help out.

All i can point out that modifying os.environ like you do here does not quarantee that the specific key/value you set to your env is available on every application. Only direct child processes will inherit that value but if process is spawned by something else, those wont see that value. It also could be that the environment is “reset” by some code later on.

Also worth pointing out:

os.environ[“access_token”] = access_token
access_token = json.loads(response.text).get("access_token")
return access_token

makes no sense at all logic wise …

Hi ,
import os
import json
import requests
import jwt
import time

def run_robot_script(access_token,robot_file ):
# Run the specified Robot Framework script with the access token as an argument
os.system(f"robot --variable access_token:{access_token} {robot_file}")

def get_access_token():
# token generation
private_key = “”“----- PRIVATE KEY-----”“”

now = int(time.time())
expiration_time = now + 7200

jwt_payload = {
    "aud" : ["https://" + "example.com" + "/oauth2/access_token"],
    "iss" : "example.com",
    "sub" : "example.com",
    "exp" : expiration_time
}

jwt_header = {
    "alg" : "RS256",
    "typ" : "JWT"
}

assertion = jwt.encode(jwt_payload, private_key, algorithm="RS256", headers=jwt_header)

# Make the POST request
url = "https://example/authorize/oauth2/token"
headers = {"Content-Type" : "application/x-www-form-urlencoded"}

data = {
    "grant_type" : "urn:ietf:params:oauth:grant-type:jwt-bearer",
    "assertion" : assertion
}

response = requests.post(url, headers=headers, data=data)

# Extract and print the access token
response_data = json.loads(response.text)
access_token = response_data.get("access_token")
print(f"Access Token: {access_token}")
# Set the access token as an environment variable
os.environ["access_token"] = access_token

# Extract the access token
access_token = json.loads(response.text).get("access_token")
return access_token

def main():
# Get access token
access_token = get_access_token()
# Define the Robot Framework script names or file paths
robot_files = [“GET_Installedproduct.robot”]
# # Run the Robot Framework scripts in sequence
for robot_file in robot_files:
run_robot_script(access_token, robot_file)
time.sleep(300)

if name == ‘main’:
main()

does this code help to resolve an issue I am facing that I don’t want to run the robot file inside the Python file , I need to get the access token and call the token in the robot file i.e., environmental variable is set for the access token so, like
${access_token}= Get Environment Variable access_token
Log Access Token: ${access_token}

Thanks

Looking at your function to start the execution in robot framework

def run_robot_script(access_token,robot_file ):
  os.system(f"robot --variable access_token:{access_token} {robot_file}")

In general, I would recommend using the python entry points that robotframework offers to start the test execution instead of os.system. Either by using robot.run_cli or robot.run
But the 2nd thing is:
You use the --variable option set overrvide the Variable with the name access_token.
When you do that, it means that the variable ${access_token} is defaulted during that test run.
So you should be able to do a
Log ${access_token}
anywhere in your tests and you should see it’s value.

There is no need to do a
${access_token}= Get Environment Variable access_token
before (as you showed in your snippet). If you do that, Robot Framework would try set the variable ${access_token} again by retrieving an environment variable value - this is not needed as you already passed the variable directly from the command-line.

We need to distinguish here between the environment variables and the robot variables.
When you hand over robot variables using --variables myVar:myValue , they are globally available in your test run.

1 Like