微博API 学习记录

error 10006

code使用如下代码一直报10006错误,我起初以为是weibo封锁了接口。查百度发现有人说是appkey过期,过期是不可能的,我的key是当天申请的。

u8 = 'https://api.weibo.com/2/statuses/public_timeline.json'
d8 ={"access_token":"xxxx",  
     }
r8 = s.get(u8,data=d8)
print r8.content

{"error":"source paramter(appkey) is missing","error_code":10006,"request":"/2/statuses/public_timeline.json"}

pic1

使用官方API调测工具发现同样的“参数”是可以用的。因此把data换为params试一下,果然成功了!r8 = s.get(u8,params=d8)

给某人最新微博评论

  • 需要检测ta的微博数(默认不删博),如果大于上次检测的值,就_comment()
  • 评论接口需要单条微博的ID;
  • 综上两点,使用domain_show接口最合适

几点注意

  • 未审核通过的应用,token有效期是一天;
  • 官方说/share.json产生的内容仅测试号自己可见,实际上其他人也能看到;

weibo.py

# -*- coding: utf-8 -*-
"""
Created on Fri May 25 09:20:25 2018
微博评论
@author: hndx
"""
import requests
import random
from kvdb import kv,bkt

s = requests.session()

u3 = 'https://api.weibo.com/2/comments/create.json'

def _comment(wbid):
    fo = open("static/tt.py", "r")
    lis = fo.readlines() #土味情话
    fo.close()
    kv.set('jlwb_comments',kv.get('jlwb_comments')+1)
    d3 = {"access_token":"xx",  
      "comment":lis[random.randint(0,68)], 
      'id':wbid  }
    s.post(u3,data=d3)
    return 'done comment'
    
def _check(): #检查是否有新微博
    u7 = 'https://api.weibo.com/2/users/domain_show.json'
    d7 ={"access_token":"xx",  
         'domain':'xx'   }#微博个性域名
    r7 = s.get(u7,params=d7)
    j7 = r7.json()
    if kv.get('jlweibo') < j7["statuses_count"]: #如果有新微博
        kv.set('jlweibo',j7["statuses_count"])
        _comment(j7["status"]['id'])
    else:
        return 'no new weibo'

def _jay(): #instagram搬运
    pic_url = 'xx'
    ask_url = 'xx'
    r = s.get(ask_url)   
    if not int(r.json()['instagram'][0]): #是否有新内容
        return 'no new insta-Jay'
    else:
        kv.set('jay_insta',kv.get('jay_insta')+1)
        jay_text= r.json()['instagram'][2]
        wb_url = 'https://api.weibo.com/2/statuses/share.json'
        d2 = {"access_token":"xx",  
        "status":"[Jay]"+jay_text+" xx"  }
        jay_pic = s.get(pic_url)
        open('/s/jay/jay.jpg', 'wb').write(jay_pic.content)
        files={"pic":open("/s/jay/jay.jpg","rb") }  
        rs = s.post(wb_url,data=d2,files=files)
        
        return rs.content
原文地址:https://www.cnblogs.com/aubucuo/p/weiboapi.html