09-04-2024 01:43 PM - edited 09-04-2024 02:20 PM
Hello,
I was running the following WebEx API and ran into an authorization header issue.
I guess it does not like using env variables in authorization header.
https://developer.webex.com/docs/getting-started#personal-access-tokens
#!/usr/bin/Pyhon3
import json
import requests
URL = "https://webexapis.com/v1/teams"
PAYLOAD = {
"name": "DevNet Associate Certification Team"
}
HEADERS = {
"Authorization": "Bearer $TOKEN",
"Content-Type": "application/json"
}
RESPONSE = requests.request("POST", URL, data=json.dumps(PAYLOAD), headers=HEADERS)
print(RESPONSE.text)
Note:
Personal ACCESS TOKEN has been copied from Developer WebEx page and saved as env:
$ export TOKEN="ZDMxNzZlNGEtMjE2Zi00NDFhLThiNWQtZmY5ZTUzZmQ4Y2Q3NThhZjRhN2ItZDll_P0A1_3e53fd2c-fe2b-4014-8e2d-c5af7b0ff23d“
$ printenv TOKEN
ZDMxNzZlNGEtMjE2Zi00NDFhLThiNWQtZmY5ZTUzZmQ4Y2Q3NThhZjRhN2ItZDll_P0A1_3e53fd2c-fe2b-4014-8e2d-c5af7b0ff23d
https://developer.webex.com/docs/getting-started#personal-access-tokens
Console Output:
{"message":"The request requires a valid access token set in the Authorization request header.","errors":[{"description":"The request requires a valid access token set in the Authorization request header."}],"trackingId":"ROUTERGW_f7df2b11-994e-46ea-adba-6d33b0418ce3"}
Please advise.
Thanks.
Solved! Go to Solution.
09-05-2024 12:09 AM
To access your environment variable use os.getenv():
#!/usr/bin/Pyhon3
import json
import requests
import os
URL = "https://webexapis.com/v1/teams"
PAYLOAD = {
"name": "DevNet Associate Certification Team"
}
HEADERS = {
"Authorization": f"Bearer {os.getenv('TOKEN')}",
"Content-Type": "application/json"
}
RESPONSE = requests.request("POST", URL, data=json.dumps(PAYLOAD), headers=HEADERS)
print(RESPONSE.text)
09-09-2024 05:43 AM
Glad it worked, please dont' foreget to mark the answer as accecpted solution
CURL is running in a Linux Shell/Bash where you access environment variables with $<ENVNAME>, however in Python you need to use a module (like the os module in the example above) in order to access env vars.
09-05-2024 12:09 AM
To access your environment variable use os.getenv():
#!/usr/bin/Pyhon3
import json
import requests
import os
URL = "https://webexapis.com/v1/teams"
PAYLOAD = {
"name": "DevNet Associate Certification Team"
}
HEADERS = {
"Authorization": f"Bearer {os.getenv('TOKEN')}",
"Content-Type": "application/json"
}
RESPONSE = requests.request("POST", URL, data=json.dumps(PAYLOAD), headers=HEADERS)
print(RESPONSE.text)
09-06-2024 01:17 PM
Thank you Marcel, this worked.
When using cURL the format for environmental token seems to be $<TOKEN>
Example:
09-09-2024 05:43 AM
Glad it worked, please dont' foreget to mark the answer as accecpted solution
CURL is running in a Linux Shell/Bash where you access environment variables with $<ENVNAME>, however in Python you need to use a module (like the os module in the example above) in order to access env vars.
09-05-2024 12:42 AM
Please be also aware the the dev token is valid for 12 hours only. After that, you have to grab a new one from the page.
Better would be to create a real integration (or bot or service app) and to renew the token periodically.
If you have Webex API related questions, the developer community forum is also a good place to post. The support team is watching that forum as well: https://community.cisco.com/t5/webex-for-developers/bd-p/disc-webex-developers
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide