0

I used following mastodon python api to get all information of user and I want to access username from it.I explain my input and output below. I am using mastodon.account['username'] to access username but it gives me error.

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'
)

I am getting following output when I fire above code in python shell

Output:

{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__'" 
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
  • I just want to access username from that dictionary but I am not able to aceess. The link you given uses the same api which I used is there any other wayaround? – amit ncert May 20 '18 at 13:10

1 Answers1

2

You need to use account_verify_credentials()

For example

from mastodon import Mastodon

#   Set up Mastodon
mastodon = Mastodon(
    access_token = 'usercred.secret',
    api_base_url = 'https://botsin.space/'
)

mastodon.account_verify_credentials()

Will give you

{'id': 58380, 'username': 'openbenches', 'acct': 'openbenches', 'display_name': 'OpenBenches.org', ...

So, to get the username, call

mastodon.account_verify_credentials()["username"]
Terence Eden
  • 14,034
  • 3
  • 48
  • 89