新浪微博(十五)授权成功后的中转站(ShowAccessActivity类)

来源:互联网 发布:mac如何添加字体到ps 编辑:程序博客网 时间:2024/06/05 14:07
/* * 我这里写的这个activity对用户不可见,只是个中转站 * 它负责把用户的appkey,appsecret等信息存储到本地后,并激活另一个显示微博信息的activity。 * 这个activity的名字是WeiBoInfo * 当然,你也可以在同一个activity中实现这种功能,既存储信息又显示微博。 */package tianyi.sina;import tianyi.oauth.Oauth;import tianyi.oauth.OauthUtils;import tianyi.weibo.WeiBoFrameActivity;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.widget.Toast;public class ShowAccessActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);Intent intent = getIntent();// url是http://www.baidu.com/?oauth_token=一串字符&oauth_verifier=一串数字String url = intent.getStringExtra("testurl");Uri uri = Uri.parse(url);// 匹配验证码,从url中获取oauth_verifier,显然它的值是一串数字,如825778String oauth_verifier = uri.getQueryParameter("oauth_verifier");String Oauth_token_key = OauthUtils.getInstance().getOauth_token_key();String Token_secret = OauthUtils.getInstance().getToken_secret();/* * 这是授权验证的第三步, 用 oauth_token、oauth_token_secret、oauth_verifier的数据请求, * 交换access_token 拿到交换后的oauth_token 和 oauth_token_secret。 * 在getAccessToken()方法中已经对交换后的token和tokensecret值执行了set()方法 * 因而下文代码使用get()方法便可获取交换后的token和tokensecret值 */new Oauth().getAccessToken(Token_secret, Oauth_token_key,oauth_verifier);String Oauth_access_key = OauthUtils.getInstance().getAccess_token_key();String Access_secret = OauthUtils.getInstance().getAccess_secret();String userid = OauthUtils.getInstance().getId();// 将access_token和access_secret存储到配置文件parametersSharedPreferences share = getSharedPreferences("parameters",Context.MODE_PRIVATE);Editor editor = share.edit();editor.putString("sina_AppKey", OauthUtils.sina_AppKey);editor.putString("sina_AppSecret", OauthUtils.sina_AppSecret);editor.putString("sina_access_token", Oauth_access_key);editor.putString("sina_access_secret", Access_secret);editor.putString("user_id", userid);editor.commit();change();}private void change() {Handler handler = new Handler();Runnable mTasks = new Runnable() {@Overridepublic void run() {Toast.makeText(ShowAccessActivity.this, "亲,正在跳转……",Toast.LENGTH_SHORT).show();Intent intent2 = new Intent();// WeiBoFrameActivity是用来显示微博信息的activityintent2.setClass(ShowAccessActivity.this, WeiBoFrameActivity.class);intent2.putExtra("Sign", "TRUE");intent2.putExtra("address", "show");startActivity(intent2);}};handler.post(mTasks);}}

原创粉丝点击