[PYTHON] Use configparser when using API

Be in trouble

I want to check the margin balance of bitflyer lightning with python, but API_KEY and API_SECRET never want to stick to the source code.

solution

Prepare config.txt separately and describe API_KEY and API_SECRET. Use configparser. https://docs.python.org/ja/3/library/configparser.html

config.txt


[bf]
api_key = <API here_Enter KEY>
api_secret = <API here_Enter SELECT>

Margin acquisition.py


import configparser
import hmac
import datetime
import hashlib
import requests

config = configparser.ConfigParser()

config.read('./config.txt')
API_KEY = config['bf']['api_key']
API_SECRET = config['bf']['api_secret']

print(getcollateral())

#Margin confirmation
def getcollateral():
    api_key = API_KEY
    api_secret = API_SECRET

    base_url = "https://api.bitflyer.jp"
    path_url = "/v1/me/getcollateral"
    method = "GET"

    timestamp = str(datetime.datetime.today())
    message = timestamp + method + path_url

    signature = hmac.new(bytearray(api_secret.encode('utf-8')), message.encode('utf-8') , digestmod = hashlib.sha256 ).hexdigest()

    headers = {
        'ACCESS-KEY' : api_key,
        'ACCESS-TIMESTAMP' : timestamp,
        'ACCESS-SIGN' : signature,
        'Content-Type' : 'application/json'
    }

    response = requests.get( base_url + path_url , headers = headers)
    return response.json()
{'collateral': 5070.0, 'open_position_pnl': 0.0, 'require_collateral': 0.0, 'keep_rate': 0.0}

For how to use the API, I referred to the post by the following person. Thank you very much. https://qiita.com/sodiumplus3/items/b69dbd3e51fc2a0f7e01

Recommended Posts

Use configparser when using API
Proxy measures when using WEB API
Use JIRA API
[Google Cloud Platform] Use Google Cloud API using API Client Library
Load test method when using external service API
Summary when using Fabric
Precautions when using Chainer
Initial settings when using the foursquare API in python
(Personal) points when using ctypes
Environment variables when using Tkinter
Use Trello API with python
When using optparse with iPython
Test CloudStack API Using Simulator
Use Twitter API with Python
Age recognition using Pepper's API
Try using the Twitter API
Upload videos using YouTube API
DEBUG settings when using Django
Try using the Twitter API
Use "% tensorflow_version 2.x" when using TPU with Tensorflow 2.1.0 in Colaboratory
Try using the PeeringDB 2.0 API
When using if and when using while
File structure when using serverless-python-requirements
Use subsonic API with python3
Use e-Stat API from Python
When using property, use a class that inherits object (new-style class)
Small speedup when using pytorch
[SEO] Flow / sample code when using Google Analytics API in Python
A memorandum when using beautiful soup
Variable scope when using internal functions
Precautions when using pit in Python
Data acquisition using python googlemap api
Precautions when using TextBlob trait analysis
Logo detection using TensorFlow Object Detection API
[Python3] Google translate google translate without using api
Precautions when using codecs and pandas
Try using Pleasant's API (python / FastAPI)
Precautions when using the urllib.parse.quote function
Use API not implemented in twython
Get Amazon data using Keep API # 1 Get data
Use kabu StationĀ® API from Python
[Python] Be careful when using print
How to use OpenPose's Python API
Data acquisition memo using Backlog API
Use the Flickr API from Python
Try using Python argparse's action API
How to use bing search api
Facial expression recognition using Pepper's API
Create a CRUD API using FastAPI
Run Ansible from Python using API
Precautions when using phantomjs from python
ResourceWarning when using requests: unclosed workaround
How to use the ConfigParser module
Use gcc-4.2 when compiling Python (MacOS)
[Python] How to use Typetalk API
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
[VS Code] ~ Tips when using python ~
Use Google Analytics API from Python
When using regular expressions in Python
I tried using the checkio API