<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"/>
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; //Weibo引用对象 public static Oauth2AccessToken accessToken; //访问token private Button oauthBtn; private TextView mText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取Weibo引用对象,传入appkey和回调url 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() { //Oauth2.0认证过程中,如果认证窗口被关闭或认证取消时调用 Toast.makeText(getApplicationContext(), "Auth cancel" , Toast.LENGTH_LONG).show(); } @Override public void onComplete(Bundle values) { // 认证结束后调用此方法 String token = values.getString("access_token");// 表明用户身份的token,用于微博API的调用 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) { //Oauth2.0认证过程中,当认证对话框中的webView接收数据出现错误时调用此方法 Toast.makeText(getApplicationContext(), "Auth error:" + e.getMessage(), Toast.LENGTH_LONG).show(); } @Override public void onWeiboException(WeiboException e) { //当认证过程中捕获到WeiboException时调用 Toast.makeText(getApplicationContext(), "Auth exception:" + e.getMessage(), Toast.LENGTH_LONG).show(); } } }
package com.weibo.sdk.android.demo; /** * 定义一个常量接口,方便管理常量 * @author wwj * */ public interface ConstantS { // 应用的key请到官方申请正式的appkey替换APP_KEY public static final String APP_KEY = "249239496"; // 替换为开发者REDIRECT_URL public static final String REDIRECT_URL = "http://www.sina.com"; // 新支持scope,支持传入多个scope权限,用逗号分隔 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"; }
public static Weibo getInstance(java.lang.String appKey, java.lang.String redirectUrl, java.lang.String aScope)
appKey
- 第三方应用的appkey
redirectUrl
- 第三方应用的回调页
java.lang.RuntimeException
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; /** * 该类用于保存Oauth2AccessToken到sharepreference,并提供读取功能 * * @author wwj * */ public class AccessTokenKeeper { private static final String PREFERENCES_NAME = "com_weibo_sdk_android"; /** * 保存accsssToken到SharedPreferences * @param context 上下文对象 * @param token Oauth2AccessToken */ 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(); } /** * 清空sharedPreferences * @param context */ public static void clear(Context context) { SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND); Editor editor = pref.edit(); editor.clear(); editor.commit(); } /** * 从SharedPreferences读取accessToken * @param context * @return Oauth2AccessToken */ 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; } }
很多同学不知道如何填写这一项,我建议开发者填写默认的回调页就行了,在调取授权页面的时候是需要传这个回调地址过去的,不然也是无法进行授权。
无法获取访问令牌,也是很容易出现的问题,一些同学并不知道需要添加相应的测试帐号,所以一直无法进行登录授权,总是提示无法获取access_token。添加测试帐号如下图所示: