新浪微博python下api的OAuth登陆

本文介绍了一种通过OAuth认证获取新浪微博API访问权限的方法,并演示了如何利用这些API抓取公共时间线上的微博信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值