通过输入方式在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) ...
随机推荐
- 简易nagios安装出现的问题及解决方法
安装时候所遇到的问题: A 安装PHP的时候报错xml2-config未找到 安装PHP的时候报错:未找到包libxml2 解决方法:安装libxml2包即可,libxml2是php的依赖包,从而需要 ...
- bzoj3717: [PA2014]Pakowanie
Time Limit: 90 Sec Memory Limit: 256 MBSubmit: 128 Solved: 43[Submit][Status][Discuss]Description 你有 ...
- linux下使用libiconv库转码
iconv命令实现linux下字符集编码的转换 windows下的文件复制到linux下时常会乱码,因为windows下文件编码为GBK,linux下默认文件编码为UTF-8,故需要libiconv库 ...
- 生成500个0-1000的随机数&&数组查找—小练习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ubuntu 14.04 root破解
Advanced Programmable interrupt controller 高级可编程中断控制;Advanced create table `users` (`id` int(11) not ...
- AutoCAD.NET二次开发错误集锦
1.CAD崩溃,错误“unhandled access violation reading”=“未将对象引用设置到对象的实例”. 2. 建模操作错误:指向给定边的指针为空. 建模操作错误:访问冲突. ...
- centos安装postfixadmin
postfixadmin的安装,跟普通网站安装没什么区别 配置好虚拟目录,然后在数据库中创建数据库postfix 修改config.inc.php文件,详细搜索谷歌 访问http://www.你的域名 ...
- My集合框架第六弹 左式堆
左式堆(Leftist Heaps)又称作最左堆.左倾堆.左式堆作为堆的一种,保留了堆的一些属性. 第1,左式堆仍然以二叉树的形式构建: 第2,左式堆的任意结点的值比其子树任意结点值均小(最小堆的特性 ...
- 10个经典的Java main 方法面试题
1. 不用main方法如何定义一个类? 不行,没有main方法不能运行Java类. 在Java 7之前,你可以通过使用静态初始化运行Java类.但是,从Java 7 开始就不行了. 2. main() ...
- JedisPool使用原理和源代码
1,JedisPool的使用 <!-- 连接池的配置信息 --><beanid="jedisConfig"class="redis.clients.je ...