python接入微博第三方API之2接入用户登录和微博发布

# coding=utf-8

import requests
import json
import MySQLdb
from datetime import datetime
from flask import Flask, redirect, request
app = Flask(__name__) # 全局变量token
token = {} def get_ticket():
# App Key:13866668888 app key就是url中的 YOUR_CLIENT_ID
# YOUR_REGISTERED_REDIRECT_URI就是 http://test.chinasoft.com
client_id = 13866668888
redirect_url = "http://test.chinasoft.com/a"
url = 'https://api.weibo.com/oauth2/authorize?client_id={app_key}&response_type=code&redirect_uri={redirect_url}'.format(app_key = client_id, redirect_url = redirect_url)
return url def get_token(code):
# App Secret:6dc11f2039
redirect_url = "http://test.chinasoft.com/a"
client_secret = "6dc11f2039"
client_id = 13866668888
url = "https://api.weibo.com/oauth2/access_token?client_id={client_id}&client_secret={client_secret}&grant_type=authorization_code&redirect_uri={redirect_url}&code={code}".format(client_id = client_id,client_secret = client_secret,redirect_url = redirect_url,code = code)
resp = requests.post(url)
global token
token = resp.json()
return token def get_info(access_token, uid):
url = "https://api.weibo.com/2/users/show.json"
resp = requests.get(url, params = {
'access_token':access_token,
'uid':uid
})
return resp.json() def get_conn():
""" 获取mysql的连接"""
try:
conn = MySQLdb.connect(
host="localhost",
port=3306,
user="root",
password="root",
db="user_grade",
)
except MySQLdb.Error as e:
print("Error %d:%s" % (e.args[0], e.args[1])) return conn def weibo_share(access_token):
url = "https://api.weibo.com/2/statuses/share.json"
resp = requests.post(url, {
'access_token':access_token,
'status':'现在是北京时间:{0} http://test.chinasoft.com'.format(datetime.now())
})
return resp.json() @app.route('/a')
def index():
code = request.args.get('code', None)
# 根据code获取token
token = get_token(code)
# 获取用户信息
user_info = get_info(token['access_token'], token['uid']) third_id = user_info['id']
nickname = user_info['screen_name']
headimg = user_info['profile_image_url'] # 获取数据库的连接,将用户信息存入到数据库中
# create table user(third_id int,nickname varchar(255),headimg varchar(1000));
conn = get_conn()
cur = conn.cursor()
# 注意 sql中的 values中的字段一定要加 单引号,否则会报错
sql = "insert into user(third_id,nickname,headimg) values('{third_id}','{nickname}','{headimg}')".format(third_id = third_id,nickname = nickname,headimg = headimg)
cur.execute(sql)
conn.autocommit(True) return json.dumps(user_info) @app.route('/weibo')
def weibo():
ticket = get_ticket()
return redirect(ticket) @app.route('/share')
def share():
ticket = get_ticket()
rest = weibo_share(token['access_token'])
return json.dumps(rest) if __name__ == '__main__':
app.run(port=80, debug=True)

登录api

https://open.weibo.com/wiki/Connect/login

https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI

访问:http://127.0.0.1:5000/weibo

获取token

最新文章

  1. Play Framework 完整实现一个APP(十)
  2. Sqlite创建增删改查
  3. http权威指南
  4. 自增长主键Id的另类设计
  5. monkeyrunner之eclipse中运行monkeyrunner脚本之环境搭建(四)
  6. C# 采用线程重绘图形要点记录
  7. Dreamweaver CS6破解教程[序列号+破解补丁]
  8. C++获取文件大小常用技巧
  9. Visual Studio 2017正式版安装
  10. Redis之String
  11. call是什么?一次说个明白
  12. IdentityServer4 知多少
  13. MySql 双主多从配置指导
  14. PPS--在download DN出现的问题注意:
  15. C++面试基础之回调
  16. [转帖]国产闪存颗粒终于熬出头 紫光存储S100固态硬盘评测
  17. pycharm 使用教程
  18. BZOJ3159决战——树链剖分+非旋转treap(平衡树动态维护dfs序)
  19. hdu 1213 求连通分量(并查集模板题)
  20. c# SerialPort会出现“已关闭 Safe handle”的错误

热门文章

  1. linux系统编程之信号(六)
  2. javax.servlet.ServletException: Circular view path [index]: would dispatch back to the current handler URL [/pay/index] again. Check your ViewResolver setup!
  3. maven中profile的使用
  4. PHP高手干货分享:不能不看的50个细节!
  5. java常用日期类型转换
  6. 完成一个springboot项目的完整总结-------二
  7. php 文件包含函数
  8. 如何使用ArcGIS Pro发布自定义打印服务
  9. C#/Java 常用轮子 (子文章)(持续更新)
  10. Docker镜像使用