I would like to get a post of weibo from python, but I get a TypeError.

Asked 2 years ago, Updated 2 years ago, 17 views

I would like to use the program below to get a post on weibo from python.

 from weibo import Client

API_KEY='294703483' #appkey
API_SECRET='a2ef9de0a580edb7a26daf2804d27624'#appsecret
REDIRECT_URI='https://api.weibo.com/oauth2/default.html' #callbackurl

c=Client(API_KEY, API_SECRET, REDIRECT_URI, token="2.00skPOWG0zpXw_e8253b9b7c9GKIdE", username="USER", password="PASSWORD")

class Client (object):
   def__init__(self, api_key, api_secret, redirect_uri, token=None,
   username=None, password=None):
 # const define
        self.site='https://api.weibo.com/'
        self.authorization_url = self.site + 'oauth2/authorize'
        self.token_url = self.site + 'oauth2/access_token'
        self.api_url=self.site+'2/'

Run Results

[root@localhost Desktop] #python cinco.py
Traceback (most recent call last):
  File "cinco.py", line 7, in <module>
    c=Client(API_KEY, API_SECRET, REDIRECT_URI,
token="2.00skPOWG0zpXw_e8253b9b7c9GKIdE", username="USER", password="PASSWORD")
  File"/usr/lib/python 2.7/site-packages/weibo.py", line 45, in __init__
    self.set_token(token)
  File"/usr/lib/python 2.7/site-packages/weibo.py", line84, in set_token
    self.uid=token ['uid']
TypeError: string indications must be integrers, not str

I got information about weibo and python from the link below.
http://weibo.lxyu.net/ (weibo documentation)

https://www.cs.cmu.edu/~lingwang/weiboguide/ (about weibo access_token)

What you got from the link above

{"access_token":"2.00skPOWG0zpXw_e8253b9b7c9GKIdE", "remind_in": "157679999", "expires_in":157679999, "uid": "5973036902"}
code=682ac33cecf21d6efee0882e2a91eba4

https://github.com/michaelliao/sinaweibopy/wiki/OAuth2-HOWTO (about getting weibo posts from python)

http://open.weibo.com/apps/294703483/info/basic (contains information such as API_KEY)

I would appreciate it if you could let me know if anyone understands.
Thank you for your cooperation.

python

2022-09-30 16:35

1 Answers

The point of the error message is

File"/usr/lib/python 2.7/site-packages/weibo.py", line 45, in __init__
    self.set_token(token)
  File"/usr/lib/python 2.7/site-packages/weibo.py", line84, in set_token
    self.uid=token ['uid']
TypeError: string indications must be integrers, not str

However, the subscripts to the string must be numeric and cannot be accessed with a string key such as token['uid'].In short, the token specified for Client(...) is a dictionary type that corresponds to {"access_token":"...", "remind_in":"...", "expires_in":..., "uid":"...}, not the access_token string

It is correct to use the code from the existing client, such as the following code in the document:

token=c.token
c2 = Client (API_KEY, API_SECRET, REDIRECT_URI, token)

It is true that only access_token is needed to invoke the Weibo API, but it seems that your lxyu/weibo does not allow access_token to be configured only.
It looks like I only use access_token, so I think it will work if I give you a dictionary with other elements.

Of course, it depends on the library, so michaeliao/sinaweibopy mentioned in the same question can only be configured with access_token, but it's a different library from what you're using now.


2022-09-30 16:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.