简体   繁体   中英

Get request with Access and Secret Key

I am a newbie to Python. I am trying to write a Python script to get some JSON data. The Linux shell command is as below and it works perfectly when fired form shell:-

curl -H "X-ApiKeys: accessKey={accessKey}; secretKey={secretKey}" https://example.com/example/api/v1/example/list

Below is the Python script as far as I have reached till now and got stuck and absolutely no idea what to do next.

import requests
import ConfigParser
import json
Config = ConfigParser.ConfigParser()
Config.read('C:/Users/admin/source/repos/MyProject/MyProject/config.ini')
Username = Config.get('credentials',  'Username')
Password = Config.get('credentials', 'Password')

So I don't have a login for the API, but the general program should look like this

import requests
import ConfigParser
import json

config = ConfigParser.ConfigParser()
config.read('C:/Users/admin/source/repos/MyProject/MyProject/config.ini')
username = config.get('credentials',  'Username')
password = config.get('credentials', 'Password')

request_url = 'example_url'
headers = {'X-ApiKeys' : 'accessKey=' + username + '; secretKey=' + password}

print requests.get(request_url,headers=headers).json()

I changed the variables declared to be lowercase just to follow python conventions. Keys are generally put in the header of the request which is a dictionary in python's requests library. If that doesn't work, feel free to include the error and I will try to make my answer work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM