通过输入方式在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) ...
随机推荐
- R 环境内存限制的更改
由于R语言非常消耗内存,所以做较大数据的处理时需要增加内存空间,有以下种方式: 一. 在未开启R之前,在cmd中,输入下面指令 r −−max−mem− s i z e =4Gb 二. 在开启R之后, ...
- 【LeetCode】111 - Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 闲谈Future模式-订蛋糕
一. Future模式简介 Future有道翻译:n. 未来:前途:期货:将来时.我觉得用期货来解释比较合适.举个实际生活中例子来说吧,今天我女朋友过生日,我去蛋糕店准备给女朋友定个大蛋糕,超级大的那 ...
- CentOS7 安装 scala 2.11.1
wget http://downloads.typesafe.com/scala/2.11.6/scala-2.11.6.tgz?_ga=1.61986863.2013247204.144801902 ...
- C语言基础(不断更新)
1.memcpy. memmove.memccpy的区别 字符串函数功能查询 memcpy要求源串和目的串不能重叠 memccpy:copy直至遇到由参数指定的ch. memmove: 源串和目的串可 ...
- 正整数N是否是素数
来自:[数据结构与算法分析——C语言描述]练习2.13 问题描述: a. 编写一个程序来确定正整数N是否是素数. b. 你的程序在最坏的情形下的运行时间是多少(用N表示)? c. 令B等于N的二进制表 ...
- Java ClassLoader 原理详细分析
一.什么是ClassLoader? 大家都知道,当我们写好一个Java程序之后,不是管是CS还是BS应用,都是由若干个.class文件组织而成的一个完整的Java应用程序,当程序在运行时,即会调用该程 ...
- 第三百四十二天 how can I 坚持
再问世间都去哪儿了,天气预报没搞完,计划没制定,又周三了. 今天回到家八点,吃完饭接近九点,和老妈开了会视频,这就九点半多了,发了呆洗了个碗就到这时候了,整天浑浑噩噩的,该如何是好. 又有点上火,舌头 ...
- OpenGL复习要点
[OpenGL要点复习] 1.和像素有关的信息(例如像素的颜色)组织成位平面 (bitplane)的形式,位平面又可以组织成帧缓冲区(framebuffer)的形式.位平面是一块内存区域,保存了屏幕上 ...
- 时隔3年,再次折腾BlackBerry 8830!
2010年手头换得8830,之后就是好几番刷机.解SPC.倒腾各种软件..算软件注册码..那个时候记得最难弄的注册码就是crunchSMS.需要运行虚拟机来从内存地址读取注册码..不过黑莓真的很经得起 ...