博客园 OpenAPI 调用记录(Python)
目录
申请 OpenAPI
通过后,你将得到 ClientId 和 ClientSecret
OpenAPI 调用
接口文档: https://api.cnblogs.com/Help
请求示例:
1、Client_Credentials授权
import requests
import json
from urllib import parse
ClientId = 'xxxx'
ClientSecret = 'xxxx'
url = 'https://api.cnblogs.com/token'
headers = {"Content-Type": "application/x-www-form-urlencoded;charset=utf-8" }
param = {
'client_id' : ClientId,
'client_secret' : ClientSecret,
'grant_type' : 'client_credentials',
}
data = parse.urlencode(param)
ret = requests.post(url=url, headers=headers, data=data)
print(ret, ret.text)
你将得到:
{
"access_token":"xxxxx",
"expires_in":10800,
"token_type":"Bearer",
"scope":"CnBlogsApi"
}
2.1 获取授权码
需要使用 web 访问url,拼接上你的参数,得到这样的 url:
https://oauth.cnblogs.com/connect/authorize?client_id=xxx&scope=openid profile CnBlogsApi offline_access&response_type=code id_token&redirect_uri=https://oauth.cnblogs.com/auth/callback&state=xxx&nonce=xxx
用户授权后,跳转的页面显示
默认授权回调页
授权码(code): xxx
这个code 将在 2.2 接口中使用
2.2 Authorization_Code授权
def rqst_connect_token():
headers = {"Content-Type": "application/x-www-form-urlencoded;charset=utf-8" }
print('\n\n-- rqst_connect_token \n')
param = {
'client_id' : ClientId,
'client_secret' : ClientSecret,
'grant_type' : 'authorization_code',
'code' : 'xxxxx', # 上面得到的code
'redirect_uri' : 'https://oauth.cnblogs.com/auth/callback',
}
url = 'https://oauth.cnblogs.com/connect/token'
data = parse.urlencode(param)
ret = requests.post(url=url, headers=headers, data=data)
print(ret, ret.text)
得到
{
"id_token":"xxx",
"access_token":"xxx",
"expires_in":10800,
"token_type":"Bearer",
"refresh_token":"xxx",
"scope":"openid profile CnBlogsApi offline_access"
}
获取当前登录用户信息
我将上面得到的 access_token 保存到了 access_token.txt
def rqst_users():
access_token = access_token = open('access_token.txt').read().strip()
print('-- rqst_users : ', access_token[:10])
url = 'https://api.cnblogs.com/api/users'
headers = {'Authorization' : 'Bearer '+ access_token}
ret = requests.get(url=url, headers=headers)
print(ret, ret.text)
得到结果
{
"UserId":"xxx",
"SpaceUserID":698385,
"AccountId":698385,
"BlogId":549327,
"DisplayName":"小蓝S",
"Face":"https://pic.cnblogs.com/face/xx/xx42.png",
"Avatar":"https://pic.cnblogs.com/avatar/698385/xxx.png",
"Seniority":"9年",
"BlogApp":"fldev",
"FollowingCount":45,
"FollowerCount":13,
"IsVip":false,
"Joined":"2014-11"
}
获取个人博客信息
headers 同上,是 Bear + access_token
def rqst_blog():
blogApp = 'fldev'
url = f'https://api.cnblogs.com/api/blogs/{blogApp}'
ret = requests.get(url=url, headers=headers)
print(ret, ret.text)
{
"Result":{
"BlogId":549327,
"Title":"如行",
"Subtitle":"",
"PostCount":199,
"PageSize":40
},
"Id":149,
"Exception":null,
"Status":5,
"IsCanceled":false,
"IsCompleted":true,
"IsCompletedSuccessfully":true,
"CreationOptions":0,
"AsyncState":null,
"IsFaulted":false
}
获取最新一条闪存内容
headers 同上
def rqst_statuses_recent():
print('\n\n-- rqst_statuses_recent \n')
url = 'https://api.cnblogs.com/api/statuses/recent'
ret = requests.get(url=url, headers=headers)
# ret = requests.get(url=url) # 401
print(ret, ret.text)
得到一串 html 字符串
<a href="//ing.cnblogs.com/u/698385/status/2757169/" title="闪于12-26 16:33,6个回应">博客园 开放接口,谁还有链接和文档吗?</a>
2023-12-27(三)