在微博认证方式里,基本的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" android:layout_width="fill_parent"android:layout_height="fill_parent" android:scrollbars="vertical"android:fadingEdge="vertical"> <TextView android:layout_width="fill_parent" android: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上截屏?

    如何通过代码而不是通过任何程序来截屏电话屏幕的选定区域? #1楼 Mualig的回答很好,但是我遇到了Ewoks描述的相同问题,但我没有得到背景知识. 因此,有时足够好,有时我会在黑色背景上出现黑色文 ...

  2. android 代码 截取屏幕,如何以编程方式在Android上截取屏幕截图?

    这是允许我的屏幕截图存储在SD卡上的代码,以后用于满足您的任何需求: 首先,您需要添加适当的权限来保存文件: 这是代码(在Activity中运行):private void takeScreensho ...

  3. 相机java程序_以编程方式在Android上用相机拍照

    请看下面的演示代码 . 这是用于UI的XML文件, android:layout_width="fill_parent" android:layout_height="f ...

  4. android上传本地图片到服务器上,Android使用post方式上传图片到服务器的方法

    本文实例讲述了Android使用post方式上传图片到服务器的方法.分享给大家供大家参考,具体如下: /** * 上传文件到服务器类 * * @author tom */ public class U ...

  5. 长微博android,Android 上最强大的长微博工具:BlackLight 长微博

    手机端的长微博工具屈指可数,Android 上的更是寥寥无几.除却官方客户端那不太像样的长微博功能之外,能够将长文字转换成图片的恐怕就只有锤子便签和 Smooth 了.BlackLight 作为新晋的 ...

  6. 全面详解Android实现多线程的几种方式(史上最全,最详细)

    一.前言 Android多线程实现方式包括: 1.基础使用 继承Thread类 实现Runnable接口 Handler 2.复合使用 AsyncTask HandlerThread IntentSe ...

  7. android.mk 添加v7_在Android上以命令行方式移植FFmpeg

    最近要做视频I帧提取和摘要生成的项目,在Android平台上的应用,经过调研,发现ffmpeg有很多相关功能的轮子,因此考虑将ffmpeg移植到Android平台.为了更好解耦和以及更多的文档参考,选 ...

  8. 如何在 Android 上恢复删除屏幕截图/照片的四种方式

    以下是在 Android 上恢复丢失的屏幕截图的方法--在您的 Android 手机上打开 Google 相册.在左上角,单击三个垂直线,然后选择垃圾箱.单击您要取消删除的照片,然后单击恢复以将选定的 ...

  9. android 腾讯微博授权,5腾讯微博Android客户端开发获取请求用户授权Request Token.pdf...

    腾讯微博Android客户端开发 博客:/coolszy Android Android 腾讯微博AAnnddrrooiidd客户端开发 RequestToken RequestToken --获取请 ...

最新文章

  1. 密切值matlab程序,密切值法
  2. python公共键_Python利用公共键如何对字典列表进行排序详解
  3. curl php 模拟来源_PHP-Curl模拟HTTPS请求
  4. pp助手苹果版_再见!PP助手iOS端即将下线 曾是中国最大的苹果助手
  5. python中imread导入失败_ImportError:无法导入加载图像文件所需的Python Imaging Library(PIL)...
  6. jfinal 源码中文乱码解决
  7. Spring Boot 2.x 把 Guava 干掉了,拥抱本地缓存之王 Caffeine!
  8. mint ui css覆盖,vue中配置mint-ui报css错误问题的解决方法
  9. ScheduledExecutorService 延迟 / 周期执行线程池
  10. LBP及纹理表达 转自http://blog.sina.com.cn/s/blog_ba9d7d9901018k4v.html
  11. Eclipse找不到或无法加载主类
  12. 计算机视觉应用期末试卷,计算机视觉期末复习
  13. Gson解析json文件
  14. win7桌面背景地址
  15. 误差棒到底是个什么棒?到底棒不棒!
  16. Android 签名方法---同时使用V1和V2签名
  17. HTML中label标签的用途
  18. 数据结构初阶:二叉树
  19. 网友感到担忧!iOS 17支持第三方应用商店:这下跟安卓没区别了
  20. 一个EXE引发的危机 — 浏览器劫持实战篇

热门文章

  1. python---之os.path.splitext(“文件路径”)
  2. 数学之---KL散度
  3. PyTorch 入坑五 autograd与逻辑回归
  4. filemode对git diff的影响
  5. 齐次坐标和单应性矩阵
  6. c++工程模式+配置文件+动态调用类
  7. mysql序列化字段反序列化_序列化serialize()与反序列化unserialize()的实例
  8. python 自动填excel_使用python自动填充文字.docx从excel fi
  9. java中怎么把数字打印在屏幕上_java中如何打印出蜗牛形状的数字
  10. linux运行非法指令,illegal instruction非法指令的解决思路