@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.oauthv1); oAuth = new OAuthV1(oauthCallback); oAuth.setOauthConsumerKey(oauthConsumeKey); oAuth.setOauthConsumerSecret(oauthConsumerSecret); // 设置界面元素,并添加对各按钮的监听 btnRequest = (Button) findViewById(R.id.btnRequest); textRequest = (TextView) findViewById(R.id.textRequest); btnAuthorize = (Button) findViewById(R.id.btnAuthorize); textVerifier = (TextView) findViewById(R.id.textVerifier); btnAccess = (Button) findViewById(R.id.btnAccess); textAccess = (TextView) findViewById(R.id.textAccess); btnAPItest = (Button) findViewById(R.id.btnAPItest); btnRequest.setOnClickListener(listener); btnAuthorize.setOnClickListener(listener); btnAccess.setOnClickListener(listener); btnAPItest.setOnClickListener(listener); // 关闭OAuthV1Client中的默认开启的QHttpClient。 OAuthV1Client.getQHttpClient().shutdownConnection(); // 为OAuthV1Client配置自己定义QHttpClient。 OAuthV1Client.setQHttpClient(new QHttpClient()); }
public OAuthV1 getOAuthV1() throws Exception { OAuthV1 oAuth = new OAuthV1("null"); oAuth.setOauthConsumerKey(APP_KEY); oAuth.setOauthConsumerSecret(APP_SECRET); oAuth = OAuthV1Client.requestToken(oAuth); return oAuth; }
public void onClick(View v) { Intent intent; switch (v.getId()) { case R.id.btnRequest: Log.i(TAG, "---------Step1: Get requestToken--------"); try { // 向腾讯微博开放平台请求获得未授权的Request_Token oAuth = OAuthV1Client.requestToken(oAuth); } catch (Exception e) { e.printStackTrace(); } textRequest.append( "\nrequest_token:\n" + oAuth.getOauthToken() + "\nrequest_token_secret:\n" + oAuth.getOauthTokenSecret()); break; case R.id.btnAuthorize: Log.i(TAG, "---------Step2: authorization--------"); Log.i(TAG, "start WebView intent"); intent = new Intent( OAuthV1Activity.this, OAuthV1AuthorizeWebView.class); // 创建Intent,使用WebView让用户授权 intent.putExtra("oauth", oAuth); startActivityForResult(intent, 1); break; case R.id.btnAccess: Log.i(TAG, "---------Step3: getAccessToken--------"); try { oAuth = OAuthV1Client.accessToken(oAuth); /* * 注意:此时oauth中的Oauth_token和Oauth_token_secret将发生变化,用新获取到的 * 已授权的access_token和access_token_secret替换之前存储的未授权的request_token * 和request_token_secret. */ } catch (Exception e) { e.printStackTrace(); } textAccess.append( "\naccess_token:\n" + oAuth.getOauthToken() + "\naccess_token_secret:\n" + oAuth.getOauthTokenSecret()); break; case R.id.btnAPItest: Log.i(TAG, "---------Step4: Test API V1--------"); intent = new Intent( OAuthV1Activity.this, WeiBoAPIV1Activity.class); // 创建Intent,转到调用Qweibo API的Activity intent.putExtra("oauth", oAuth); startActivity(intent); break; } }
/* * 通过读取OAuthV1AuthorizeWebView返回的Intent,获取用户授权后的验证码 */ protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1) { if (resultCode == OAuthV1AuthorizeWebView.RESULT_CODE) { // 从返回的Intent中获取验证码 oAuth = (OAuthV1) data.getExtras().getSerializable("oauth"); textVerifier.append("\nverifier:\n" + oAuth.getOauthVerifier()); } } }
@Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); WebSettings settings = webview.getSettings(); settings.setJavaScriptEnabled(true); settings.setSupportZoom(true); webview.requestFocus(); String authornizeUrl = OAuthConstants.OAUTH_V1_AUTHORIZE_URL + "?oauth_token=" + oauth.getOauthToken(); webview.loadUrl(authornizeUrl); webview.setWebChromeClient( new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { pb.setProgress(newProgress); if (newProgress == 100) { pb.setVisibility(View.GONE); } } }); client = new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { if (url.indexOf("checkType=verifycode") != -1) { int start = url.indexOf("checkType=verifycode&v") + 23; int end = start + 6; String verifyCode = url.substring(start, end); oauth.setOauthVerifier(verifyCode); Intent intent = new Intent(); intent.putExtra("oauth", oauth); setResult(RESULT_CODE, intent); view.destroyDrawingCache(); finish(); } } }; webview.setWebViewClient(client); }