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": "https://www.example.com",      
      "subscriptions": [        
          "rule.created"      
      ]    
   }  
 } 
}

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
#Documentation: https://developer.adobelaunch.com/api/

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,
      "https://ims-na1.adobelogin.com/s/ent_reactor_sdk": True,
      "aud": "https://ims-na1.adobelogin.com/c/" + api_key
   }
 
   with open(key_path,"rb") as readFile:
      privateKey = readFile.read()
 
   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 = "https://ims-na1.adobelogin.com/ims/exchange/jwt"
   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 = requests.post(exchangeEndpoint, 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
    else: 
        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 = "https://reactor.adobe.io/properties/" + property_id + "/rules"
url = "https://reactor.adobe.io/properties/" + 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


Thanks!

ASKER CERTIFIED SOLUTION
leakim971
Multitechnician
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.
or
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 >>

Mike

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