本文整理汇总了Python中mastodon.Mastodon.create_app方法的典型用法代码示例。如果您正苦于以下问题:Python Mastodon.create_app方法的具体用法?Python Mastodon.create_app怎么用?Python Mastodon.create_app使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mastodon.Mastodon
的用法示例。
在下文中一共展示了Mastodon.create_app方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create_app
# 需要导入模块: from mastodon import Mastodon [as 别名]
# 或者: from mastodon.Mastodon import create_app [as 别名]
def test_create_app(mocker, to_file=None, redirect_uris=None, website=None):
# there is no easy way to delete an anonymously created app so
# instead we mock Requests
resp = Mock()
resp.json = Mock(return_value=dict(
client_id='foo',
client_secret='bar',
))
mocker.patch('requests.post', return_value=resp)
app = Mastodon.create_app("Mastodon.py test suite",
api_base_url="example.com",
to_file=to_file,
redirect_uris=redirect_uris,
website=website
)
assert app == ('foo', 'bar')
assert requests.post.called
示例2: test_app_account_create
# 需要导入模块: from mastodon import Mastodon [as 别名]
# 或者: from mastodon.Mastodon import create_app [as 别名]
def test_app_account_create():
# This leaves behind stuff on the test server, which is unfortunate, but eh.
suffix = str(time.time()).replace(".", "")[-5:]
test_app = test_app = Mastodon.create_app(
"mastodon.py generated test app",
api_base_url="http://localhost:3000/"
)
test_app_api = Mastodon(
test_app[0],
test_app[1],
api_base_url="http://localhost:3000/"
)
test_token = test_app_api.create_account("coolguy" + suffix, "swordfish", "email@localhost" + suffix, agreement=True)
assert test_token
示例3: setup
# 需要导入模块: from mastodon import Mastodon [as 别名]
# 或者: from mastodon.Mastodon import create_app [as 别名]
def setup(self):
print(
"First, we'll need the base URL of the Mastodon instance you want to connect to,"
)
print("e.g. https://mastodon.social or https://botsin.space")
base_url = input("Base URL: ")
result = input("Do you already have a Mastodon app registered (y/n)? ")
if result[0] == "n":
print("OK, we'll create an app first")
app_name = input("App name: ")
client_id, client_secret = MastodonClient.create_app(
app_name, api_base_url=base_url
)
print("App successfully created.")
else:
client_id = input("Client ID: ")
client_secret = input("Client Secret: ")
print("Now we'll need your user credentials")
mastodon = MastodonClient(
client_id=client_id, client_secret=client_secret, api_base_url=base_url
)
email = input("Email address: ")
password = input("Password: ")
mastodon.log_in(email, password)
print("Successfully authenticated to Mastodon")
self.config.add_section("mastodon")
self.config.set("mastodon", "base_url", base_url)
self.config.set("mastodon", "client_id", client_id)
self.config.set("mastodon", "client_secret", client_secret)
self.config.set("mastodon", "access_token", mastodon.access_token)
return True
示例4: register
# 需要导入模块: from mastodon import Mastodon [as 别名]
# 或者: from mastodon.Mastodon import create_app [as 别名]
def register(self):
"""
Register application and saves client secret.
"""
print("Registering app")
Mastodon.create_app(
self.name,
api_base_url=self.url,
scopes=self.scopes,
to_file=self.client_secret
)
示例5: interactive_login
# 需要导入模块: from mastodon import Mastodon [as 别名]
# 或者: from mastodon.Mastodon import create_app [as 别名]
def interactive_login(self):
try:
if (not hasattr(self, "domain")):
domain = input("{0}: Enter the instance domain [mastodon.social]: ".format(self.name))
domain = domain.strip()
if (domain == ""): domain = "mastodon.social"
self.config.domain = domain
# We have to generate these two together, if just one is
# specified in the config file it's no good.
if (not hasattr(self, "client_id") or not hasattr(self, "client_secret")):
client_name = input("{0}: Enter a name for this bot or service [{0}]: ".format(self.name))
client_name = client_name.strip()
if (client_name == ""): client_name = self.name
self.config.client_id, self.config.client_secret = Mastodon.create_app(client_name,
api_base_url="https://"+self.config.domain)
# TODO handle failure
self.config.save()
if (not hasattr(self, "access_token")):
email = input("{0}: Enter the account email: ".format(self.name))
email = email.strip()
password = getpass.getpass("{0}: Enter the account password: ".format(self.name))
try:
mastodon = Mastodon(client_id = self.config.client_id,
client_secret = self.config.client_secret,
api_base_url = "https://"+self.config.domain)
self.config.access_token = mastodon.log_in(email, password)
self.config.save()
except ValueError as e:
self.log("login", "Could not authenticate with {0} as '{1}':"
.format(self.config.domain, email))
self.log("login", str(e))
self.log("debug", "using the password {0}".format(password))
return False
return True
except KeyboardInterrupt:
return False
示例6: get_or_create_host
# 需要导入模块: from mastodon import Mastodon [as 别名]
# 或者: from mastodon.Mastodon import create_app [as 别名]
def get_or_create_host(hostname):
mastodonhost = db.session.query(MastodonHost).filter_by(hostname=hostname).first()
if not mastodonhost:
try:
client_id, client_secret = Mastodon.create_app(
"Moa",
scopes=mastodon_scopes,
api_base_url=f"https://{hostname}",
website="https://moa.party/",
redirect_uris=url_for("mastodon_oauthorized", _external=True)
)
app.logger.info(f"New host created for {hostname}")
mastodonhost = MastodonHost(hostname=hostname,
client_id=client_id,
client_secret=client_secret)
db.session.add(mastodonhost)
db.session.commit()
except MastodonNetworkError as e:
app.logger.error(e)
return None
except KeyError as e:
# Hubzilla doesn't return a client_id
app.logger.error(e)
return None
app.logger.debug(f"Using Mastodon Host: {mastodonhost.hostname}")
return mastodonhost
示例7: _ensure_client_exists_for_instance
# 需要导入模块: from mastodon import Mastodon [as 别名]
# 或者: from mastodon.Mastodon import create_app [as 别名]
def _ensure_client_exists_for_instance(instance):
"Create the client creds file if it does not exist, and return its path."
client_id = "t2m_%s_clientcred.txt" % instance
if not os.path.exists(client_id):
Mastodon.create_app('t2m', to_file=client_id,
api_base_url='https://%s' % instance,
)
return client_id
示例8: setUp
# 需要导入模块: from mastodon import Mastodon [as 别名]
# 或者: from mastodon.Mastodon import create_app [as 别名]
def setUp(self):
if not os.path.isfile(TestMastodonClientBase.APPKEY_FILE):
if not os.path.exists("./tmp/"):
os.makedirs("./tmp/")
(client_id, client_secret) = Mastodon.create_app(
"nabmastodond",
api_base_url=TestMastodonClientBase.API_BASE_URL,
redirect_uris=TestMastodonClientBase.REDIRECT_URI,
)
with open(TestMastodonClientBase.APPKEY_FILE, "w") as appkey_file:
appkey_file.writelines(
[client_id + "\n", client_secret + "\n"]
)
with open(TestMastodonClientBase.APPKEY_FILE, "r") as appkey_file:
self.client_id = appkey_file.readline().strip()
self.client_secret = appkey_file.readline().strip()
self.user1_access_token, self.user1_username = self.login(
TestMastodonClientBase.USER1KEY_FILE,
TestMastodonClientBase.USER1OOB_FILE,
1,
)
self.user2_access_token, self.user2_username = self.login(
TestMastodonClientBase.USER2KEY_FILE,
TestMastodonClientBase.USER2OOB_FILE,
2,
)
示例9: post
# 需要导入模块: from mastodon import Mastodon [as 别名]
# 或者: from mastodon.Mastodon import create_app [as 别名]
def post(self, request, *args, **kwargs):
config = Config.load()
# Determine callback URL from passed location.
location = urlparse(request.POST["location"])
redirect_location = (
location.scheme,
location.netloc,
reverse("nabmastodond.oauthcb"),
"",
"",
"",
)
redirect_uri = urlunparse(redirect_location)
if (
config.instance != request.POST["instance"]
or config.redirect_uri != redirect_uri
):
config.client_id = None
config.client_secret = None
config.redirect_uri = None
config.instance = request.POST["instance"]
# Register application on Mastodon
if config.client_secret is None:
try:
(client_id, client_secret) = Mastodon.create_app(
"nabmastodond",
api_base_url="https://" + config.instance,
redirect_uris=redirect_uri,
)
config.client_id = client_id
config.client_secret = client_secret
config.redirect_uri = redirect_uri
reset_access_token(config)
except MastodonError as e:
return HttpResponse(
"Unknown error",
content=f'{{"status":"error",'
f'"code":"MastodonError",'
f'"message":"{e}"}}',
mimetype="application/json",
status=500,
)
# Start OAuth process
mastodon_client = Mastodon(
client_id=config.client_id,
client_secret=config.client_secret,
api_base_url="https://" + config.instance,
)
request_url = mastodon_client.auth_request_url(
redirect_uris=redirect_uri
)
return JsonResponse({"status": "ok", "request_url": request_url})