cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
265
Views
0
Helpful
4
Replies

WebEx API Create Team - Access Token Issue

Netmart
Level 1
Level 1

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.

 

2 Accepted Solutions

Accepted Solutions

Marcel Zehnder
Spotlight
Spotlight

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)

View solution in original post

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.

View solution in original post

4 Replies 4

Marcel Zehnder
Spotlight
Spotlight

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)

Thank you Marcel, this worked.

When using cURL the format for environmental token seems to be $<TOKEN>

Example:

>  -H "Authorization:Bearer $TOKEN" \
>  -H "Content-Type: application/json" \
> -d{"roomid":"d6qce5aLZkt6VLRD"."text":"Welcome to Webex!"}

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.

dtibbe
VIP
VIP

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