在微博认证方式里,基本的OAuth认证是必须要调整到跳转到第三方页面上进行授权的,例如下面的例子:

    1、从http://open.weibo.com/wiki/index.php/SDK#Android下载SDK包。
    2、在AndroidExample/src/weibo4android/Weibo.java中填入App key和App Secret。
 
public class Weibo extends WeiboSupport implements java.io.Serializable {
public static String CONSUMER_KEY = "41199486xx";
public static String CONSUMER_SECRET = "d589738xx1e0026xxce22xx84cf87dxx";
    3、运行工程。
 
    4、点击GoGo后跳转到新浪微博的认证页面。
    5、认证成功,可以根据AccessToken访问微博的接口。
 
    在以上的过程中,手机端跳转过程非常麻烦,需要打开浏览器,一方面新浪显示的页面无法根据应用风格定制,另外有很大可能性由于浏览器原因无法打开页面,或者输入失败后就无法回到应用程序中。
 
    想到在BasicAuth的方式下,我们是可以在自己的应用中输入用户名和密码,这样控制起来非常方便。那么我们能否结合BasicAuth方式的简单和OAuth方式的安全性呢?
 
    新浪微博提供了callback=json的方式来帮助我们绕过OAuth的跳转步骤,只需要将用户名和密码传递给oauth/authorize接口,即可直接获得verifiercode。相关说明如下:
    我们下面根据这种方式来修改上面的SDK以支持用户名和密码输入方式。
 
    打开res/main.xml文件,注释掉Button01,添加两个输入框、一个按钮和TextView。
<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>
然后在src下的weibo4android.http包里添加OAuthVerifier.java类,这个类是实体类,代表OAuth的VerifierCode对象,代码如下:
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;
}
保存并运行工程。
 
    在弹出的界面上输入新浪微博帐号和密码,并点击auth按钮。
 
    马上能够看到获取到的access_token和access_token_secret。
 
    我在去年7月开发的新浪微博傲游插件也是采用这种方式实现的OAuth认证,只是全部采用Javascript实现,在新浪微博认证方式从Basic Auth切换到OAuth的情况下不受任何影响。
    腾讯微博没有提供这种OAuth方式的支持,我一直非常遗憾,考虑到QQ帐号的重要性,也可以理解。另外腾讯微博最近提供了手机端的登录方式支持,有兴趣的同学可以自行了解。  

通过输入方式在Android上进行微博OAuth登录的更多相关文章

  1. 在Android上实现使用Facebook登录(基于Facebook SDK 3.5)

    准备工作: 1.       Facebook帐号,国内开发者需要一个vpn帐号(网页可以浏览,手机可以访问) 2.       使用Facebook的SDK做应用需要一个Key Hashes值. 2 ...

  2. Android上的SQLLite性能分析

    也许有人还不知道,Android 是有一些内建的 类库支持 SQL Lite 数据库的操作.他提供了一个很好的方式在 Android 上组织少量的数据.不管怎样,在使用这些类库的时候有一些陷阱是需要注 ...

  3. 关于Unity程序在IOS和Android上显示内嵌网页的方式

    近期因为有须要在Unity程序执行在ios或android手机上显示内嵌网页.所以遍从网上搜集了一下相关的资料.整理例如以下: UnityWebCore 从搜索中先看到了这个.下载下来了以后发现这个的 ...

  4. 一个Android上的以滑动揭示的方式显示并切换图片的View

    SlideView是一个Android上的以滑动揭示的方式显示并切换图片的View,以视觉对比的方式把一套相似的图片展示出来. 示例 翻页图片揭示效果: 特性 设置一组(List<ImageIn ...

  5. [Deprecated!] Android开发案例 - 微博正文

    Deprecated! 更好的实现方式: 使用 android.support.design.widget.CoordinatorLayout. 本文详细介绍如何实现如下图中的微博正文页面效果, 其中 ...

  6. 系列篇|编译可在Android上运行的依赖库(一):glib库

    前言 这是系列文章,它们由<编译可在Android上运行的glib库>及其他4篇文章组成,这4篇文章在“编译依赖库”一节中列出.由于glib库依赖于其他第三方库,所以需要先将依赖的第三方库 ...

  7. ZT 理解 Android 上的安全性

    理解 Android 上的安全性 http://www.ibm.com/developerworks/cn/xml/x-androidsecurity/ 利用沙箱.应用程序签名和权限增强应用程序安全性 ...

  8. 如何在Android上编写高效的Java代码

    转自:http://www.ituring.com.cn/article/177180 作者/ Erik Hellman Factor10咨询公司资深移动开发顾问,曾任索尼公司Android团队首席架 ...

  9. 如何调试 Android 上 HTTP(S) 流量

    http://greenrobot.me/devpost/how-to-debug-http-and-https-traffic-on-android/ 如何调试 Android 上 HTTP(S) ...

随机推荐

  1. R 环境内存限制的更改

    由于R语言非常消耗内存,所以做较大数据的处理时需要增加内存空间,有以下种方式: 一. 在未开启R之前,在cmd中,输入下面指令 r −−max−mem− s i z e =4Gb 二. 在开启R之后, ...

  2. 【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 ...

  3. 闲谈Future模式-订蛋糕

    一. Future模式简介 Future有道翻译:n. 未来:前途:期货:将来时.我觉得用期货来解释比较合适.举个实际生活中例子来说吧,今天我女朋友过生日,我去蛋糕店准备给女朋友定个大蛋糕,超级大的那 ...

  4. CentOS7 安装 scala 2.11.1

    wget http://downloads.typesafe.com/scala/2.11.6/scala-2.11.6.tgz?_ga=1.61986863.2013247204.144801902 ...

  5. C语言基础(不断更新)

    1.memcpy. memmove.memccpy的区别 字符串函数功能查询 memcpy要求源串和目的串不能重叠 memccpy:copy直至遇到由参数指定的ch. memmove: 源串和目的串可 ...

  6. 正整数N是否是素数

    来自:[数据结构与算法分析——C语言描述]练习2.13 问题描述: a. 编写一个程序来确定正整数N是否是素数. b. 你的程序在最坏的情形下的运行时间是多少(用N表示)? c. 令B等于N的二进制表 ...

  7. Java ClassLoader 原理详细分析

    一.什么是ClassLoader? 大家都知道,当我们写好一个Java程序之后,不是管是CS还是BS应用,都是由若干个.class文件组织而成的一个完整的Java应用程序,当程序在运行时,即会调用该程 ...

  8. 第三百四十二天 how can I 坚持

    再问世间都去哪儿了,天气预报没搞完,计划没制定,又周三了. 今天回到家八点,吃完饭接近九点,和老妈开了会视频,这就九点半多了,发了呆洗了个碗就到这时候了,整天浑浑噩噩的,该如何是好. 又有点上火,舌头 ...

  9. OpenGL复习要点

    [OpenGL要点复习] 1.和像素有关的信息(例如像素的颜色)组织成位平面 (bitplane)的形式,位平面又可以组织成帧缓冲区(framebuffer)的形式.位平面是一块内存区域,保存了屏幕上 ...

  10. 时隔3年,再次折腾BlackBerry 8830!

    2010年手头换得8830,之后就是好几番刷机.解SPC.倒腾各种软件..算软件注册码..那个时候记得最难弄的注册码就是crunchSMS.需要运行虚拟机来从内存地址读取注册码..不过黑莓真的很经得起 ...