通过输入方式在Android上进行微博OAuth登录

    在微博认证方式里,基本的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帐号的重要性,也可以理解。另外腾讯微博最近提供了手机端的登录方式支持,有兴趣的同学可以自行了解。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值