新浪微博 android 开发中 oauth2 认证有两种 一种是code 一种的sso
code的一种 最常用 sso的需要安装新浪微博客户端3.0以上
刚申请的新应用复用以前的代码就报 permision denied,百思不得其解。新浪微博开发的不向前兼容的么,后来查看
得知,需要把授权参数从原来的 token 替换为code
parameters.add("response_type", "code");
问题来了,得到code 如何换取 access_token呢
而开发中code方式 每次都需要授权 很繁琐,
所以就去官方问:如何授权后 得到Oauth2AccessToken
官方的回答是:请自行从CODE中获取Oauth2AccessToken
听到这句话 蛋都碎了一地
找了两个小时 终于找到 解决方法
通过这个URLhttps://api.weibo.com/oauth2/access_token
POST过去后 返回JSON字符串
JSON字符串中就有
access_token
expires_in
坑爹的是expires_in 是按秒计算的
而oauth2AccessToken.isSessionValid() 是按 毫秒计算的.....
哥又绕了20分钟才过去
半成品的API 伤不起啊!!
附上源代码:
AccessTokenKeeper类 主要用于访问 保存文件的
- 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;
- }
- }
MainActivity 住页面
- public class MainActivity extends Activity {
- private Weibo weibo;
- private static Oauth2AccessToken oauth2AccessToken;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- weibo = Weibo.getInstance(ConstantS.APP_KEY,ConstantS.REDIRECT_URL,ConstantS.SCOPE);
- MainActivity.oauth2AccessToken = AccessTokenKeeper.readAccessToken(this);
- if (oauth2AccessToken!=null&&oauth2AccessToken.isSessionValid()) {//验证通过
- //跳向主页面
- String date = new java.text.SimpleDateFormat("yyyy/MM/dd hh:mm:ss")
- .format(new java.util.Date(MainActivity.oauth2AccessToken
- .getExpiresTime()));
- Toast.makeText(this,"access_token 仍在有效期内,无需再次登录: \naccess_token:"
- + MainActivity.oauth2AccessToken.getToken() + "\n有效期:" + date,50000).show();
- Intent intent=new Intent(MainActivity.this,LuoBoActivity.class);
- startActivity(intent);
- }
- else {
- weibo.anthorize(MainActivity.this, new authdiloag());
- }
- }
- class authdiloag implements WeiboAuthListener
- {
- @Override
- public void onComplete(Bundle values) {
- String code=values.getString("code");
- // AccessTokenKeeper.keepAccessToken(MainActivity.this, oauth2AccessToken);
- if (code!=null) {
- Toast.makeText(MainActivity.this, "认证code成功"+code, Toast.LENGTH_SHORT).show();
- //android 4.0以后访问HTTP必须异步处理
- httpThread hThread=new httpThread(code);
- Thread thread=new Thread(hThread);
- thread.start();
- Intent intent=new Intent(MainActivity.this,LuoBoActivity.class);
- startActivity(intent);
- }
- }
- @Override
- public void onCancel() {
- }
- @Override
- public void onError(WeiboDialogError arg0) {
- }
- @Override
- public void onWeiboException(WeiboException arg0) {
- } }
- class httpThread implements Runnable
- {
- String code;
- public httpThread(String code)
- {
- this.code=code;
- }
- @Override
- public void run() {
- try {//key secret 在基本信息里 回调页面 在高级信息里
- HttpPost post=new HttpPost(URI.create("https://api.weibo.com/oauth2/access_token?client_id=自己的APPKEY&client_secret=自己的SECRET&grant_type=authorization_code&redirect_uri=自己的回调页面&code="+code));
- HttpClient httpClient=new DefaultHttpClient();
- HttpResponse response= httpClient.execute(post);
- if (response.getStatusLine().getStatusCode()==200) {
- String temp=EntityUtils.toString(response.getEntity());
- JSONObject o=new JSONObject(temp);
- String access_token=o.getString("access_token");
- String express_in=o.getString("expires_in");
- //这里按照 毫秒计算
- long time= Long.parseLong(express_in)*1000+ System.currentTimeMillis();
- if (oauth2AccessToken==null) {
- oauth2AccessToken=new Oauth2AccessToken();
- }
- oauth2AccessToken.setExpiresTime(time);
- oauth2AccessToken.setToken(access_token);
- AccessTokenKeeper.keepAccessToken(MainActivity.this, oauth2AccessToken);
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (ParseException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- }
- 记录之,帮助后来人~