通过输入方式在Android上进行微博OAuth登录
在微博认证方式里,基本的OAuth认证是必须要调整到跳转到第三方页面上进行授权的,例如下面的例子:
public class Weibo extends WeiboSupport implements java.io.Serializable {
public static String CONSUMER_KEY = "41199486xx";
public static String CONSUMER_SECRET = "d589738xx1e0026xxce22xx84cf87dxx";




<EditText android:layout_height="wrap_content"
android:text=""
android:layout_width="260dip"
android:id="@+id/account" />
<EditText android:layout_height="wrap_content"
android:text=""
android:layout_width="260dip"
android:id="@+id/password" />
<Button android:layout_height="wrap_content"
android:text="auth"
android:layout_width="wrap_content"
android:id="@+id/authButton" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
androidandroid:layout_width="fill_parent"android:layout_height="fill_parent"
androidandroid:scrollbars="vertical"android:fadingEdge="vertical">
<TextView android:layout_width="fill_parent"
androidandroid:layout_height="wrap_content"android:id="@+id/authResult"
android:paddingTop="5dip"/>
</ScrollView>
public class OAuthVerifier extends OAuthToken {
private static final long serialVersionUID = -8344528374458826291L;
private String verifier;
OAuthVerifier(Response res) throws WeiboException {
this(res.asString());
}
OAuthVerifier(String str) {
super(str);
String[] results = str.split(":");
if(results.length >= 3) {
verifier =results[2].substring(1, 7);
} else {
verifier = "";
}
}
public OAuthVerifier(String token,String tokenSecret) {
super(token, tokenSecret);
}
/**
*
* @return verifier
* @since Weibo4android
*/
public String getVerifier() {
return verifier;
}
}
然后修改androidexamples包下的AndroidExample类。初始化界面元素,设置authButton点击时的事件处理。
/* 初始化界面元素 */
ButtonbeginOuathBtn = (Button)findViewById(R.id.authButton);
final EditText accountInput= (EditText) findViewById(R.id.account);
final EditTextpasswordInput = (EditText) findViewById(R.id.password); /* oauth认证按钮的点击事件 */
beginOuathBtn.setOnClickListener(newButton.OnClickListener()
{
public void onClick( View v )
{
Weiboweibo = OAuthConstant.getInstance().getWeibo(); // init weibo object
RequestTokenrequestToken;
try {
requestToken = weibo.getOAuthRequestToken(); OAuthConstant.getInstance().setRequestToken(requestToken); String username =accountInput.getText().toString();
String password =passwordInput.getText().toString(); OAuthVerifier oauthVerifier = weibo.getOauthVerifier(username,password); // get verifier String verifier = oauthVerifier.getVerifier();
AccessToken accessToken =requestToken.getAccessToken(verifier); // get access token OAuthConstant.getInstance().setAccessToken(accessToken);
TextView textView = (TextView) findViewById(R.id.authResult);
textView.setText("得到AccessToken的key和Secret,可以使用这两个参数进行授权登录了.\n Access token:\n"
+ accessToken.getToken() + "\n Access token secret:\n" + accessToken.getTokenSecret()); } catch (WeiboException e) {
e.printStackTrace();
}
}
});
在src下weibo4android包的Weibo.java里添加getOAuthVerifier方法。
public OAuthVerifiergetOAuthVerifier(Stringusername, Stringpassword) throws WeiboException {
return http.getOAuthVerifier(username,password);
}
在src下weibo4android.http包的HttpClient.java文件里添加如下代码:
private OAuthVerifier oauthVerifier = null;
public OAuthVerifiergetOAuthVerifier(String username, String password) throws WeiboException {
this.oauthVerifier = newOAuthVerifier(httpRequest(authorizationURL,
new PostParameter[]{new PostParameter("userId", username), new PostParameter("passwd", password), new PostParameter("oauth_callback", "json")}
,true)); // callback = json isimportant!
return (OAuthVerifier) this.oauthVerifier;
}


通过输入方式在Android上进行微博OAuth登录的更多相关文章
- 在Android上实现使用Facebook登录(基于Facebook SDK 3.5)
准备工作: 1. Facebook帐号,国内开发者需要一个vpn帐号(网页可以浏览,手机可以访问) 2. 使用Facebook的SDK做应用需要一个Key Hashes值. 2 ...
- Android上的SQLLite性能分析
也许有人还不知道,Android 是有一些内建的 类库支持 SQL Lite 数据库的操作.他提供了一个很好的方式在 Android 上组织少量的数据.不管怎样,在使用这些类库的时候有一些陷阱是需要注 ...
- 关于Unity程序在IOS和Android上显示内嵌网页的方式
近期因为有须要在Unity程序执行在ios或android手机上显示内嵌网页.所以遍从网上搜集了一下相关的资料.整理例如以下: UnityWebCore 从搜索中先看到了这个.下载下来了以后发现这个的 ...
- 一个Android上的以滑动揭示的方式显示并切换图片的View
SlideView是一个Android上的以滑动揭示的方式显示并切换图片的View,以视觉对比的方式把一套相似的图片展示出来. 示例 翻页图片揭示效果: 特性 设置一组(List<ImageIn ...
- [Deprecated!] Android开发案例 - 微博正文
Deprecated! 更好的实现方式: 使用 android.support.design.widget.CoordinatorLayout. 本文详细介绍如何实现如下图中的微博正文页面效果, 其中 ...
- 系列篇|编译可在Android上运行的依赖库(一):glib库
前言 这是系列文章,它们由<编译可在Android上运行的glib库>及其他4篇文章组成,这4篇文章在“编译依赖库”一节中列出.由于glib库依赖于其他第三方库,所以需要先将依赖的第三方库 ...
- ZT 理解 Android 上的安全性
理解 Android 上的安全性 http://www.ibm.com/developerworks/cn/xml/x-androidsecurity/ 利用沙箱.应用程序签名和权限增强应用程序安全性 ...
- 如何在Android上编写高效的Java代码
转自:http://www.ituring.com.cn/article/177180 作者/ Erik Hellman Factor10咨询公司资深移动开发顾问,曾任索尼公司Android团队首席架 ...
- 如何调试 Android 上 HTTP(S) 流量
http://greenrobot.me/devpost/how-to-debug-http-and-https-traffic-on-android/ 如何调试 Android 上 HTTP(S) ...
随机推荐
- Sunday算法(字符串查找、匹配)
字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).两个算法在最坏情况下均具有线性的查找时间.但是在实用上,KMP算法并不比最简单的 ...
- C#入门基础
那么由 = 连接起来的这个式子 就叫赋值表达式 解决异常的 办法 for循环 常量 常量在程序中 怎么定义: const 类型 常量名= 值; 在程序中可以改变常量的值吗 不可以 一旦改变 ...
- 浅谈jquery选择器
首先来说说jquery选择器的优势: 1.简洁的写法 2.支持css1.0到3.0选择器 3.完善的处理机制. 再来说说分类: jquery选择器分为基本选择器.层次选择器.属性选择器.基本过滤选择 ...
- (转载)OC学习篇之---归档和解挡
前几篇文章说到了OC中的Foundation框架,今天我们来看一下OC中的一个重要知识点:归档 OC中的归档就是将对象写入到一个文件中,Java中的ObjectInputStream和ObjectOu ...
- web前端笔试题
1, 判断字符串是否是这样组成的,第一个必须是字母,后面可以是字母.数字.下划线,总长度为5-20 var reg = /^[a-zA-Z][a-zA-Z_0-9]{4,19}$/; reg.test ...
- phpcms学习总结
文件目录结构 根目录 | – api 接口文件目录 | – caches 缓存文件目录 | – configs 系统配置文件目录 | – caches_* 系统缓存目录 | – phpcms phpc ...
- Python进程和线程
引入进程和线程的概念及区别 1.线程的基本概念 概念 线程是进程中执行运算的最小单位,是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点在运行中必不可少的资源,但 ...
- PHP获取本周开始时间
/*先设置时区*/date_default_timezone_set('PRC');/*网上的写法:总觉得这周跨年或者跨月的时候会悲剧 未验证*/echo mktime(0,0,0,date('m') ...
- 在Cubieboard上关闭irqbalance服务避免内存泄漏
十一一个假期回来,顺手看了看自己的cubieboard运行状态怎么样 aria2正常: btsync正常: samba正常: 很好, 顺手htop一下,已经开机了13天了,CPU使用率4%,内存使用率 ...
- gradle使用eclipse debug 代码
设置环境变量 unix,linux export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,s ...