steemconnect-python-client is a simple yet powerful library to interact with the Steemconnect. There was no production ready library for Python (Or I couldn't find it.) so I have decided to write my own.
SteemConnect V2 implements OAUTH standards. If you don't know about it, you can read the SteemConnect wiki to learn the workflow and the token based authorization.
Also, you need to register your app into steemconnect before working with them. This will provide client_id
and client_secret
information which you will need to interact with the API.
$ (sudo) pip install steemconnect
c = Client(
client_id="app_name",
client_secret="client_secret",
)
After getting the Client instance:
auth_url = c.get_login_url(
"callback_url_to_your_app",
"login,vote",
)
This will return a full login URL which you redirect users and after they login, you get the access_token in the query string with the specified callback url.
Access tokens has a limited lifetime, so if you need to refresh them in your own without authenticating the user again, you can add get_refresh_token=True
parameter to this function.
If you set this as True, callback url will have a authorization code instead of an access token. With this code, you can ask access tokens first:
tokens = c.get_access_token(
code,
)
Example output:
{
'access_token': 'access_token_string',
'expires_in': 604800,
'username': 'emrebeyler',
'refresh_token': 'refresh_token_string'
}
As you can see, the lifetime of this token is 604800 seconds. After it expires, you can refresh it with refresh_access_token
method.
c.refresh_access_token(
access_token_response.get("refresh_token"),
"login,vote" # scopes
)
This method revokes the passed access token.
self.c.revoke_token(tokens.get("access_token"))
Once you get the access token, you can create a new Client instance with just access_token.
c = Client(
access_token="access_token",
)
This endpoint returns information about the authorized user.
print(c.me())
Updates the metadata of authorized user.
metadata = {
"profile": {
"name": "Emre",
"location": "Istanbul, Turkey",
"about": "Developer, STEEM witness.",
"profile_image": "http://foo.bar/image.png"
}
}
resp = self.c.update_user_metadata(metadata)
All supported operations are located at the steemconnect.operations
module.
vote = Vote("account", "author", "permlink", percent)
c.broadcast([vote.to_operation_structure()])
comment = Comment(
"author",
"permlink",
"body",
title="test title",
json_metadata={"app":"foo/0.0.1"},
)
c.broadcast([comment.to_operation_structure()])
comment = Comment(
"author",
"permlink",
"body",
title="test title",
json_metadata={"app":"foo/0.0.1"},
)
comment_options = CommentOptions(
parent_comment=comment,
allow_curation_rewards=False,
)
c.broadcast([
comment.to_operation_structure(),
comment_options. comment.to_operation_structure()
])
follow = Follow("follower", "following")
c.broadcast([follow.to_operation_structure()])
unfollow = Unfollow("follower", "following")
c.broadcast([unfollow.to_operation_structure()])
ignore = Mute("follower", "following")
c.broadcast([ignore.to_operation_structure()])
resteem = Resteem("account", "author", "permlink")
c.broadcast([resteem.to_operation_structure()])
claim_reward_balance = ClaimRewardBalance('account', '0.000 STEEM', '1.500 SBD', '1132.996000 VESTS')
c.broadcast([claim_reward_balance.to_operation_structure()])
delete_comment = DeleteComment(
"author", "permlink"
)
c.broadcast([delete_comment.to_operation_structure()])
custom_json = CustomJson(
required_auth,
required_posting_auths,
id
json_structure,
)
c.broadcast([custom_json.to_operation_structure()])
hot_sign() method creates a SteemConnect specific URL which you can redirect users and expect them to broadcast operations are not supported in the api. (transfer, create_delegation, etc.)
It has an optional redirect_uri parameter. If you pass that information, SteemConnect will redirect the user to that URL after the operation succeeds.
Example usage:
url = self.c.hot_sign(
"transfer",
{
"to": "emreberyler",
"amount": "0.001 SBD",
"memo": "Donation",
},
redirect_uri="http://localhost"
)
This will generate this URL, (which sends 1 SBD to me with a memo as "Donation".)