调用weibo的开放平台api进行OAuth认证Python实现

本文介绍了如何使用Python调用微博开放平台API进行OAuth认证。首先,需要在微博开放平台注册并获取app key和app secret。然后,通过requests库发送请求获取code,并利用code换取access_token。详细步骤参照相关教程,最终成功获取到用户的access_token及其他相关信息。
摘要由CSDN通过智能技术生成

第一步,微博开放平台提供了微博数据的api接口,不仅可以直接通过api调用微博服务发布微博查询微博,更重要的是,可以在自己的网站上获得新浪微博api的授权,调用微博的某些内容,就好像我们再网站中看到好文章要分享到微博或者其他社交网站中一样,首先要进行注册开发者模式获取app key 和 app secret。

参考https://www.cnblogs.com/opensesame/p/6103553.html,写的很详细。

 

第二步,Python环境安装必要的包,requests;

首先通过网页获取到code,

https://api.weibo.com/oauth2/authorize?client_id=
3075546956&redirect_uri=https://blog.csdn.net/zzmxz&response_type=code

import requests

payload = {'client_id':'3075546956','client_secret':'d83aa9a7d607336fdfe01c8304595baa','grant_type':'authorization_code','code':'37b31a8e28f11d9496bfbd1458d1d250','redirect_uri':'https://blog.csdn.net/zzmxz'}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
weibo node sdk 是新浪微博 Node.js SDK。 特点 api可配置化 接口采用promise 最少依赖,专注新浪微博OAuth2.0认证 使用方法 安装 npm install iweibo 配置 引入iweibo var iweibo = require('iweibo'); var Weibo = iweibo.Weibo; 配置app信息 iweibo.set(name, options); //设置单条 iweibo.set(optionsObject);  //设置多条 iweibo.set({  appkey: 'xxx',  appsecret: 'xxxxxxxxxx'  }) 支持的配置: var CONFIG = {  appkey: '',  appsecret: '',  oauth_host: 'https://api.weibo.com/oauth2/authorize',  access_url: 'https://api.weibo.com/oauth2/access_token',  api_url: 'https://api.weibo.com/2/'  } 配置api接口 iweibo.setAPI(apiname, options); //设置单条api iweibo.setAPI(optionsObject);  //设置多条api iweibo.setAPI('statuses/update', {  method: 'post',  params: {  status: 'hello, world',  visible: 0  } }); 配置下微博接口(由于太多,并且不时更新,所以我就没全配置),配置下自己使用的接口,方法参考下件,基本如下: '接口名称': {  method: 'get', //请求方法,post或者get(get可省略),参考api文档  params: { //默认参数,不用填写appkey和access_token,程序会自动补上  } } 可以讲接口统一写到一个json或者js文件中,然后使用 require 引入,直接给 setAPI 传入 使用 参考 examples/app.js 文件(需要先在本目录执行 npm install 安装依赖模块) 修改host,添加下面内容: 127.0.0.1 testapp.cn 进入 open.weibo.com 设置应用回调地址到 http://testapp.cn/callbak 获取登录链接 weibo.getAuthorizeURL(backURL); 获取access_token weibo.getAccessToken('code', {  code: code,  redirect_uri: backURL }).done(function(err, data) {  var realpath = templateDir   'callback.html';  html = fs.readFileSync(realpath);  data = JSON.parse(data);  data.refresh_token = data.refresh_token || '';  req.session.refresh_token = data.refresh_token;  req.session.access_token = data.access_token;  req.session.uid = data.uid;  html = bdTemplate(html, data);  res.end(html); }).fail(function(err, data) {  var html;  if (err) {  html = fs.readFileSync(templateDir   'error.html');  }      res.end(html); }); 使用api接口 //所有API都支持promise接口  weibo.api('users/show', urlObj).done(function(err, result) {  console.log(result);  res.end(JSON.stringify(result));  }); 测试方法 进入examples 修改config.json,回调地址需要在open.weibo.com配置好,然后修改自己的host,将回调地址指到127.0.0.1 执行 npm install 访问自己在config.json配置的网站 标签:weibo

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值