import webbrowser
import weibopy
from weibopy.auth import OAuthHandler
from weibopy.api import API
class myapp:
def __init__(self,appkey='',appsecret=''):
self.consumer_key=appkey
self.consumer_secret=appsecret
self.u=user()
def getauth(self):
if len(self.consumer_key)==0:
print("Please set consumer_key")
return
if len(self.consumer_secret)==0:
print("Please set consumer_secret")
return
return OAuthHandler(self.consumer_key,self.consumer_secret)
def getapi(self):
'''
返回新浪微博api
'''
auth=self.getauth()
auth_url=auth.get_authorization_url()
print('Please authorize:'+auth_url)
webbrowser.open(auth_url)
verifier=input('请输入PIN码:').strip()
auth.get_access_token(verifier)
api=API(auth)
'''
网上流传的是 auth=API(auth.get_access_token(verifier))是错误的,
因为API()的参数是OAuthHandle对象,这个卡了我好久,害苦我了。
'''
return api
if __name__ =='__main__':
app=myapp()
api=app.getapi()
for weibo in api.public_timeline():
print(weibo)
新浪微博python下api的OAuth登陆
最新推荐文章于 2025-08-09 10:17:13 发布