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

python

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

以上是 python接入微博第三方API之2接入用户登录和微博发布 的全部内容, 来源链接: utcz.com/z/387410.html

回到顶部