新浪微博客户端开发之OAuth认证篇
2013年7月29日新浪微博客户端开发
OAuth2.0授权机制我在这里就不浪费口舌了,有很多大牛都发表过相关的文章解释OAuth2.0认证的流程,我就随便找了一张图片把这部分内容略过。
在新浪微博API中呢,如何去实现这个认证流程是下面要说的:
实现步骤:
1. 下载新浪微博Android SDK,去新浪开发平台那里找去。
2. 创建项目,这里是OauthTest
2. 解压SDK压缩包,知道jar把它导入到项目当中
项目文档结构如下:
运行项目效果:
这里要说一下,我用的jar包并不是最新版的SDK,因为我测试过最新的这个“weiboSDK2.1_130712.jar”,在授权的时候会出现NumberFormatException的错误,我也不知道是为什么,所以我换了前面的一个版本:
weibo.sdk.android.sso.jar,就可以正确地获取access_token和expires_in,童鞋们可以自己去试一下。
下面是项目代码:
1.首先增加权限
[html] view plain copy print ?
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2. /OauthTest/src/com/weibo/sdk/android/demo/MainActivity.java
[java] view plain copy print ?
- package com.weibo.sdk.android.demo;
-
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
-
- import com.weibo.sdk.android.Oauth2AccessToken;
- import com.weibo.sdk.android.Weibo;
- import com.weibo.sdk.android.WeiboAuthListener;
- import com.weibo.sdk.android.WeiboDialogError;
- import com.weibo.sdk.android.WeiboException;
- import com.weibo.sdk.android.keep.AccessTokenKeeper;
-
- public class MainActivity extends Activity {
-
- private Weibo mWeibo;
- public static Oauth2AccessToken accessToken;
-
- private Button oauthBtn;
- private TextView mText;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- mWeibo = Weibo.getInstance(ConstantS.APP_KEY, ConstantS.REDIRECT_URL);
- mText = (TextView) findViewById(R.id.show);
- oauthBtn = (Button) findViewById(R.id.oauthBtn);
- MainActivity.accessToken = AccessTokenKeeper.readAccessToken(this);
-
-
- oauthBtn.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- mWeibo.authorize(MainActivity.this, new AuthDialogListener());
- }
- });
-
- }
-
- public class AuthDialogListener implements WeiboAuthListener {
-
- @Override
- public void onCancel() {
-
- Toast.makeText(getApplicationContext(), "Auth cancel" , Toast.LENGTH_LONG).show();
-
- }
-
- @Override
- public void onComplete(Bundle values) {
-
- String token = values.getString("access_token");
- String expires_in = values.getString("expires_in");
- System.out.println(expires_in);
- MainActivity.accessToken = new Oauth2AccessToken(token, expires_in);
- String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
- .format(new java.util.Date(MainActivity.accessToken
- .getExpiresTime()));
- mText.setText("认证成功: \r\n access_token: " + token + "\r\n"
- + "expires_in: " + expires_in + "\r\n有效期:" + date);
-
- AccessTokenKeeper.keepAccessToken(MainActivity.this, accessToken);
- Toast.makeText(MainActivity.this, "认证成功", Toast.LENGTH_SHORT)
- .show();
- }
-
- @Override
- public void onError(WeiboDialogError e) {
-
- Toast.makeText(getApplicationContext(), "Auth error:" + e.getMessage(), Toast.LENGTH_LONG).show();
- }
-
- @Override
- public void onWeiboException(WeiboException e) {
-
- Toast.makeText(getApplicationContext(), "Auth exception:" + e.getMessage(), Toast.LENGTH_LONG).show();
-
- }
-
- }
- }
3. /OauthTest/src/com/weibo/sdk/android/demo/ConstantS.java
[java] view plain copy print ?
- package com.weibo.sdk.android.demo;
-
-
-
-
-
-
-
- public interface ConstantS {
-
- public static final String APP_KEY = "249239496";
-
- public static final String REDIRECT_URL = "http://www.sina.com";
-
- public static final String SCOPE = "email,direct_messages_read,direct_messages_write," +
- "friendships_groups_read,friendships_groups_write,statuses_to_me_read," +
- "follow_app_official_microblog";
-
-
- }
原本SCOPE是要用在新版的SDK中,这是新增的一个参数
方法声明是这样的:
public static Weibo getInstance(java.lang.String appKey,
java.lang.String redirectUrl,
java.lang.String aScope)
-
微博初始化函数,需要优先其他函数调用.如果appKey或redirectUrl为空,默认抛出RuntimeException
-
-
参数:
-
appKey
- 第三方应用的appkey
-
redirectUrl
- 第三方应用的回调页
-
返回:
-
Weibo的实例
-
抛出:
-
java.lang.RuntimeException
4./OauthTest/src/com/weibo/sdk/android/keep/AccessTokenKeeper.java
[java] view plain copy print ?
- package com.weibo.sdk.android.keep;
-
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
-
- import com.weibo.sdk.android.Oauth2AccessToken;
-
-
-
-
-
-
-
- public class AccessTokenKeeper {
-
- private static final String PREFERENCES_NAME = "com_weibo_sdk_android";
-
-
-
-
-
-
- public static void keepAccessToken(Context context, Oauth2AccessToken token) {
- SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
- Editor editor = pref.edit();
- editor.putString("token", token.getToken());
- editor.putLong("expiresTime", token.getExpiresTime());
- editor.commit();
- }
-
-
-
-
- public static void clear(Context context) {
- SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
- Editor editor = pref.edit();
- editor.clear();
- editor.commit();
- }
-
-
-
-
-
- public static Oauth2AccessToken readAccessToken(Context context) {
- Oauth2AccessToken token = new Oauth2AccessToken();
- SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
- token.setToken(pref.getString("token", ""));
- token.setExpiresTime(pref.getLong("expiresTime", 0));
- return token;
- }
- }
以上就是简单的实现了第三方程序访问用户微博授权,没有太复杂的地方,主要是为了熟悉新浪微博的授权过程。
原:http://blog.csdn.net/wwj_748/article/details/9621051