troubleshooting Question

How Do I Pass JSON in header of Python Script?

Avatar of MJ
MJFlag for United States of America asked on
Web DevelopmentPythonJSON
4 Comments1 Solution13 ViewsLast Modified:
First of all, let me say that I've never done Python before. I want to know how I can pass JSON
  "data": {
    "attributes": {
      "url": "",      
      "subscriptions": [        

Open in new window

Into the header of my script. Here is the test script (I didn't write it and I have removed sensitive data, keys etc.)
#To install Requirements, use: pip3 install -r requirements.txt

import json
import time
import jwt
import datetime
import os
from datetime import datetime, timedelta, timezone, date
import requests
import csv
import sys

# BOTW Account

org_id = ""
tech_id = ""
api_key = ""
secret = ""
company_id = ""
key_path = ""
property_id = ""

def createJWT():

   expiryTime = int(time.time()) + 300

   data = {
      "exp": expiryTime,
      "iss": org_id,
      "sub": tech_id,
      "": True,
      "aud": "" + api_key
   with open(key_path,"rb") as readFile:
      privateKey =
   encoded = jwt.encode(data, privateKey, algorithm="RS256")
   #print('encoded: ', encoded)
   return encoded

#print("createJWT: ", createJWT())

def accessToken():

   encoded = createJWT()
   #print("encoded: ", encoded)
   #encoded = "eyJhbGciOiJSUzI1NiJ9"
   exchangeEndpoint = ""
   header_jwt = {'cache-control':'no-cache','content-type':'application/x-www-form-urlencoded'} ## setting the header

   exchangeData = {
      "client_id": api_key,
      "client_secret": secret,
      "jwt_token": encoded,
      'x-api-key': api_key,
       'x-gw-ims-org-id': org_id

   #print("exchangeData: ", json.dumps(exchangeData, indent=4, sort_keys=True))

   exchangeResponse =, headers=header_jwt, data=exchangeData)

   #print("exchangeResponse: ", exchangeResponse.text)

   token = exchangeResponse.json()['access_token']
   #token = exchangeResponse.json()

   #token = "eyJhbGciOiJS"

   #print("token: ", json.dumps(token, indent=4, sort_keys=True))
   return token

def checkKey(dict, key): 
    if key in dict.keys(): 
        return True
        return False 

def apiConn(url):
   token = accessToken()
   #print('token: ', token)
   #token = token['accessToken']
   #print("bearer Token: ", token)

   #header_jwt = {'cache-control':'no-cache','content-type':'application/x-www-form-urlencoded'} ## setting the header

   # create payload
   header_jwt = {
       'authorization' : 'Bearer ' + token,
       'Accept' : 'application/vnd.api+json;revision=1',
       'Content-Type': 'application/vnd.api+json',
       'x-api-key': api_key,
       'x-gw-ims-org-id': org_id
   #print('url: ', url)
   response = requests.get(url, headers=header_jwt)
   #print('response: ',    response)
   apiResponse = response.json()
   return apiResponse

#url = "" + property_id + "/rules"
url = "" + property_id + "/callbacks"
#print('url: ', url)

apiResponse = apiConn(url)
print('apiResponse', json.dumps(apiResponse,indent=4, sort_keys=True))
print('Processing Complete')

Open in new window


Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 4 Comments.
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>


Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 4 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004