我使用了下面的mastodon来获取用户的所有信息,我想从它访问用户名,我在下面解释我的输入和输出。我是使用乳齿状帐户‘用户名’访问用户名,但它会给我错误。
from mastodon import Mastodon
Mastodon.create_app(
'pytooterapp',
api_base_url = 'https://mastodon.social',
to_file = 'pytooter_clientcred.secret'
)
mastodon = Mastodon(
client_id = 'pytooter_clientcred.secret',
api_base_url = 'https://mastodon.social'
)
mastodon.log_in(
'xyz@gmail.com',
'xyz',
to_file = 'pytooter_usercred.secret'
)
mastodon = Mastodon(
client_id = 'pytooter_clientcred.secret',
access_token = 'pytooter_usercred.secret',
api_base_url = 'https://mastodon.social'
)当我在python中触发上面的代码时,我将得到以下输出
输出:
{u'account': {u'acct': u'xyz',
u'avatar': u'https://mastodon.social/avatars/original/missing.png',
u'avatar_static': u'https://mastodon.social/avatars/original/missing.png',
u'bot': False,
u'created_at': datetime.datetime(2018, 1, 31, 17, 14, 53, 985000, tzinfo=tzutc()),
u'display_name': u'',
u'emojis': [],
u'fields': [],
u'followers_count': 1,
u'following_count': 3,
u'header': u'https://mastodon.social/headers/original/missing.png',
u'header_static': u'https://mastodon.social/headers/original/missing.png',
u'id': 2871,
u'locked': False,
u'note': u'<p></p>',
u'statuses_count': 6,
u'url': u'https://mastodon.social/@xyz',
u'username': u'xyz'},
u'application': {u'name': u'pytooterapp', u'website': None},
u'content': u'<p>Tooting from python using <a href="https://mastodon.social/tags/mastodonpy" class="mention hashtag" rel="tag">#<span>mastodonpy</span></a> !</p>',
u'created_at': datetime.datetime(2018, 5, 20, 12, 32, 56, 757000, tzinfo=tzutc()),
u'emojis': [],
u'favourited': False,
u'favourites_count': 0,
u'id': 100061647780173,
u'in_reply_to_account_id': None,
u'in_reply_to_id': None,
u'language': u'en',
u'media_attachments': [],
u'mentions': [],
u'muted': False,
u'pinned': False,
u'reblog': None,
u'reblogged': False,
u'reblogs_count': 0,
u'sensitive': False,
u'spoiler_text': u'',
u'tags': [{u'name': u'mastodonpy',
u'url': u'https://mastodon.social/tags/mastodonpy'}],
u'uri': u'https://mastodon.social/users/xyz/statuses/1000616477685173',
u'url': u'https://mastodon.social/@xyz/100061640685173',
u'visibility': u'public'}
Now I want to access 'username' from above dictionary but I am not able to access. When I am trying to access username using "mastodon.account['username']" it gives me following error
"TypeError: 'instancemethod' object has no attribute '__getitem__'" 发布于 2018-08-20 16:54:07
您需要使用account_verify_credentials()
例如
from mastodon import Mastodon
# Set up Mastodon
mastodon = Mastodon(
access_token = 'usercred.secret',
api_base_url = 'https://botsin.space/'
)
mastodon.account_verify_credentials()会给你
{'id': 58380, 'username': 'openbenches', 'acct': 'openbenches', 'display_name': 'OpenBenches.org', ...因此,要获取用户名,请调用
mastodon.account_verify_credentials()["username"]https://stackoverflow.com/questions/50435109
复制相似问题