android 新浪微博认证
对于新浪微博这块一直很纠结,auth认证让人吐血。之前项目用到的都是copy别人的,最近想详细了解下,就简单实现一二,以便以后自己更好的运用.
准别好以下常量
public String consumerKey = "2803926882";// key
public String consumerSecret = "d41e6f2d605bd16c785879032ec431f5";// Secret
//自定义回调URL(这个url要在配置文件中配置,)
public static final String CALLBACK_URL = "jjhappyforever://WeiboListActivity";
// requestTokenURL
private final String requestTokenEndpointUrl = "http://api.t.sina.com.cn/oauth/request_token";
// accessTokenURL
private final String accessTokenEndpointUrl = "http://api.t.sina.com.cn/oauth/access_token";
// 授权
private final String authorizationWebsiteUrl = "http://api.t.sina.com.cn/oauth/authorize";
下面两个方法是Oauth.java中的两个方法.
// 获取auth地址
public String RetrieveAuthUrl() throws OAuthMessageSignerException,
OAuthNotAuthorizedException, OAuthExpectationFailedException,
OAuthCommunicationException {
authConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
authProvider = new CommonsHttpOAuthProvider(requestTokenEndpointUrl,
accessTokenEndpointUrl, authorizationWebsiteUrl);
return authProvider.retrieveRequestToken(authConsumer, CALLBACK_URL);
}
//这个方法是获取用户的 userId,token,tokenSecret等信息.
public User getAccessToken(String authUrl)
throws OAuthMessageSignerException, OAuthNotAuthorizedException,
OAuthExpectationFailedException, OAuthCommunicationException {
Log.i(TAG, authUrl);
authProvider.setOAuth10a(true);
authProvider.retrieveAccessToken(authConsumer, authUrl);
User user = new User();
user.userId = authProvider.getResponseParameters().getFirst("user_id");
user.token = authConsumer.getToken();
user.tokenSecret = authConsumer.getTokenSecret();
return user;
}
在Activity中调用.
// 获取authUrl地址
String authUrl = oauth.RetrieveAuthUrl();
// 启动访问新浪认证页面
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
这样就会执行web 认证
在本activity中实现onNewIntent方法,用来执行认证回调后的动作,
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
try {
User user = null;
user = oauth.getAccessToken(verifier);
Log.i(Oauth.TAG, user.toString());
} catch (OAuthMessageSignerException ex) {
ex.printStackTrace();
} catch (OAuthNotAuthorizedException ex) {
ex.printStackTrace();
} catch (OAuthExpectationFailedException ex) {
ex.printStackTrace();
} catch (OAuthCommunicationException ex) {
ex.printStackTrace();
}
}
在这里要说一下配置文件中的配置:
<activity
android:name=".TestActivity"
android:launchMode="singleInstance" ><!-——这个lanchMode模式一定要设置,不设置的话会一直执行oncreate.不会执行onNewIntent 我在这里纠结好久.——>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="Oauth"
android:scheme="jjhappyforevert" />
</intent-filter>
</activity>
到这里就实现了Oauth认证流程.下一篇,要简单介绍下根据开放平台API如果调用用户的微博信息.
android 新浪微博认证相关推荐
- Android新浪微博登陆和获取个人信息
自己总结的一些android新浪登陆和根据用户ID获取用户信息,以下代码只是写了重要的部分,详细代码可到新浪官方下载一个Demo,里面都有详细流程和各种API. Constant.java 常量类 ...
- android 新浪微博签名工具,Android 集成新浪微博分享及授权 (上)
Android 新浪微博分享及授权 在开发中遇到了各种坑....所以分享一下 希望能给大家贡献点经验 本文使用的开发的环境是 eclipse 本文叙述的新浪微博分享及其授权的内容分为两块 : 一 新 ...
- CTS(8)---Android Google认证 -CTS认证问题小结
Android Google认证 -CTS认证问题小结 0.0 前言 这段时间接手了CTS认证相关的工作,在这里整理一下相关的问题.(同时感谢同事对此总结提供的帮助及补充) 1.0 Google Wa ...
- 2020年Android GMS 认证 boot logo 最新要求
Android GMS 认证 boot logo 要求 下面是GMS认证对开机logo的8点要求,以下是特别容易忽略的几点: 第6点("Powered by Android"标志模 ...
- 2019年Android GMS 认证 boot logo 最新要求
Android GMS 认证 boot logo 要求 下面是GMS认证对开机logo的8点要求,以下是特别容易忽略的几点: 第6点("Powered by Android"标志模 ...
- android新浪微博改进版
过年之前做的新浪微博的客户端其实有很多问题,现在做了一些修改,比如对listview做了优化,图片是用了异步加载,这样不但可以保证加载数据的速度,而且还可以减少访问网络的流量,毕竟流量还是很贵的嘛.还 ...
- python新浪微博认证及发微
新浪微博认证需要安装新浪微博的API(Python),安装好后就可以使用微博提供的的各种接口.这里只简单介绍认证和发微.· 首先可以通过微博开放平台注册应用申请APP_KEY和APP_SECRET,它 ...
- 最新免费新浪微博认证方法,不需要微博认证的硬性条件
关于新浪微博认证,点开这篇文章的估计也是玩微博或者想认证微博的,微博认证需要的那几个硬性指标,相信大家都知道,唯独的就是当你满足这些条件的时候,就最后一个资质证明把很多没有资质证明的小草根挡在门外,我 ...
- IOS开发——新浪微博认证失败处理
IOS开发--新浪微博认证失败处理 认证失败错误提示: 请求返回错误( error.errorDescription):Insufficient app permissions! 1,检查 分享集成部 ...
最新文章
- Ubuntu16.04安装NVIDIA驱动+CUDA9.0+cuDNN7.0
- 对象存储与块存储、文件存储等对比
- 【转】Ubuntu VI基本用法
- 在项目中增加task定时任务
- Java中Scanner的理解大总结
- servlet跳转页面的几种方法
- java 工程新建ivy文件_Hadoop学习之路(八)在eclispe上搭建Hadoop开发环境
- windows 2008 r2 系统默认80端口被系统占用的处理
- 关于IIS 7.5 限制连接数与流量限制模块
- 物料编码是计算机系统对物料,物料编码是什么?
- 计算机绘图模型的参数方程,关于曲线绘图与运动控制问题的研究.doc
- Python刷题-7
- java中将一个字符数组赋值给另一个,两者同时变化
- 局域网办公系统服务器备份,协同办公系统的数据备份经验分享
- 浅析ServiceMesh Istio
- 软件开发团队中各个成员的英文简称
- CSS设置字体种类,如宋体,楷体,黑体,仿宋等等。。。。
- USB 协议整理 七:STM32F103之USB概述
- java定时任务:oracle导出excel后,发送excel作为附件的邮件
- 计算机基础知识考试技巧,计算机二级考试Office应试技巧